-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathUsing_Pointer_Events.html
134 lines (118 loc) · 4.15 KB
/
Using_Pointer_Events.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!doctype html>
<html lang="en">
<!--
This application is documented in "Using Pointer Events":
<https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events/Using_Pointer_Events>
The application is a port of the example in "Using Touch events":
<https://developer.mozilla.org/en-US/docs/Web/API/Touch_events>
This port uses Pointer Events instead of Touch Events.
-->
<head>
<link rel="stylesheet" type="text/css" href="https://developer.mozilla.org/en-US/docs/Template:CustomSampleCSS?raw=1" />
</head>
<body>
<canvas id="canvas" width="600" height="300" style="border:solid black 1px; touch-action:none">
Your browser does not support canvas element.
</canvas>
<br>
<button onclick="startup();">Initialize</button>
<br>
Log:
<pre id="log" style="border: 1px solid #ccc;"></pre>
<script type="text/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;
ctx.beginPath();
ctx.moveTo(ongoingTouches[idx].pageX, ongoingTouches[idx].pageY);
ctx.lineTo(evt.clientX, evt.clientY);
ctx.fillRect(evt.clientX - 4, evt.clientY - 4, 8, 8); // and a square at the end
ongoingTouches.splice(idx, 1); // remove it; we're done
} else {
log("can't figure out which touch to end");
}
}
function handleCancel(evt) {
log("pointercancel: id = " + evt.pointerId);
var idx = ongoingTouchIndexById(evt.pointerId);
ongoingTouches.splice(idx, 1); // remove it; we're done
}
function colorForTouch(touch) {
var r = touch.pointerId % 16;
var g = Math.floor(touch.pointerId / 3) % 16;
var b = Math.floor(touch.pointerId / 7) % 16;
r = r.toString(16); // make it a hex digit
g = g.toString(16); // make it a hex digit
b = b.toString(16); // make it a hex digit
var color = "#" + r + g + b;
log("color for touch with identifier " + touch.pointerId + " = " + color);
return color;
}
function copyTouch(touch) {
return { identifier: touch.pointerId, pageX: touch.clientX, pageY: touch.clientY };
}
function ongoingTouchIndexById(idToFind) {
for (var i = 0; i < ongoingTouches.length; i++) {
var id = ongoingTouches[i].identifier;
if (id == idToFind) {
return i;
}
}
return -1; // not found
}
function log(msg) {
var p = document.getElementById('log');
p.innerHTML = msg + "\n" + p.innerHTML;
}
</script>
</body>
</html>