Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

1.5 KB
Raw Blame
// Shared hooks between the DOM (canvas / img elements) and the graphics modules, so the modules stay
// independent: canvases never import a context implementation, they look the factories up here.

/** What a canvas element exposes to the context modules. */
export interface CanvasHost {
  width: number;
  height: number;
  /** set by a context module when a context is created: tells the UI engine what to draw */
  _setSurface(kind: "2d" | "webgpu", id: number): void;
  /** the current context, if any */
  _context: any;
  /** context modules register a resize callback (canvas.width/height changed) */
  _onResize: ((w: number, h: number) => void) | null;
}

/** What an img element exposes to the image loader. */
export interface ImageHost {
  /** image id in the native image registry (0 = none) */
  _imageId: number;
  naturalWidth: number;
  naturalHeight: number;
  complete: boolean;
  /** called by the loader when the image is ready / failed */
  _loaded(id: number, width: number, height: number): void;
  _failed(): void;
}

export const surfaces = {
  /** "2d" → Canvas 2D factory, "webgpu" → WebGPU canvas context factory */
  contextFactories: new Map<string, (canvas: CanvasHost, attrs: any) => any>(),
  /** canvas.toDataURL / toBlob for a canvas with a context (or blank) */
  toDataURL: null as null | ((canvas: CanvasHost, type?: string, quality?: number) => string),
  /** start loading `src` into an image element */
  loadImage: null as null | ((img: ImageHost, src: string) => void),
};