<canvas id="canvas" width="600" height="600">
Your browser does not support the canvas element.
</canvas>
<button id="clear">Clear canvas</button>Touch events / notebook
Touch events
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
#canvas {
border: 1px solid black;
display: block;
}const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// Mapping from the pointerId to the current finger position
const ongoingTouches = new Map();function handleStart(event) {
event.preventDefault();
for (const changedTouch of event.changedTouches) {
const touch = {
pageX: changedTouch.pageX,
pageY: changedTouch.pageY,
color: colorForTouch(changedTouch),
};
ongoingTouches.set(changedTouch.identifier, touch);
ctx.beginPath();
ctx.arc(touch.pageX, touch.pageY, 4, 0, 2 * Math.PI, false);
ctx.fillStyle = touch.color;
ctx.fill();
}
}
canvas.addEventListener("touchstart", handleStart);function colorForTouch(touch) {
const r = touch.identifier % 16;
const g = Math.floor(touch.identifier / 3) % 16;
const b = Math.floor(touch.identifier / 7) % 16;
// convert to hex digits
return `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
}function handleEnd(event) {
event.preventDefault();
for (const changedTouch of event.changedTouches) {
const touch = ongoingTouches.get(changedTouch.identifier);
if (!touch) {
console.error(`End: Could not find touch ${changedTouch.identifier}`);
continue;
}
ctx.lineWidth = 4;
ctx.fillStyle = touch.color;
ctx.beginPath();
ctx.moveTo(touch.pageX, touch.pageY);
ctx.lineTo(changedTouch.pageX, changedTouch.pageY);
ctx.fillRect(changedTouch.pageX - 4, changedTouch.pageY - 4, 8, 8);
ongoingTouches.delete(changedTouch.identifier);
}
}
canvas.addEventListener("touchend", handleEnd);function handleCancel(event) {
event.preventDefault();
for (const changedTouch of event.changedTouches) {
if (!ongoingTouches.has(changedTouch.identifier)) {
console.error(`Cancel: Could not find touch ${changedTouch.identifier}`);
continue;
}
ongoingTouches.delete(changedTouch.identifier);
}
}
canvas.addEventListener("touchcancel", handleCancel);function handleMove(event) {
event.preventDefault();
for (const changedTouch of event.changedTouches) {
const touch = ongoingTouches.get(changedTouch.identifier);
if (!touch) {
console.error(`Move: Could not find touch ${changedTouch.identifier}`);
continue;
}
ctx.beginPath();
ctx.moveTo(touch.pageX, touch.pageY);
ctx.lineTo(changedTouch.pageX, changedTouch.pageY);
ctx.lineWidth = 4;
ctx.strokeStyle = touch.color;
ctx.stroke();
const newTouch = {
pageX: changedTouch.pageX,
pageY: changedTouch.pageY,
color: touch.color,
};
ongoingTouches.set(changedTouch.identifier, newTouch);
}
}
canvas.addEventListener("touchmove", handleMove);document.getElementById("clear").addEventListener("click", () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});function onTouch(event) {
event.preventDefault();
if (
event.touches.length > 1 ||
(event.type === "touchend" && event.touches.length > 0)
)
return;
let type;
let touch;
switch (event.type) {
case "touchstart":
type = "mousedown";
touch = event.changedTouches[0];
break;
case "touchmove":
type = "mousemove";
touch = event.changedTouches[0];
break;
case "touchend":
type = "mouseup";
touch = event.changedTouches[0];
break;
}
const newEvent = new MouseEvent(type, {
bubbles: true,
cancelable: true,
view: event.originalTarget.ownerDocument.defaultView,
detail: 0,
screenX: touch.screenX,
screenY: touch.screenY,
clientX: touch.clientX,
clientY: touch.clientY,
ctrlKey: event.ctrlKey,
altKey: event.altKey,
shiftKey: event.shiftKey,
metaKey: event.metaKey,
button: 0,
relatedTarget: null,
});
event.originalTarget.dispatchEvent(newEvent);
}