Skip to content

Commit ca2f100

Browse files
committed
new backgroundColor property allowing to change the color when clearing the background between frames
1 parent 932bf35 commit ca2f100

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

packages/melonjs/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@
532532
- Color: added a `setHSL(h, s, l)` and `setHSV(h, s, v)` method to the Color class
533533
- Tiled: add support for the new `class` property (note: melonJS will still set the deprecated `type` one for backward compatibility)
534534
- Renderer: Canvas rendering mode can now be forced by adding `[#/&]canvas` to the URL (similarly with WebGL1/2 already)
535+
- Renderer: new `backgroundColor` property allowing to change the color when clearing the background between frames
535536
- Vector: new `moveTowards()` method for `[Observable]Vector2d/3d` objects (limited to x and y axis for 3d vectors)
536537

537538
### Changed

packages/melonjs/src/video/canvas/canvas_renderer.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,8 @@ export default class CanvasRenderer extends Renderer {
129129
* prepare the framebuffer for drawing a new frame
130130
*/
131131
clear() {
132-
if (this.settings.transparent === false) {
133-
const canvas = this.getCanvas();
134-
const context = this.getContext();
135-
context.clearRect(0, 0, canvas.width, canvas.height);
132+
if (this.backgroundColor.alpha > 0) {
133+
this.clearColor(this.backgroundColor);
136134
}
137135
}
138136

packages/melonjs/src/video/renderer.js

+13
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ export default class Renderer {
8484
*/
8585
this.type = "Generic";
8686

87+
/**
88+
* The background color used to clear the main framebuffer.
89+
* Note: alpha value will be set based on the transparent property of the renderer settings.
90+
* @default black
91+
* @type {Color}
92+
*/
93+
this.backgroundColor = new Color(
94+
0,
95+
0,
96+
0,
97+
this.settings.transparent ? 0.0 : 1.0,
98+
);
99+
87100
/**
88101
* @ignore
89102
*/

packages/melonjs/src/video/webgl/webgl_renderer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ export default class WebGLRenderer extends Renderer {
475475
*/
476476
clear() {
477477
const gl = this.gl;
478-
gl.clearColor(0, 0, 0, this.settings.transparent ? 0.0 : 1.0);
478+
const clearColor = this.backgroundColor.toArray();
479+
gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
479480
this.lineWidth = 1;
480481
if (this.depthTest === "z-buffer") {
481482
gl.clear(

0 commit comments

Comments
 (0)