-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
95 lines (66 loc) · 1.64 KB
/
main.js
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
const canvas = document.getElementById("canvas");
const increaseBtn = document.getElementById('increase')
const decreaseBtn = document.getElementById('decrease')
const clearBtn = document.getElementById('clear')
const sizeBtn = document.getElementById('size')
const colorBtn = document.getElementById('color')
const ctx = canvas.getContext("2d");
let isPressed = false
let size = 5
let color = 'black'
let x
let y
canvas.addEventListener('mousedown', (e) => {
isPressed = true
x = e.offsetX
y = e.offsetY
})
canvas.addEventListener('mouseup', (e) => {
isPressed = false
x = undefined
y = undefined
})
canvas.addEventListener('mousemove', (e) => {
if (isPressed) {
const x2 = e.offsetX
const y2 = e.offsetY
drwaCircle(x2, y2)
drwaLine(x, y, x2, y2)
x = x2
y= y2
}
})
function drwaCircle(x, y) {
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2);
ctx.fillStyle = color
ctx.fill();
}
function drwaLine(x1, y1, x2, y2) {
ctx.beginPath()
ctx.moveTo(x1, y1)
ctx.lineTo(x2, y2)
ctx.strokeStyle = color
ctx.lineWidth = size * 2
ctx.stroke()
}
function updateSizeOnScreen() {
sizeBtn.innerText = size
}
increaseBtn.addEventListener('click', () => {
size += 5
if(size > 30) {
size = 30
}
updateSizeOnScreen()
})
decreaseBtn.addEventListener('click', () => {
size -= 5
if(size < 5) {
size = 5
}
updateSizeOnScreen()
})
clearBtn.addEventListener('click', (e) => ctx.clearRect(0, 0, canvas.width, canvas.height))
colorBtn.addEventListener('change', (e) =>
color = e.target.value)