From a38f2166488030be5c4dadeabc568f0e233b9a83 Mon Sep 17 00:00:00 2001 From: wouterlucas Date: Wed, 11 Dec 2024 19:21:21 +0100 Subject: [PATCH] feat: add optional external canvas for rendering --- src/main-api/Renderer.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main-api/Renderer.ts b/src/main-api/Renderer.ts index 0e557917..134bf56c 100644 --- a/src/main-api/Renderer.ts +++ b/src/main-api/Renderer.ts @@ -267,6 +267,15 @@ export interface RendererMainSettings { * @defaultValue `true` */ strictBounds?: boolean; + + /** + * Canvas object to use for rendering + * + * @remarks + * This is used to render the scene graph. If not provided, a new canvas + * element will be created and appended to the target element. + */ + canvas?: HTMLCanvasElement; } /** @@ -358,6 +367,7 @@ export class RendererMain extends EventEmitter { quadBufferSize: settings.quadBufferSize ?? 4 * 1024 * 1024, fontEngines: settings.fontEngines, strictBounds: settings.strictBounds ?? true, + canvas: settings.canvas || document.createElement('canvas'), }; this.settings = resolvedSettings; @@ -367,12 +377,12 @@ export class RendererMain extends EventEmitter { deviceLogicalPixelRatio, devicePhysicalPixelRatio, inspector, + canvas, } = resolvedSettings; const deviceLogicalWidth = appWidth * deviceLogicalPixelRatio; const deviceLogicalHeight = appHeight * deviceLogicalPixelRatio; - const canvas = document.createElement('canvas'); this.canvas = canvas; canvas.width = deviceLogicalWidth * devicePhysicalPixelRatio; canvas.height = deviceLogicalHeight * devicePhysicalPixelRatio;