Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/elements/visual/c2dBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class C2DBase extends CustomHTMLElement {
return this.#eventProxy;
}

remove() {
this.canvas.queueRemoval(this);
}

/**
* Scales a vector by the device's pixel ratio.
*/
Expand Down
20 changes: 20 additions & 0 deletions src/elements/visual/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class Canvas2DCanvasElement extends c2dStandaloneChildren(C2DBase) {
#keyboardTracker = new KeyboardTracker();
#lastFrameTime = -1;
#mouseTracker: MouseTracker;
#removalQueue = new Set<HTMLElement>();
#renderEvents = new Set<keyof HTMLElementEventMap>();
#renderQueued = false;
#setAlpha: number | null = null;
Expand Down Expand Up @@ -250,6 +251,12 @@ export class Canvas2DCanvasElement extends c2dStandaloneChildren(C2DBase) {
return this.#mouseTracker;
}

queueRemoval(child: HTMLElement) {
this.#removalQueue.add(child);

this.queueRender();
}

queueRender() {
if (this.#renderQueued || this.#waitFor.size) return;

Expand Down Expand Up @@ -323,6 +330,19 @@ export class Canvas2DCanvasElement extends c2dStandaloneChildren(C2DBase) {
return;
}

while (this.#removalQueue.size) {
const next = this.#removalQueue.values().next();

if (next.value === undefined)
throw new Error("Found undefined value in canvas's removal queue.");

const child = next.value;

this.removeChild(child);

this.#removalQueue.delete(child);
}

const context = this.#context;

context.save();
Expand Down
5 changes: 5 additions & 0 deletions tests/c2d-bezier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Canvas2DBezier,
Canvas2DShapeBezier,
} from "../dist/types/elements/visual/bezier";
import { testRemoval } from "./testRemoval";

function testControlPoints(
setup: ElementTestSetup<{ controlA: Vector2D; controlB: Vector2D }>
Expand Down Expand Up @@ -169,6 +170,8 @@ describe("c2d-bezier", () => {
testFill(setup, "bezierCurveTo");

testShadow(setup, "bezierCurveTo");

testRemoval(setup);
});

describe("c2d-shape-bezier", () => {
Expand All @@ -189,4 +192,6 @@ describe("c2d-shape-bezier", () => {
testControlPoints(setup);

testTransform(setup, 1);

testRemoval(setup);
});
5 changes: 5 additions & 0 deletions tests/c2d-ellipse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Canvas2DEllipse,
Canvas2DShapeEllipse,
} from "../dist/types/elements/visual/ellipse";
import { testRemoval } from "./testRemoval";

function testStartEndAngles(
setup: ElementTestSetup<{ startAngle: Angle; endAngle: Angle }>
Expand Down Expand Up @@ -91,6 +92,8 @@ describe("c2d-ellipse", () => {
testFill(setup, "ellipse");

testShadow(setup, "ellipse");

testRemoval(setup);
});

describe("c2d-shape-ellipse", () => {
Expand All @@ -111,4 +114,6 @@ describe("c2d-shape-ellipse", () => {
testTransform(setup, 1);

testRectangleBounds(setup, "ellipse", 0.5);

testRemoval(setup);
});
3 changes: 3 additions & 0 deletions tests/c2d-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { testOffset } from "./testOffset";
import { testShadow } from "./testShadow";
import { ElementTestSetup } from "./types";
import { Canvas2DImage } from "../dist/types/elements/visual/image";
import { testRemoval } from "./testRemoval";

describe("c2d-image", () => {
mockMatchMedia();
Expand Down Expand Up @@ -91,4 +92,6 @@ describe("c2d-image", () => {
testRectangleBounds(setup, "drawImage", 1, 3, 4);

testShadow(setup, "drawImage");

testRemoval(setup);
});
5 changes: 5 additions & 0 deletions tests/c2d-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Canvas2DLine,
Canvas2DShapeLine,
} from "../dist/types/elements/visual/line";
import { testRemoval } from "./testRemoval";

function testTo(setup: ElementTestSetup<{ to: Vector2D }>) {
describe("to", () => {
Expand Down Expand Up @@ -108,6 +109,8 @@ describe("c2d-line", () => {
testStroke(setup, "lineTo");

testShadow(setup, "lineTo");

testRemoval(setup);
});

describe("c2d-shape-line", () => {
Expand All @@ -126,4 +129,6 @@ describe("c2d-shape-line", () => {
testTo(setup);

testTransform(setup, 1);

testRemoval(setup);
});
5 changes: 5 additions & 0 deletions tests/c2d-rectangle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Canvas2DRectangle,
Canvas2DShapeRectangle,
} from "../dist/types/elements/visual/rectangle";
import { testRemoval } from "./testRemoval";

function testBorderRadius(
setup: ElementTestSetup<{
Expand Down Expand Up @@ -209,6 +210,8 @@ describe("c2d-rectangle", () => {
testFill(setup, "rect");

testShadow(setup, "rect");

testRemoval(setup);
});

describe("c2d-shape-rectangle", () => {
Expand All @@ -233,4 +236,6 @@ describe("c2d-shape-rectangle", () => {
testBorderRadius(setup);

testRectangleBounds(setup, "rect");

testRemoval(setup);
});
3 changes: 3 additions & 0 deletions tests/c2d-shape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { waitFor } from "@testing-library/dom";
import { testShadow } from "./testShadow";
import { ElementTestSetup } from "./types";
import { Canvas2DShape } from "../dist/types/elements/visual/shape";
import { testRemoval } from "./testRemoval";

describe("c2d-shape", () => {
setupJestCanvasMock();
Expand Down Expand Up @@ -73,4 +74,6 @@ describe("c2d-shape", () => {
testStroke(setup, "moveTo");

testShadow(setup, "moveTo");

testRemoval(setup);
});
3 changes: 3 additions & 0 deletions tests/c2d-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { waitFor } from "@testing-library/dom";
import { testShadow } from "./testShadow";
import { ElementTestSetup } from "./types";
import { Canvas2DText } from "../dist/types/elements/visual/text";
import { testRemoval } from "./testRemoval";

describe("c2d-text", () => {
setupJestCanvasMock();
Expand Down Expand Up @@ -130,4 +131,6 @@ describe("c2d-text", () => {
testFill(setup, "fillText", false);

testShadow(setup, "fillText");

testRemoval(setup);
});
3 changes: 3 additions & 0 deletions tests/c2d-video.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { waitFor } from "@testing-library/dom";
import { testShadow } from "./testShadow";
import { ElementTestSetup } from "./types";
import { Canvas2DVideo } from "../dist/types/elements/visual/video";
import { testRemoval } from "./testRemoval";

describe("c2d-video", () => {
mockMatchMedia();
Expand Down Expand Up @@ -145,4 +146,6 @@ describe("c2d-video", () => {
});

testShadow(setup, "drawImage");

testRemoval(setup);
});
29 changes: 29 additions & 0 deletions tests/testRemoval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { jest } from "@jest/globals";
import { waitFor } from "@testing-library/dom";
import { ElementTestSetup } from "./types";

export function testRemoval(setup: ElementTestSetup<{}>) {
test("Queues canvas render when removed", async () => {
const { canvas, element, teardown } = setup();

const queueRemoval = jest.spyOn(canvas, "queueRemoval");

const queueRender = jest.spyOn(canvas, "queueRender");

expect(queueRemoval).not.toHaveBeenCalled();

expect(queueRender).not.toHaveBeenCalled();

expect(canvas.contains(element)).toBe(true);

element.remove();

expect(queueRemoval).toHaveBeenCalled();

expect(queueRender).toHaveBeenCalled();

await waitFor(() => !canvas.contains(element));

teardown();
});
}