-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.js
More file actions
87 lines (79 loc) · 1.65 KB
/
draw.js
File metadata and controls
87 lines (79 loc) · 1.65 KB
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
// Settings
var canvasWidth = 600,
canvasHeight = 600,
backgroundColor = "#FFFFFF",
lineColor = "green",
lineWidth = 10;
// Prepare canvas
var c = document.getElementById("myCanvas"),
ctx = c.getContext("2d");listX = new Array,
listY = new Array,
listDrag = new Array,
paint = false;
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
// Clears canvas
function clearDraw() {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
listX = [];
listY = [];
listDrag = [];
}
// Detect mouse actions
function mouseDown(event) {
var x = event.clientX-10;
var y = event.clientY-10;
paint = true;
addList(x, y, false);
redraw();
}
function mouseMove(event) {
var x = event.clientX-10;
var y = event.clientY-10;
var coords = "X coords: " + x + ", Y coords: " + y;
document.getElementById("demo").innerHTML = coords;
if (paint) {
addList(x, y, true);
}
redraw();
listX.shift();
listY.shift();
listDrag.shift();
}
function mouseUp(event) {
listX = [];
listY = [];
listDrag = [];
paint = false;
}
function mouseOut() {
listX = [];
listY = [];
listDrag = [];
paint = false;
document.getElementById("demo").innerHTML = "";
}
// Adds points to draw to array
function addList(x, y, drag) {
listX.push(x);
listY.push(y);
listDrag.push(drag);
}
// Draw on canvas
function redraw() {
ctx.beginPath();
ctx.strokeStyle = lineColor;
ctx.lineWidth = lineWidth;
ctx.lineJoin = "round";
ctx.moveTo(listX[0], listY[0]);
for (i = 0; i < listX.length; i++) {
if (listDrag[i]) {
ctx.moveTo(listX[i - 1], listY[i - 1]);
} else {
ctx.moveTo(listX[i], listY[i]);
}
ctx.lineTo(listX[i], listY[i]);
ctx.closePath();
ctx.stroke();
}
}