handle touch events
This commit is contained in:
parent
5152ea259e
commit
a87bfef4f6
34
bezier-2.ts
34
bezier-2.ts
@ -35,7 +35,17 @@ function init() {
|
||||
const bezier = cubicBezier(...points);
|
||||
drawCurve(ctx, bezier);
|
||||
|
||||
canvas.onmousedown = (evt) => {
|
||||
canvas.ontouchstart = (evt: TouchEvent) => {
|
||||
console.assert(evt.touches.length === 1, "Multiple touch points are not supported");
|
||||
const { clientX, clientY } = evt.touches[0];
|
||||
for (const p of points) {
|
||||
if (Math.abs(p.x - clientX) < 10 && Math.abs(p.y - clientY) < 10) {
|
||||
selection = points.indexOf(p);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
canvas.onmousedown = (evt: MouseEvent) => {
|
||||
const { clientX, clientY } = evt;
|
||||
for (const p of points) {
|
||||
if (Math.abs(p.x - clientX) < 10 && Math.abs(p.y - clientY) < 10) {
|
||||
@ -44,7 +54,22 @@ function init() {
|
||||
}
|
||||
};
|
||||
|
||||
canvas.onmousemove = (evt) => {
|
||||
canvas.ontouchmove = (evt: TouchEvent) => {
|
||||
console.assert(evt.touches.length === 1, "Multiple touch points are not supported");
|
||||
const { clientX, clientY } = evt.touches[0];
|
||||
if (selection !== undefined) {
|
||||
points[selection].x = clientX;
|
||||
points[selection].y = clientY;
|
||||
// redraw
|
||||
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
||||
const bezier = cubicBezier(...points);
|
||||
drawCurve(ctx, bezier);
|
||||
drawPoints(ctx, points);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
canvas.onmousemove = (evt: MouseEvent) => {
|
||||
if (selection !== undefined) {
|
||||
points[selection].x = evt.clientX;
|
||||
points[selection].y = evt.clientY;
|
||||
@ -56,6 +81,11 @@ function init() {
|
||||
}
|
||||
};
|
||||
|
||||
canvas.ontouchend = () => {
|
||||
selection = undefined;
|
||||
};
|
||||
|
||||
|
||||
canvas.onmouseup = () => {
|
||||
selection = undefined;
|
||||
};
|
||||
|
39
spline.ts
39
spline.ts
@ -60,7 +60,8 @@ function init() {
|
||||
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
||||
draw(ctx, points);
|
||||
};
|
||||
canvas.onmousedown = (evt) => {
|
||||
|
||||
canvas.onmousedown = (evt: MouseEvent) => {
|
||||
const { clientX, clientY } = evt;
|
||||
for (const p of points) {
|
||||
if (Math.abs(p.x - clientX) < 10 && Math.abs(p.y - clientY) < 10) {
|
||||
@ -76,7 +77,24 @@ function init() {
|
||||
}
|
||||
};
|
||||
|
||||
canvas.onmousemove = (evt) => {
|
||||
canvas.ontouchstart = (evt: TouchEvent) => {
|
||||
console.assert(evt.touches.length === 1, "Multiple touch points are not supported");
|
||||
const { clientX, clientY } = evt.touches[0];
|
||||
for (const p of points) {
|
||||
if (Math.abs(p.x - clientX) < 10 && Math.abs(p.y - clientY) < 10) {
|
||||
selection = points.indexOf(p);
|
||||
}
|
||||
}
|
||||
if (selection === undefined) {
|
||||
points.push({ x: clientX, y: clientY });
|
||||
// redraw
|
||||
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
||||
draw(ctx, points);
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
canvas.onmousemove = (evt: MouseEvent) => {
|
||||
if (selection !== undefined) {
|
||||
points[selection].x = evt.clientX;
|
||||
points[selection].y = evt.clientY;
|
||||
@ -86,9 +104,26 @@ function init() {
|
||||
}
|
||||
};
|
||||
|
||||
canvas.ontouchmove = (evt: TouchEvent) => {
|
||||
console.assert(evt.touches.length === 1, "Multiple touch points are not supported");
|
||||
const { clientX, clientY } = evt.touches[0];
|
||||
if (selection !== undefined) {
|
||||
points[selection].x = clientX;
|
||||
points[selection].y = clientY;
|
||||
// redraw
|
||||
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
||||
draw(ctx, points);
|
||||
}
|
||||
};
|
||||
|
||||
canvas.onmouseup = () => {
|
||||
selection = undefined;
|
||||
};
|
||||
|
||||
canvas.ontouchend = () => {
|
||||
selection = undefined;
|
||||
};
|
||||
}
|
||||
|
||||
init();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user