-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.js
More file actions
57 lines (44 loc) · 1.31 KB
/
draw.js
File metadata and controls
57 lines (44 loc) · 1.31 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
const canvas = document.getElementById('drawSpace');
const tool = document.getElementById('tools');
const ctx = canvas.getContext('2d');
const canvasOffsetX = canvas.offsetLeft;
const canvasOffsetY = canvas.offsetTop;
canvas.width = window.innerWidth - canvasOffsetX;
canvas.height = window.innerHeight - canvasOffsetY;
let isPainting = false;
let lineWidth = 6;
let startX;
let startY;
tool.addEventListener('click', listener => {
if (listener.target.id === 'clear') {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
});
tool.addEventListener('change', listener => {
if(listener.target.id === 'penColor') {
ctx.strokeStyle = listener.target.value;
}
if(listener.target.id === 'penSize') {
lineWidth = listener.target.value;
}
});
const draw = (listener) => {
if(!isPainting) {
return;
}
ctx.lineWidth = lineWidth;
ctx.lineCap = "round";
ctx.lineTo(listener.clientX - (canvasOffsetX*.5) + 5, listener.clientY-canvasOffsetY + 80);
ctx.stroke();
}
canvas.addEventListener('mousedown', (listener) => {
isPainting = true;
startX = listener.off;
startY = listener.clientY;
});
canvas.addEventListener('mouseup', listener => {
isPainting = false;
ctx.stroke();
ctx.beginPath();
});
canvas.addEventListener('mousemove', draw);