<canvas id="canvas" width="400" height="400" style="border:solid black 1px; touch-action:none">
Your browser does not support canvas element.
</canvas>
<br>
<button onclick="window.startup();">Initialize</button>
<br>
Log:
<pre id="log" style="border: 1px solid #ccc;"></pre>
CSS
body {
padding: 10px;
font-family: sans-serif;
}
JavaScript
function startup() {
var el = document.getElementsByTagName("canvas")[0];
el.addEventListener("pointerdown", handleStart, false);
el.addEventListener("pointerup", handleEnd, false);
el.addEventListener("pointercancel", handleCancel, false);
el.addEventListener("pointermove", handleMove, false);
log("initialized.");
}
var ongoingTouches = new Array();
function handleStart(evt) {
log("pointerdown.");
var el = document.getElementsByTagName("canvas")[0];
var ctx = el.getContext("2d");
log("pointerdown: id = " + evt.pointerId);
ongoingTouches.push(copyTouch(evt));
var color = colorForTouch(evt);
ctx.beginPath();
ctx.arc(touches[i].pageX, touches[i].pageY, 4, 0, 2 * Math.PI, false); // a circle at the start
ctx.arc(evt.clientX, evt.clientY, 4, 0, 2 * Math.PI, false); // a circle at the start
ctx.fillStyle = color;
ctx.fill();
}
function handleMove(evt) {
var el = document.getElementsByTagName("canvas")[0];
var ctx = el.getContext("2d");
var color = colorForTouch(evt);
var idx = ongoingTouchIndexById(evt.pointerId);
log("continuing touch: idx = " + idx);
if (idx >= 0) {
ctx.beginPath();
log("ctx.moveTo(" + ongoingTouches[idx].pageX + ", " + ongoingTouches[idx].pageY + ");");
ctx.moveTo(ongoingTouches[idx].pageX, ongoingTouches[idx].pageY);
log("ctx.lineTo(" + evt.clientX + ", " + evt.clientY + ");");
ctx.lineTo(evt.clientX, evt.clientY);
ctx.lineWidth = 4;
ctx.strokeStyle = color;
ctx.stroke();
ongoingTouches.splice(idx, 1, copyTouch(evt)); // swap in the new touch record
log(".");
} else {
log("can't figure out which touch to continue: idx = " + idx);
}
}
function handleEnd(evt) {
log("pointerup");
var el = document.getElementsByTagName("canvas")[0];
var ctx = el.getContext("2d");
var color = colorForTouch(evt);
var idx = ongoingTouchIndexById(evt.pointerId);
if (idx >= 0) {
ctx.lineWidth = 4;
ctx.fillStyle = color;
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.