forked from codepo8/pixels-and-colours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-continuous-paint.html
70 lines (66 loc) · 1.28 KB
/
03-continuous-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
69
70
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta name="viewport" content="width=device-width">
<meta charset="UTF-8">
<title>Continuous paint</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
font-family: helvetica,arial,sans-serif;
}
canvas {
display: block;
background: #ccc;
}
</style>
</head>
<body>
<canvas></canvas>
<script>
var c = document.querySelector('canvas');
var cx = c.getContext('2d');
var mousedown = false;
var oldx = null;
var oldy = null;
function setupCanvas() {
c.height = 480;
c.width = 320;
cx.lineWidth = 20;
cx.lineCap = 'round';
cx.strokeStyle = 'rgb(0, 0, 50)';
}
function onmousedown(ev) {
mousedown = true;
ev.preventDefault();
}
function onmouseup(ev) {
mousedown = false;
ev.preventDefault();
}
function onmousemove(ev) {
var x = ev.clientX;
var y = ev.clientY;
if (mousedown) {
paint(x, y);
}
}
function paint(x, y) {
cx.beginPath();
if (oldx > 0 && oldy > 0) {
cx.moveTo(oldx, oldy);
}
cx.lineTo(x, y);
cx.stroke();
cx.closePath();
oldx = x;
oldy = y;
}
c.addEventListener('mousedown', onmousedown, false);
c.addEventListener('mouseup', onmouseup, false);
c.addEventListener('mousemove', onmousemove, false);
setupCanvas();
</script>
</body>
</html>