@vgpu/renderAdvanced3 symbolsView source ↗

Symbols in this topic

canvasMouseTracker

Listens for pointer movement over a canvas and exposes the latest coordinates. Use it to feed shaders with mouse positions without wiring global event listeners.

Import

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

Signature

TypeScript
1
export function canvasMouseTracker(spec: CanvasMouseTrackerSpec): CanvasMouseTracker;

Parameters

ParamTypeRequiredDefaultNotes
specCanvasMouseTrackerSpecConfiguration object describing the canvas and how to normalize coordinates.
spec.canvasHTMLCanvasElementTarget element that receives pointermove events.
spec.normalizebooleanfalseWhen true, position is expressed in [0, 1] relative coordinates; otherwise uses raw canvas pixel coordinates.
spec.flipYbooleanfalseReflects the Y axis (top → bottom) while preserving the chosen unit (normalized or pixel).

Returns: CanvasMouseTracker — exposes a live position tuple ([x, y]) and a dispose() method that removes the internal event listener.

Examples

TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { canvasMouseTracker } from "@vgpu/render/utils";
 
const canvas = document.createElement("canvas");
const effect = { set(values: { readonly mouse: readonly [number, number] }): void { void values; } };
const mouse = canvasMouseTracker({ canvas, normalize: true, flipY: true });
 
function frame() {
  const [u, v] = mouse.position; // normalized UV with origin at bottom-left
  effect.set({ mouse: [u, v] });
  requestAnimationFrame(frame);
}
 
frame();
// Later:
mouse.dispose();

Notes

  • The initial position is [0, 0] until the first pointer event fires; guard against that if your shader requires seeded values.
  • The tracker prefers PointerEvent.offsetX/Y when available, falling back to clientX/Y minus the canvas bounds, so it works with both pointer-lock and classic pointer events.
  • Call dispose() before removing the canvas from the DOM to avoid dangling listeners.
  • See also: canvasResolution, frameClock

CanvasMouseTrackerSpec

Configuration object accepted by canvasMouseTracker.

Fields

FieldTypeRequiredDefaultNotes
canvasHTMLCanvasElementCanvas to observe.
normalizebooleanfalseEnables normalized [0, 1] output in both axes; otherwise uses raw pixel units.
flipYbooleanfalseMirrors the Y coordinate so normalized output matches WebGPU clip space (0 at bottom).

CanvasMouseTracker

Handle returned by canvasMouseTracker.

Fields

FieldTypeRequiredDefaultNotes
positionreadonly [number, number]Latest [x, y] coordinates (normalized or pixels depending on spec). Always returns a frozen tuple.
dispose() => voidRemoves the internal pointermove listener; idempotent.