-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
166 lines (137 loc) · 4.62 KB
/
Copy pathscript.js
File metadata and controls
166 lines (137 loc) · 4.62 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
window.addEventListener('load', () => {
const canvas = document.querySelector('#canvas')
const ctx = canvas.getContext('2d')
const colors = document.querySelectorAll('.color')
const picker = document.querySelector('.color-picker')
const range = document.querySelector('.range')
const clear = document.querySelector('.clear')
const undo = document.querySelector('.undo')
const redo = document.querySelector('.redo')
const erase = document.querySelector('.erase')
const bg = document.querySelector('.bg')
canvas.height = 0.77 * window.innerHeight
canvas.width = 0.8 * window.innerWidth
let bgColor = 'white'
let penColor = 'black'
let penWidth = '3'
let flag = false
let undoArray = []
let undoIdx = -1
let redoArray = []
const addBgPicker = () => {
bg.addEventListener('click', e => {
bg.innerHTML = `<div class='bg-box'>
<input type='color' class='bg-picker' />
</div>`
const bgPicker = document.querySelector('.bg-picker')
bgPicker.addEventListener('input', e => {
bgColor = bgPicker.value
canvas.style.background = bgColor
bg.innerHTML = 'background'
})
})
}
const changeColor = () => {
Array.from(colors).forEach((color, i) => {
color.addEventListener('click', e => {
console.log("color " + i + " clicked")
console.log(color.style.background)
penColor = (i == 0 ? 'red' : (i == 1 ? 'blue' : (i == 2 ? 'green' : 'yellow')))
})
})
}
const changePickerColor = () => {
picker.addEventListener('input', e => {
penColor = picker.value
})
}
const changeWidth = () => {
range.addEventListener('input', e => {
penWidth = range.value
})
}
const clearPage = () => {
clear.addEventListener('click', e => {
ctx.fillStyle = bgColor
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.fillRect(0, 0, canvas.width, canvas.height)
})
}
const undoCanvas = () => {
undo.addEventListener('click', e => {
console.log(undoIdx)
if(undoIdx <= 0) {
ctx.fillStyle = bgColor
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.fillRect(0, 0, canvas.width, canvas.height)
undoArray = []
undoIdx = -1
}
else {
undoIdx -= 1
undoArray.slice(undoIdx, 1)
ctx.putImageData(undoArray[undoIdx], 0, 0)
}
})
}
const redoCanvas = () => {
redo.addEventListener('click', e => {
if(undoIdx + 1 < redoArray.length) {
undoIdx += 1
undoArray.push(redoArray[undoIdx])
ctx.putImageData(redoArray[undoIdx], 0, 0)
}
})
}
const erasePage = () => {
erase.addEventListener('click', e => {
console.log('eraser clicked')
penColor = bgColor
canvas.classList.add('eraser')
console.log(canvas.classList.item(0))
})
}
changeColor()
changePickerColor()
changeWidth()
clearPage()
undoCanvas()
redoCanvas()
erasePage()
addBgPicker()
const start = (e) => {
flag = true
ctx.beginPath()
ctx.moveTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop)
}
const end = (e) => {
if(flag) {
ctx.closePath()
flag = false
}
if(e.type != 'mouseout') {
undoArray.push(ctx.getImageData(0, 0, canvas.width, canvas.height))
redoArray.push(ctx.getImageData(0, 0, canvas.width, canvas.height))
undoIdx += 1;
console.log(undoArray)
}
}
const draw = (e) => {
if(!flag) return
ctx.lineWidth = penWidth
ctx.lineCap = 'round'
ctx.strokeStyle = penColor
ctx.lineJoin = 'round'
ctx.lineTo(e.clientX - canvas.offsetLeft, e.clientY - canvas.offsetTop)
ctx.stroke()
}
canvas.addEventListener('mousedown', start)
canvas.addEventListener('mouseup', end)
canvas.addEventListener('mousemove', draw)
canvas.addEventListener('mouseout', end)
window.addEventListener('resize', (e) => {
canvas.height = 0.77 * window.innerHeight
canvas.width = 0.8 * window.innerWidth
ctx.putImageData(undoArray[undoIdx], 0, 0)
})
})