spline: convert screen to canvas coordinates

This commit is contained in:
2026-08-15 13:44:15 +02:00
parent f2222079c1
commit 3a58f98acf
+5 -2
View File
@@ -65,13 +65,16 @@ function init() {
canvas.onmousedown = (evt: MouseEvent) => { canvas.onmousedown = (evt: MouseEvent) => {
const { clientX, clientY } = evt; 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) { 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); selection = points.indexOf(p);
} }
} }
if (selection === undefined) { if (selection === undefined) {
points.push({ x: clientX, y: clientY }); points.push({ x: click.x, y: click.y });
// redraw // redraw
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
draw(ctx, points); draw(ctx, points);