Skip to content

Commit ab49d0e

Browse files
committed
Don't allow mounting with same canvas
1 parent d501105 commit ab49d0e

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"stage-js": patch
3+
---
4+
5+
Don't allow mounting with same canvas

src/core/root.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export type Viewbox = {
5252
mode?: FitMode;
5353
};
5454

55+
let DEFAULT_CANVAS_MOUNTED = false;
56+
5557
export class Root extends Component {
5658
canvas: HTMLCanvasElement | null = null;
5759
dom: HTMLCanvasElement | null = null;
@@ -99,6 +101,13 @@ export class Root extends Component {
99101
}
100102

101103
if (!this.canvas) {
104+
if (DEFAULT_CANVAS_MOUNTED) {
105+
throw new Error(
106+
"Default canvas element is already mounted. Please provide a canvas element or an id of a canvas element to mount.",
107+
);
108+
}
109+
DEFAULT_CANVAS_MOUNTED = true;
110+
102111
console.debug && console.debug("Creating canvas element...");
103112
this.canvas = document.createElement("canvas");
104113
Object.assign(this.canvas.style, {
@@ -116,6 +125,11 @@ export class Root extends Component {
116125
body.insertBefore(this.canvas, body.firstChild);
117126
}
118127

128+
if (this.canvas["__STAGE_MOUNTED"]) {
129+
console.error("Canvas element is already mounted: ", this.canvas);
130+
}
131+
this.canvas["__STAGE_MOUNTED"] = true;
132+
119133
this.dom = this.canvas;
120134

121135
this.context = this.canvas.getContext("2d");
@@ -180,7 +194,8 @@ export class Root extends Component {
180194
this.canvas.width = this.drawingWidth;
181195
this.canvas.height = this.drawingHeight;
182196

183-
console.debug && console.debug(
197+
console.debug &&
198+
console.debug(
184199
"Resize: [" +
185200
this.drawingWidth +
186201
", " +
@@ -221,7 +236,7 @@ export class Root extends Component {
221236

222237
if (this.drawingWidth > 0 && this.drawingHeight > 0) {
223238
this.context.setTransform(1, 0, 0, 1, 0, 0);
224-
this.context.clearRect(0, 0, this.drawingWidth, this.drawingHeight);
239+
this.context.clearRect(0, 0, this.drawingWidth, this.drawingHeight);
225240
if (this.debugDrawAxis > 0) {
226241
this.renderDebug(this.context);
227242
}
@@ -254,7 +269,7 @@ export class Root extends Component {
254269
context.lineTo(0, size);
255270
context.lineTo(+0.2 * size, 0.8 * size);
256271
context.lineTo(0, 0.8 * size);
257-
context.strokeStyle = 'rgba(93, 173, 226)';
272+
context.strokeStyle = "rgba(93, 173, 226)";
258273
context.lineJoin = "round";
259274
context.lineCap = "round";
260275
context.lineWidth = lineWidth;
@@ -267,7 +282,7 @@ export class Root extends Component {
267282
context.lineTo(size, 0);
268283
context.lineTo(0.8 * size, +0.2 * size);
269284
context.lineTo(0.8 * size, 0);
270-
context.strokeStyle = 'rgba(236, 112, 99)';
285+
context.strokeStyle = "rgba(236, 112, 99)";
271286
context.lineJoin = "round";
272287
context.lineCap = "round";
273288
context.lineWidth = lineWidth;

0 commit comments

Comments
 (0)