From 3a58f98acffff81e359bd902130115934a6cf2f5 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Sat, 15 Aug 2026 13:44:15 +0200 Subject: [PATCH] spline: convert screen to canvas coordinates --- spline.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spline.ts b/spline.ts index ea568b8..64d552c 100644 --- a/spline.ts +++ b/spline.ts @@ -65,13 +65,16 @@ function init() { canvas.onmousedown = (evt: MouseEvent) => { const { clientX, clientY } = evt; + const rect = ctx.canvas.getBoundingClientRect(); + const click = { x: clientX - rect.left, y: clientY - rect.top }; // screen to canvas coordinates + for (const p of points) { - if (Math.abs(p.x - clientX) < 10 && Math.abs(p.y - clientY) < 10) { + if (Math.abs(p.x - click.x) < 10 && Math.abs(p.y - click.y) < 10) { selection = points.indexOf(p); } } if (selection === undefined) { - points.push({ x: clientX, y: clientY }); + points.push({ x: click.x, y: click.y }); // redraw ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); draw(ctx, points);