Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exports/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
19 changes: 15 additions & 4 deletions src/core/platforms/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLCanvasElement, 'style'> & {
style?: CSSStyleDeclaration;
};

/**
* Settings for the Platform
*/
Expand All @@ -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
Expand All @@ -66,7 +77,7 @@ export abstract class Platform {
Required<Pick<PlatformSettings, 'numImageWorkers' | 'forceWebGL2'>>;

public glw: GlContextWrapper | null = null;
public canvas: HTMLCanvasElement | null = null;
public canvas: RendererCanvas | null = null;

constructor(settings: PlatformSettings = {}) {
// Apply default settings
Expand All @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion src/core/platforms/web/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 = {
Expand Down
7 changes: 4 additions & 3 deletions src/core/text-rendering/CanvasTextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -46,15 +47,15 @@ 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;

// Whether the drawing canvas is an OffscreenCanvas (supports transferToImageBitmap)
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;
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/main-api/Inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -204,7 +205,7 @@ const knownProperties = new Set<string>([

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;
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 14 additions & 6 deletions src/main-api/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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();

Expand Down