Skip to content

Commit cce31e0

Browse files
obiotclaude
andcommitted
Address Copilot: cache shared gradient render, doc scratch array, fix test name
- Gradient: skip re-render when same gradient/coords already on shared target - dashPath: document return value as internal scratch array - Fix test description for all-zero pattern behavior Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 88dba46 commit cce31e0

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

packages/melonjs/src/video/gradient.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import CanvasRenderTarget from "./rendertarget/canvasrendertarget.js";
1111
* @ignore
1212
*/
1313
let sharedRenderTarget = null;
14+
let sharedLastGradient = null;
15+
let sharedLastX = NaN;
16+
let sharedLastY = NaN;
1417

1518
/**
1619
* A Gradient object representing a linear or radial gradient fill.
@@ -144,6 +147,20 @@ export class Gradient {
144147
const tw = nextPowerOfTwo(Math.max(1, Math.ceil(width)));
145148
const th = nextPowerOfTwo(Math.max(1, Math.ceil(height)));
146149

150+
// skip if this gradient already rendered to the shared target at these coords
151+
if (
152+
sharedRenderTarget &&
153+
sharedLastGradient === this &&
154+
!this._dirty &&
155+
sharedLastX === x &&
156+
sharedLastY === y &&
157+
sharedRenderTarget.width === tw &&
158+
sharedRenderTarget.height === th
159+
) {
160+
this._renderTarget = sharedRenderTarget;
161+
return this._renderTarget.canvas;
162+
}
163+
147164
// reuse the shared render target to avoid GPU memory leaks
148165
if (!sharedRenderTarget) {
149166
sharedRenderTarget = new CanvasRenderTarget(tw, th);
@@ -189,6 +206,9 @@ export class Gradient {
189206
ctx.fillRect(0, 0, tw, th);
190207

191208
this._dirty = false;
209+
sharedLastGradient = this;
210+
sharedLastX = x;
211+
sharedLastY = y;
192212
this._renderTarget.invalidate(renderer);
193213
return this._renderTarget.canvas;
194214
}

packages/melonjs/src/video/utils/dash.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Carries the dash state across segment boundaries for continuous dashing.
44
* @param {Array<{x: number, y: number}>} pts - pairs of start/end points
55
* @param {number[]} pattern - dash pattern [on, off, on, off, ...]
6-
* @returns {Array<{x: number, y: number}>} dashed segment pairs
6+
* @returns {Array<{x: number, y: number}>} dashed segment pairs (internal scratch array — consume immediately, do not store)
77
*/
88
// pre-allocated scratch array to avoid per-frame allocations
99
const _dashPathResult = [];

packages/melonjs/tests/dash.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe("dash utilities", () => {
8181
expect(multiSeg.length).toEqual(singleSeg.length);
8282
});
8383

84-
it("should return copy for all-zero pattern", () => {
84+
it("should return original points for all-zero pattern", () => {
8585
const pts = [
8686
{ x: 0, y: 0 },
8787
{ x: 100, y: 0 },

0 commit comments

Comments
 (0)