Skip to content

Commit

Permalink
Add fallback for when OffscreenCanvas does not exist (iOS 15)
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Feb 22, 2024
1 parent ae2ea6f commit 9ddc5da
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ export interface RootParams {
loadImage?: (src: string) => Promise<IImage>;
}

function createOffscreenCanvas(doc: Document) {
return (w: number, h: number) => {
const c = doc.createElement('canvas');
c.width = w;
c.height = h;
return c;
};
}

export class Root extends TelaEventTarget {
ctx: ICanvasRenderingContext2D;
dirty: boolean;
Expand All @@ -34,9 +43,14 @@ export class Root extends TelaEventTarget {
this.render = this.render.bind(this);
this.renderCount = 0;
this.renderQueued = false;
this.Canvas = opts.Canvas || OffscreenCanvas;
this.DOMMatrix = opts.DOMMatrix || DOMMatrix;
this.Path2D = opts.Path2D || Path2D;
this.Canvas =
opts.Canvas ||
globalThis.OffscreenCanvas ||
createOffscreenCanvas(
(ctx.canvas as HTMLCanvasElement).ownerDocument,
);
this.DOMMatrix = opts.DOMMatrix || globalThis.DOMMatrix;
this.Path2D = opts.Path2D || globalThis.Path2D;
if (opts.loadImage) {
this.loadImage = opts.loadImage;
}
Expand Down

0 comments on commit 9ddc5da

Please sign in to comment.