-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
261 lines (200 loc) · 7.41 KB
/
script.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
import brush from "./paint-tools/brush.js";
import flood_fill from "./paint-tools/bucket.js";
import ditherBrush from "./paint-tools/ditherBrush.js";
import eraser from "./paint-tools/eraser.js";
import pencil from "./paint-tools/pencil.js";
import spray from "./paint-tools/spray.js";
import { hexToRGBA } from "./paint-tools/utils.js";
const canvas = document.getElementById("painting");
const context = canvas.getContext("2d", {
willReadFrequently: true
});
let imageData = context.getImageData(0, 0, canvas.width, canvas.height);
let imageHistory = [imageData]
const selects = document.querySelectorAll("div.select")
const selectValues = {}
for (const select of selects) {
selectValues[select.id] = select.ariaCurrent || ''
const defaultSelected = select.querySelector(`[value="${select.ariaCurrent}"]`)
defaultSelected.classList.add("selected")
const buttons = select.querySelectorAll("button")
for (const button of buttons) {
button.onclick = function() {
const lastSelected = select.querySelector(".selected")
if (lastSelected) lastSelected.classList.remove("selected")
button.classList.add("selected")
selectValues[select.id] = button.value
select.ariaCurrent = button.value
}
}
}
const colorValue = document.getElementById("fillColor")
const intensityValue = document.getElementById("intensity")
const undoButton = document.getElementById("undo")
const redoButton = document.getElementById("redo")
let currentHistoryPosition = 1
function pushHistory(overrideImage) {
let savedImage = imageData
if (overrideImage != undefined) savedImage = overrideImage
imageHistory.splice(imageHistory.length - (currentHistoryPosition - 1))
redoButton.disabled = true
undoButton.disabled = false
currentHistoryPosition = 1
imageHistory.push(savedImage)
if (imageHistory.length > 5) imageHistory.shift()
}
function moveHistory(position) {
currentHistoryPosition = Math.max(1, Math.min(imageHistory.length, currentHistoryPosition - position))
if (currentHistoryPosition == imageHistory.length) {
undoButton.disabled = true
} else {
undoButton.disabled = false
}
if (currentHistoryPosition > 1) {
redoButton.disabled = false
} else {
redoButton.disabled = true
}
console.log(imageHistory, currentHistoryPosition)
let newImageData = imageHistory[imageHistory.length - currentHistoryPosition]
if (newImageData == undefined) return
context.putImageData(newImageData, 0, 0);
}
undoButton.onclick = () => moveHistory(-1)
redoButton.onclick = () => moveHistory(1)
function clearCanvas() {
let newImageData = new ImageData(imageData.width, imageData.height)
imageData = newImageData
context.putImageData(newImageData, 0, 0);
pushHistory()
}
document.getElementById("clearCanvas").onclick = clearCanvas
let activeClick = false;
let lastPos = undefined;
let paintDebounce = false;
let lastTouches = {};
const tools = {
"pencil": pencil,
"brush": brush,
"ditherBrush": ditherBrush,
"spray": spray,
"eraser": eraser
}
const startTools = {
"bucket": async (pixels, x, y, color, weight) => {
const newPixels = await flood_fill(canvas, context, x, y, color)
pushHistory(newPixels)
activeClick = false;
return newPixels
}
}
async function paint(event, isTouch, currentToolList) {
if (paintDebounce == true || activeClick == false) return;
if (currentToolList == undefined) {
currentToolList = tools
}
if (currentToolList[selectValues.mode] == undefined) return;
paintDebounce = true
let identifier = -1
if (event.identifier != undefined) {
isTouch = true;
identifier = event.identifier
}
let force = 1
if (event.force != undefined) force = event.force
function setLastPosition() {
if (isTouch == true) {
lastTouches[identifier] = { y: y, x: x }
} else {
lastPos = { y: y, x: x }
}
}
const x = Math.floor((event.offsetX / canvas.offsetWidth) * canvas.width);
const y = Math.floor((event.offsetY / canvas.offsetHeight) * canvas.height);
let currentLast = undefined
if (isTouch == true) {
currentLast = lastTouches[identifier]
} else {
currentLast = lastPos
}
imageData = context.getImageData(0, 0, canvas.width, canvas.height)
let newPixels = imageData
if (currentLast == null) currentLast = { y: y, x: x };
const diffX = x - currentLast?.x
const diffY = y - currentLast?.y
const distance = Math.sqrt(diffX * diffX + diffY * diffY)
const steps = Math.max(1, Math.floor(distance * 2))
const sizeRadius = Math.floor(parseInt(selectValues.radius) * force)
// makes an approximated path from the last position to the new position
for (let t = 0; t < steps; t++) {
const time = t / steps
const lerpX = Math.floor((1 - time) * currentLast.x + (time * x))
const lerpY = Math.floor((1 - time) * currentLast.y + (time * y))
newPixels = await currentToolList[selectValues.mode](
newPixels, // pixels
lerpX, lerpY, // coordinates
hexToRGBA(colorValue.value), // color
sizeRadius, // radius
intensityValue.value // weight
);
}
paintDebounce = false
if (newPixels != undefined) {
context.putImageData(newPixels, 0, 0);
if (activeClick == true) setLastPosition()
}
}
function setMouseStatus(event, status) {
if (status == activeClick) return;
if (status == true) {
activeClick = true;
if (startTools[selectValues.mode] != undefined) {
paint(event, false, startTools)
activeClick = false;
return
}
paint(event, event.identifier != undefined, tools);
return;
}
pushHistory()
lastPos = undefined;
activeClick = false;
}
canvas.addEventListener("mouseleave", (e) => setMouseStatus(e, false));
canvas.addEventListener("mouseup", (e) => setMouseStatus(e, false));
canvas.addEventListener("mousedown", (e) => setMouseStatus(e, true));
canvas.addEventListener("mousemove", paint);
function convertTouchToMouse(TouchEvent, callback) {
if (TouchEvent == undefined) return
if (TouchEvent.touches == undefined || callback == undefined) return
for (const touch of TouchEvent.touches) {
const offset = {
offsetX: touch.clientX - canvas.offsetLeft,
offsetY: touch.clientY - canvas.offsetTop
}
callback({
...touch,
identifier: touch.identifier,
force: touch.force,
...offset
}, true)
}
}
function endTouch(event) {
for (const touchIdentifier of Object.keys(lastTouches)) {
let exists = false
for (const touch of event.touches) {
if (touch.identifier == touchIdentifier) exists = true
}
if (exists == false) {
delete lastTouches[touchIdentifier]
}
}
if (event.touches.length < 1) {
setMouseStatus(event, false);
}
}
canvas.addEventListener("touchend", endTouch);
canvas.addEventListener("touchcancel", endTouch);
canvas.addEventListener("touchstart", (event) => convertTouchToMouse(event, setMouseStatus));
canvas.addEventListener("touchmove", (event) => convertTouchToMouse(event, paint));