@motion-core/motion-gpu
Version:
Framework-agnostic WebGPU runtime for fullscreen WGSL shaders with explicit Svelte, React, and Vue adapter entrypoints.
71 lines (70 loc) • 1.94 kB
JavaScript
//#region src/lib/core/pointer.ts
/**
* Returns a monotonic timestamp in seconds.
*/
function getPointerNowSeconds() {
if (typeof performance !== "undefined" && typeof performance.now === "function") return performance.now() / 1e3;
return Date.now() / 1e3;
}
/**
* Creates the initial pointer state snapshot.
*/
function createInitialPointerState() {
return {
px: [0, 0],
uv: [0, 0],
ndc: [-1, -1],
inside: false,
pressed: false,
dragging: false,
pointerType: null,
pointerId: null,
button: null,
buttons: 0,
time: getPointerNowSeconds(),
downPx: null,
downUv: null,
deltaPx: [0, 0],
deltaUv: [0, 0],
velocityPx: [0, 0],
velocityUv: [0, 0]
};
}
/**
* Converts client coordinates to canvas-relative pointer coordinates.
*/
function getPointerCoordinates(clientX, clientY, rect) {
const width = Math.max(rect.width, 1);
const height = Math.max(rect.height, 1);
const nx = (clientX - rect.left) / width;
const ny = (clientY - rect.top) / height;
const pxX = clientX - rect.left;
const pxY = clientY - rect.top;
const uvX = nx;
const uvY = 1 - ny;
return {
px: [pxX, pxY],
uv: [uvX, uvY],
ndc: [nx * 2 - 1, uvY * 2 - 1],
inside: nx >= 0 && nx <= 1 && ny >= 0 && ny <= 1
};
}
/**
* Resolves frame wake-up strategy for pointer-driven updates.
*/
function resolvePointerFrameRequestMode(mode, renderMode) {
if (mode !== "auto") return mode;
if (renderMode === "manual") return "advance";
if (renderMode === "on-demand") return "invalidate";
return "none";
}
/**
* Normalizes unknown pointer kind values to the public `PointerKind`.
*/
function normalizePointerKind(pointerType) {
if (pointerType === "mouse" || pointerType === "pen" || pointerType === "touch") return pointerType;
return "mouse";
}
//#endregion
export { createInitialPointerState, getPointerCoordinates, getPointerNowSeconds, normalizePointerKind, resolvePointerFrameRequestMode };
//# sourceMappingURL=pointer.js.map