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
| Param | Type | Required | Default | Notes |
|---|---|---|---|---|
| canvas | HTMLCanvasElement | ✔ | — | Target 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.observe | boolean | ✖ | false | When 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
observeisfalse,width/heightstay at the values captured whencanvasResolution(...)was called. Create a newCanvasResolutionor passobserve: trueif later canvas attribute changes must be reflected. - The helper calls
ResizeObserver.observe(canvas)only whenobserve: true; calldispose()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