@vgpu/renderAdvanced2 symbolsView source ↗

Symbols in this topic

canvasResolution

Reads the current drawing buffer size of a canvas and optionally watches for resize changes. Use it when you need [width, height] uniforms without wiring observers manually.

Import

TypeScript
1
import { canvasResolution } from "@vgpu/render/utils";

Signature

TypeScript
1
2
3
4
export function canvasResolution(
  canvas: HTMLCanvasElement,
  opts?: { readonly observe?: boolean },
): CanvasResolution;

Parameters

ParamTypeRequiredDefaultNotes
canvasHTMLCanvasElementTarget element; width/height are read from its drawing buffer, not CSS pixels.
opts{ observe?: boolean }{}Optional behavior flags. Omitted options behave like { observe: false }.
opts.observebooleanfalseWhen true, attaches a ResizeObserver that keeps the cached width/height in sync.

Returns: CanvasResolution — exposes width, height, and dispose(). Without observe: true, width and height are the initial drawing-buffer snapshot; with observe: true, they update when the ResizeObserver callback runs.

Examples

TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { canvasResolution } from "@vgpu/render/utils";
 
const canvas = document.createElement("canvas");
const effect = { set(values: { readonly resolution: readonly [number, number] }): void { void values; } };
const resolution = canvasResolution(canvas, { observe: true });
 
function frame() {
  effect.set({ resolution: [resolution.width, resolution.height] });
  requestAnimationFrame(frame);
}
 
frame();
// Later:
resolution.dispose();

Notes

  • When observe is false, width/height stay at the values captured when canvasResolution(...) was called. Create a new CanvasResolution or pass observe: true if later canvas attribute changes must be reflected.
  • The helper calls ResizeObserver.observe(canvas) only when observe: true; call dispose() before removing the canvas to disconnect the observer.
  • The returned values reflect the drawing buffer size (canvas.width/height), which already accounts for DPR scaling if you manage it manually.
  • See also: canvasMouseTracker, frameClock