From 62f4cda14b505abb9f78916a064c1b60c7dd9867 Mon Sep 17 00:00:00 2001 From: eXecutable <3399019+eXecutable@users.noreply.github.com> Date: Thu, 19 Nov 2026 16:07:20 +0000 Subject: [PATCH] fix(renderer): restore canvas style guard for styleless canvases Platforms may provide a canvas without a style property. Reintroduce the guard around setting canvas.style dimensions and type the renderer canvas as RendererCanvas so style is optional. --- exports/platform.ts | 1 + src/core/platforms/Platform.ts | 19 ++++++++++++++---- src/core/platforms/web/lib/utils.ts | 4 +++- src/core/text-rendering/CanvasTextRenderer.ts | 7 ++++--- src/main-api/Inspector.ts | 5 +++-- src/main-api/Renderer.ts | 20 +++++++++++++------ 6 files changed, 40 insertions(+), 16 deletions(-) diff --git a/exports/platform.ts b/exports/platform.ts index 2f9005067..9d9d5a302 100644 --- a/exports/platform.ts +++ b/exports/platform.ts @@ -20,6 +20,7 @@ export { Platform, type PlatformSettings, + type RendererCanvas, } from '../src/core/platforms/Platform.js'; export { WebPlatform } from '../src/core/platforms/web/WebPlatform.js'; export { WebPlatformNext } from '../src/core/platforms/web/WebPlatformNext.js'; diff --git a/src/core/platforms/Platform.ts b/src/core/platforms/Platform.ts index 774384ff8..07495168f 100644 --- a/src/core/platforms/Platform.ts +++ b/src/core/platforms/Platform.ts @@ -21,6 +21,17 @@ import { type Stage } from '../Stage.js'; import type { ImageResponse } from '../textures/ImageTexture.js'; import type { GlContextWrapper } from './GlContextWrapper.js'; +/** + * Canvas element used for rendering. + * + * @remarks + * `style` is optional because some platforms provide a canvas that does not + * implement CSS styling. + */ +export type RendererCanvas = Omit & { + style?: CSSStyleDeclaration; +}; + /** * Settings for the Platform */ @@ -40,7 +51,7 @@ export interface PlatformSettings { /** * Optional provided canvas element to use for rendering. If not provided, the platform will create its own canvas element. */ - canvas?: HTMLCanvasElement | null; + canvas?: RendererCanvas | null; /** * Request Animation Frame Error Callback @@ -66,7 +77,7 @@ export abstract class Platform { Required>; public glw: GlContextWrapper | null = null; - public canvas: HTMLCanvasElement | null = null; + public canvas: RendererCanvas | null = null; constructor(settings: PlatformSettings = {}) { // Apply default settings @@ -82,9 +93,9 @@ export abstract class Platform { /** * Creates a new canvas element. - * @returns The created HTMLCanvasElement. + * @returns The created canvas element. */ - abstract createCanvas(): HTMLCanvasElement; + abstract createCanvas(): RendererCanvas; abstract createOffscreenCanvas(): OffscreenCanvas | null; diff --git a/src/core/platforms/web/lib/utils.ts b/src/core/platforms/web/lib/utils.ts index 78755a60b..a3c59881e 100644 --- a/src/core/platforms/web/lib/utils.ts +++ b/src/core/platforms/web/lib/utils.ts @@ -17,6 +17,8 @@ * limitations under the License. */ +import type { RendererCanvas } from '../../Platform.js'; + export const PROTOCOL_REGEX = /^(data|ftps?|https?):/; export function isBase64Image(src: string) { @@ -74,7 +76,7 @@ export function convertUrlToAbsolute(url: string): string { } export function createWebGLContext( - canvas: HTMLCanvasElement | OffscreenCanvas, + canvas: RendererCanvas | OffscreenCanvas, forceWebGL2 = false, ): WebGLRenderingContext { const config: WebGLContextAttributes = { diff --git a/src/core/text-rendering/CanvasTextRenderer.ts b/src/core/text-rendering/CanvasTextRenderer.ts index 843e0973d..2282a324b 100644 --- a/src/core/text-rendering/CanvasTextRenderer.ts +++ b/src/core/text-rendering/CanvasTextRenderer.ts @@ -19,6 +19,7 @@ import { assertTruthy } from '../../utils.js'; import type { Stage } from '../Stage.js'; +import type { RendererCanvas } from '../platforms/Platform.js'; import type { CanvasRenderInfo, FontHandler, @@ -46,7 +47,7 @@ const type = 'canvas' as const; const font: FontHandler = CanvasFontHandler; let stage: Stage | null = null; -let canvas: HTMLCanvasElement | OffscreenCanvas | null = null; +let canvas: RendererCanvas | OffscreenCanvas | null = null; let context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D | null = null; @@ -54,7 +55,7 @@ let context: let isOffscreen = false; // Separate canvas and context for text measurements -let measureCanvas: HTMLCanvasElement | OffscreenCanvas | null = null; +let measureCanvas: RendererCanvas | OffscreenCanvas | null = null; type MeasureContext = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D | null; let measureContext: MeasureContext = null; @@ -272,7 +273,7 @@ const drawAndCaptureText = ({ // transferToImageBitmap at the end. For HTMLCanvasElement we allocate a // fresh canvas per text node so it can be passed directly as the texture // source — no pixel readback or intermediate copy needed. - let drawCanvas: HTMLCanvasElement | OffscreenCanvas; + let drawCanvas: RendererCanvas | OffscreenCanvas; if (isOffscreen) { drawCanvas = canvas; } else { diff --git a/src/main-api/Inspector.ts b/src/main-api/Inspector.ts index 99d53e087..911d392e6 100644 --- a/src/main-api/Inspector.ts +++ b/src/main-api/Inspector.ts @@ -12,6 +12,7 @@ import type { import { isProductionEnvironment } from '../utils.js'; import { CoreTextNode, type CoreTextNodeProps } from '../core/CoreTextNode.js'; import type { Texture } from '../core/textures/Texture.js'; +import type { RendererCanvas } from '../core/platforms/Platform.js'; import { TextureType } from '../core/textures/Texture.js'; /** @@ -204,7 +205,7 @@ const knownProperties = new Set([ export class Inspector { private root: HTMLElement | null = null; - private canvas: HTMLCanvasElement | null = null; + private canvas: RendererCanvas | null = null; private mutationObserver: MutationObserver = new MutationObserver(() => {}); private resizeObserver: ResizeObserver = new ResizeObserver(() => {}); private height = 1080; @@ -258,7 +259,7 @@ export class Inspector { // Animation stats printing timer private animationStatsTimer: number | null = null; - constructor(canvas: HTMLCanvasElement, settings: RendererMainSettings) { + constructor(canvas: RendererCanvas, settings: RendererMainSettings) { if (isProductionEnvironment === true) return; if (!settings) { diff --git a/src/main-api/Renderer.ts b/src/main-api/Renderer.ts index 3d52ec6d4..2eedbdba8 100644 --- a/src/main-api/Renderer.ts +++ b/src/main-api/Renderer.ts @@ -39,7 +39,11 @@ import type { ShaderMap, } from '../core/CoreShaderManager.js'; import { WebPlatform } from '../core/platforms/web/WebPlatform.js'; -import { Platform, type PlatformSettings } from '../core/platforms/Platform.js'; +import { + Platform, + type PlatformSettings, + type RendererCanvas, +} from '../core/platforms/Platform.js'; /** * FPS Update Event Data @@ -549,7 +553,7 @@ export type RendererMainSettings = RendererRuntimeSettings & */ export class RendererMain extends EventEmitter { readonly root: INode; - readonly canvas: HTMLCanvasElement; + readonly canvas: RendererCanvas; readonly stage: Stage; private inspector: Inspector | null = null; @@ -628,8 +632,10 @@ export class RendererMain extends EventEmitter { this.canvas.width = deviceLogicalWidth * devicePhysicalPixelRatio; this.canvas.height = deviceLogicalHeight * devicePhysicalPixelRatio; - this.canvas.style.width = `${deviceLogicalWidth}px`; - this.canvas.style.height = `${deviceLogicalHeight}px`; + if (this.canvas.style) { + this.canvas.style.width = `${deviceLogicalWidth}px`; + this.canvas.style.height = `${deviceLogicalHeight}px`; + } // Initialize the stage this.stage = new Stage({ @@ -1054,8 +1060,10 @@ export class RendererMain extends EventEmitter { this.canvas.width = deviceLogicalWidth * devicePhysicalPixelRatio; this.canvas.height = deviceLogicalHeight * devicePhysicalPixelRatio; - this.canvas.style.width = `${deviceLogicalWidth}px`; - this.canvas.style.height = `${deviceLogicalHeight}px`; + if (this.canvas.style) { + this.canvas.style.width = `${deviceLogicalWidth}px`; + this.canvas.style.height = `${deviceLogicalHeight}px`; + } this.stage.renderer.updateViewport();