diff --git a/src/renderer/canvas/CanvasRenderer.js b/src/renderer/canvas/CanvasRenderer.js index 17b16f920..f60c29148 100644 --- a/src/renderer/canvas/CanvasRenderer.js +++ b/src/renderer/canvas/CanvasRenderer.js @@ -354,6 +354,7 @@ var CanvasRenderer = new Class({ * Called at the start of the render loop. * * @method Phaser.Renderer.Canvas.CanvasRenderer#preRender + * @fires Phaser.Renderer.Events#PRE_RENDER_CLEAR * @fires Phaser.Renderer.Events#PRE_RENDER * @since 3.0.0 */ @@ -369,6 +370,8 @@ var CanvasRenderer = new Class({ ctx.globalCompositeOperation = 'source-over'; ctx.setTransform(1, 0, 0, 1, 0, 0); + this.emit(Events.PRE_RENDER_CLEAR); + if (config.clearBeforeRender) { ctx.clearRect(0, 0, width, height); diff --git a/src/renderer/events/PRE_RENDER_CLEAR_EVENT.js b/src/renderer/events/PRE_RENDER_CLEAR_EVENT.js new file mode 100644 index 000000000..9cc066a1c --- /dev/null +++ b/src/renderer/events/PRE_RENDER_CLEAR_EVENT.js @@ -0,0 +1,26 @@ +/** + * @author Richard Davey + * @copyright 2013-2024 Phaser Studio Inc. + * @license {@link https://opensource.org/licenses/MIT|MIT License} + */ + +/** + * The Pre-Render Clear Event. + * + * This event is dispatched by the Phaser Renderer. It happens at the start of the render step, before + * the WebGL gl.clear function has been called. This allows you to toggle the `config.clearBeforeRender` property + * as required, to have fine-grained control over when the canvas is cleared during rendering. + * + * Listen to it from within a Scene using: `this.renderer.events.on('prerenderclear', listener)`. + * + * It's very important to understand that this event is called _before_ the scissor and mask stacks are cleared. + * This means you should not use this event to modify the scissor or mask. Instead, use the `prerender` event for that. + * + * If using the Canvas Renderer, this event is dispatched before the canvas is cleared, but after the context globalAlpha + * and transform have been reset. + * + * @event Phaser.Renderer.Events#PRE_RENDER_CLEAR + * @type {string} + * @since 3.85.0 + */ +module.exports = 'prerenderclear'; diff --git a/src/renderer/webgl/WebGLRenderer.js b/src/renderer/webgl/WebGLRenderer.js index 059bc983d..0fdeca2f5 100644 --- a/src/renderer/webgl/WebGLRenderer.js +++ b/src/renderer/webgl/WebGLRenderer.js @@ -2643,6 +2643,7 @@ var WebGLRenderer = new Class({ * Clears the current vertex buffer and updates pipelines. * * @method Phaser.Renderer.WebGL.WebGLRenderer#preRender + * @fires Phaser.Renderer.Events#PRE_RENDER_CLEAR * @fires Phaser.Renderer.Events#PRE_RENDER * @since 3.0.0 */ @@ -2655,6 +2656,8 @@ var WebGLRenderer = new Class({ // Make sure we are bound to the main frame buffer gl.bindFramebuffer(gl.FRAMEBUFFER, null); + this.emit(Events.PRE_RENDER_CLEAR); + if (this.config.clearBeforeRender) { var clearColor = this.config.backgroundColor;