3 Commits

Author SHA1 Message Date
95afbf9bd3 drawWithAlpha 2023-10-25 17:40:39 -06:00
c58861bc93 deferred drawing 2023-10-24 01:11:15 -06:00
7d6b54825d oops 2023-10-24 00:55:01 -06:00

View File

@@ -89,6 +89,7 @@ export class Doodler {
// } // }
for (const [i, l] of (this.layers || []).entries()) { for (const [i, l] of (this.layers || []).entries()) {
l(this.ctx, i); l(this.ctx, i);
this.drawDeferred();
} }
this.drawUI(); this.drawUI();
} }
@@ -198,6 +199,13 @@ export class Doodler {
this.ctx.restore(); this.ctx.restore();
} }
drawWithAlpha(alpha: number, cb: () => void) {
this.ctx.save();
this.ctx.globalAlpha = Math.min(Math.max(alpha, 0), 1);
cb();
this.ctx.restore();
}
drawImage(img: HTMLImageElement, at: Vector): void; drawImage(img: HTMLImageElement, at: Vector): void;
drawImage(img: HTMLImageElement, at: Vector, w: number, h: number): void; drawImage(img: HTMLImageElement, at: Vector, w: number, h: number): void;
drawImage(img: HTMLImageElement, at: Vector, w?: number, h?: number) { drawImage(img: HTMLImageElement, at: Vector, w?: number, h?: number) {
@@ -227,12 +235,27 @@ export class Doodler {
); );
} }
private deferredDrawings: (() => void)[] = [];
deferDrawing(cb: () => void) {
this.deferredDrawings.push(cb);
}
drawDeferred() {
while (this.deferredDrawings.length) {
this.deferredDrawings.pop()?.();
}
}
setStyle(style?: IStyle) { setStyle(style?: IStyle) {
const ctx = this.ctx; const ctx = this.ctx;
ctx.fillStyle = style?.color || style?.fillColor || "black"; ctx.fillStyle = style?.color || style?.fillColor || "black";
ctx.strokeStyle = style?.color || style?.strokeColor || "black"; ctx.strokeStyle = style?.color || style?.strokeColor || "black";
ctx.lineWidth = style?.weight || 1; ctx.lineWidth = style?.weight || 1;
ctx.textAlign = style?.textAlign || ctx.textAlign;
ctx.textBaseline = style?.textBaseline || ctx.textBaseline;
} }
fillText(text: string, pos: Vector, maxWidth: number, style?: IStyle) { fillText(text: string, pos: Vector, maxWidth: number, style?: IStyle) {