-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
paint-canvas-element.js
261 lines (224 loc) · 6.57 KB
/
paint-canvas-element.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const states = new WeakMap()
const histories = new WeakMap()
class PaintCanvasElement extends HTMLElement {
constructor() {
super()
const shadowRoot = this.attachShadow({mode: 'open'})
shadowRoot.innerHTML = `<canvas></canvas>`
const canvas = shadowRoot.querySelector('canvas')
states.set(this, {
drawing: false,
color: "#000000",
bgcolor: "#ffffff",
size: 10,
canvas,
context: canvas.getContext('2d')
})
histories.set(this, {
log: [],
currentEntry: [],
currentStep: 0
})
}
static get observedAttributes() {
return ['height', 'width', 'color', 'size', 'bgcolor']
}
get canvas() {
return states.get(this).canvas
}
get height() {
return Number(this.getAttribute('height'))
}
set height(value) {
this.setAttribute('height', value)
}
get width() {
return Number(this.getAttribute('width'))
}
set width(value) {
this.setAttribute('width', value)
}
get color() {
return this.getAttribute('color')
}
set color(value) {
this.setAttribute('color', value)
}
get size() {
return Number(this.getAttribute('size'))
}
set size(value) {
this.setAttribute('size', value)
}
get bgcolor() {
return this.getAttribute('bgcolor')
}
set bgcolor(value) {
this.setAttribute('bgcolor', value)
}
get isDrawing() {
return this.hasAttribute('drawing')
}
set isDrawing(value) {
value ? this.setAttribute('drawing', '') : this.removeAttribute('drawing')
}
insertStep(step) {
const history = histories.get(this)
const {currentStep} = history
history.log.push(step)
history.currentStep = history.log.length
}
undo() {
const history = histories.get(this)
const {currentStep} = history
redraw(this, currentStep - 1)
}
redo() {
const history = histories.get(this)
const {currentStep} = history
redraw(this, currentStep + 1)
}
clear() {
const {canvas, context, bgcolor, width, height} = states.get(this)
canvas.style.width = `${width}px`
canvas.width = Number(width) * window.devicePixelRatio
canvas.style.height = `${height}px`
canvas.height = Number(height) * window.devicePixelRatio
context.scale(window.devicePixelRatio, window.devicePixelRatio)
context.beginPath()
context.fillStyle = bgcolor
context.fillRect(0, 0, width, height)
context.closePath()
}
reset() {
this.clear()
const history = histories.get(this)
history.log = []
history.currentStep = 0
}
attributeChangedCallback(attr, oldValue, newValue) {
const state = states.get(this)
if (attr === 'height') {
state.height = newValue
redraw(this)
}
if (attr === 'width') {
state.width = newValue
redraw(this)
}
if (attr === 'color') state.color = newValue
if (attr === 'size') state.size = newValue
if (attr === 'bgcolor') {
state.bgcolor = newValue
redraw(this)
}
}
connectedCallback() {
this.addEventListener('mousedown', startDrawing)
this.addEventListener('touchstart', startDrawing)
this.addEventListener('mouseup', stopDrawing)
this.addEventListener('touchcancel', stopDrawing)
this.addEventListener('mouseleave', stopDrawing)
this.addEventListener('touchend', stopDrawing)
this.addEventListener('touchmove', draw)
this.addEventListener('mousemove', draw)
this.addEventListener('keydown', historyControl)
this.setAttribute('tabindex', '0')
}
}
function historyControl(event) {
if (event.key.toLowerCase() !== 'z' || (!event.metaKey && !event.ctrlKey)) return
event.shiftKey ? event.currentTarget.redo() : event.currentTarget.undo()
}
function redraw(element, toStep) {
const {context} = states.get(element)
const history = histories.get(element)
const {log} = history
const destination = toStep === undefined ? history.currentStep : Math.max(Math.min(toStep, log.length), 0)
let start
if (toStep && history.currentStep <= destination) {
start = history.currentStep
} else {
element.clear()
start = 0
}
for (const entry of log.slice(start, destination)) {
if (entry instanceof Function) {
entry(element)
continue
}
for (const [from, to, color, size] of entry) {
context.lineJoin = 'round'
context.lineCap = 'round'
context.lineWidth = size
context.strokeStyle = color
context.beginPath()
context.moveTo(...from)
context.lineTo(...to)
context.stroke()
context.closePath()
}
}
if (history.currentStep !== destination) {
history.currentStep = destination
element.dispatchEvent(new CustomEvent('paint-canvas:history-step', {
bubbles: true,
detail: history
}))
}
}
function startDrawing(event) {
if (event.touches && event.touches.length > 1) return
if (event.touches) event.preventDefault()
const state = states.get(event.currentTarget)
state.drawing = true
event.currentTarget.isDrawing = true
}
function stopDrawing(event) {
if (event.touches && event.touches.length > 1) return
if (event.touches) event.preventDefault()
const state = states.get(event.currentTarget)
const history = histories.get(event.currentTarget)
draw(event)
if (history.currentEntry.length > 0) {
// Rewrite history if we are not at the latest step
if (history.currentStep !== history.log.length) {
history.log = history.log.slice(0, history.currentStep)
}
event.currentTarget.insertStep(history.currentEntry)
event.currentTarget.dispatchEvent(new CustomEvent('paint-canvas:history-change', {
bubbles: true,
detail: history
}))
}
history.currentEntry = []
state.drawing = false
event.currentTarget.isDrawing = false
state.lastX = null
state.lastY = null
}
function draw(event) {
if (event.touches && event.touches.length > 1) return
if (event.touches) event.preventDefault()
const state = states.get(event.currentTarget)
const history = histories.get(event.currentTarget)
const {drawing, canvas, context, color, size, lastX, lastY} = state
if (!drawing) return
const {offsetX, offsetY} = event
const from = [lastX || offsetX, lastY || offsetY]
const to = [offsetX, offsetY]
history.currentEntry.push([from, to, color, size])
context.lineJoin = 'round'
context.lineCap = 'round'
context.lineWidth = size
context.strokeStyle = color
context.beginPath()
context.moveTo(...from)
context.lineTo(...to)
context.stroke()
context.closePath()
state.lastX = offsetX
state.lastY = offsetY
}
window.customElements.define('paint-canvas', PaintCanvasElement)
window.PaintCanvasElement = PaintCanvasElement