-
Notifications
You must be signed in to change notification settings - Fork 4
/
paint.html
68 lines (60 loc) · 1.9 KB
/
paint.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
<!doctype html>
<html>
<head>
<title>Paint!</title>
</head>
<body>
<style>
canvas {
-ms-touch-action: none; /* IE10 prefixed support */
touch-action: none; /* Disable panning */
}
</style>
<canvas id="canvas" width="800" height="600" style="border: 2px solid #333"></canvas>
<script type='text/javascript'>
var COLORS = ["red", "blue", "green", "yellow", "black", "orange"];
var MAX_WIDTH = 10;
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
var pointers=[];
context.lineJoin = "round";
context.lineCap = "round";
if(window.PointerEvent) {
canvas.addEventListener("pointerdown", startPaint);
canvas.addEventListener("pointermove", paint);
canvas.addEventListener("pointerup", endPaint);
canvas.addEventListener("pointercancel", endPaint);
}else if(window.MSPointerEvent) {
canvas.addEventListener("MSPointerDown", startPaint);
canvas.addEventListener("MSPointerMove", paint);
canvas.addEventListener("MSPointerUp", endPaint);
canvas.addEventListener("MSPointerCancel", endPaint);
}
function startPaint(event) {
//Set up a running list of points
pointers[event.pointerId] = [{x: event.offsetX, y: event.offsetY}];
}
function endPaint(event) {
delete pointers[event.pointerId];
}
function paint(event) {
if(event.buttons>0) {
event.preventDefault();
//Add newest point
pointers[event.pointerId].push({x: event.offsetX, y: event.offsetY});
//Create path
context.beginPath();
context.moveTo(pointers[event.pointerId][0].x, pointers[event.pointerId][0].y);
context.lineTo(pointers[event.pointerId][1].x, pointers[event.pointerId][1].y);
//Stroke path
context.lineWidth = event.pressure * MAX_WIDTH;
context.strokeStyle = COLORS[event.pointerId % COLORS.length];
context.stroke();
context.closePath();
//Shift out oldest point
pointers[event.pointerId].shift();
}
}
</script>
</body>
</html>