website_next: part 4

This commit is contained in:
nym21
2026-07-06 13:17:11 +02:00
parent 3f9edb211e
commit 3f2a48f248
16 changed files with 1175 additions and 929 deletions
+54
View File
@@ -0,0 +1,54 @@
import { VIEWBOX_WIDTH } from "./viewbox.js";
/**
* @typedef {Object} ChartPointerPosition
* @property {number} x
* @property {number} y
*/
/**
* @param {SVGSVGElement} svg
* @param {() => number | undefined} getHeight
* @param {(position: ChartPointerPosition) => void} onMove
*/
export function createChartPointer(svg, getHeight, onMove) {
let rect = svg.getBoundingClientRect();
let clientX = 0;
let clientY = 0;
let frame = 0;
function measure() {
rect = svg.getBoundingClientRect();
return rect;
}
function cancel() {
if (frame) cancelAnimationFrame(frame);
frame = 0;
}
/** @param {PointerEvent} event */
function update(event) {
clientX = event.clientX;
clientY = event.clientY;
if (frame) return;
frame = requestAnimationFrame(() => {
frame = 0;
const height = getHeight();
if (height === undefined) return;
onMove({
x: ((clientX - rect.left) / rect.width) * VIEWBOX_WIDTH,
y: ((clientY - rect.top) / rect.height) * height,
});
});
}
return /** @type {const} */ ({
cancel,
measure,
update,
});
}