forked from kmcgrady/pixel-art-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixel-art.js
377 lines (315 loc) · 9.72 KB
/
pixel-art.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* eslint-disable require-jsdoc */
'use strict';
let COLORS = [
'white',
'black',
'red',
'orange',
'pink',
'brown',
'green',
'cornflowerblue',
'blue',
'purple',
'dimgray',
'aqua',
'darkblue',
'cornsilk',
'deepskyblue',
'deeppink',
'gold',
'indigo',
'greenyellow',
'fuchsia',
]; // 20 colors
const ls = localStorage;
const blankColor = '#cccccc';
let historyPaint = [];
document.addEventListener('keydown', stepBack, false);
const canvas = document.getElementById('canvas');
const palette = document.getElementById('palette');
const sizeCanvas = document.getElementById('sizeCanvas');
canvas.addEventListener('mousedown', paintListener, false);
canvas.addEventListener('contextmenu', (e) => e.preventDefault(), false);
sizeCanvas.addEventListener('change', repaintCanvas, false);
sizeCanvas.addEventListener('keydown', isDelete, false);
palette.addEventListener('mousedown', colorListener, false);
palette.addEventListener('contextmenu', (e) => e.preventDefault(), false);
const container = document.querySelector('.container');
let lbmColor = document.querySelector('.lbm-color').style;
let rbmColor = document.querySelector('.rbm-color').style;
let currentFirstColor = lbmColor.backgroundColor = COLORS[0];
let currentSecondColor = rbmColor.backgroundColor = COLORS[1];
const colorPickerValue = document.querySelector('.color-result');
const colorPicker = document.getElementById('colorPicker');
colorPickerValue.addEventListener('mousedown', colorListener, false);
colorPickerValue.addEventListener(
'contextmenu', (e) => e.preventDefault(), false
);
colorPicker.addEventListener('change',
(e) => colorPickerValue.style.backgroundColor = e.target.value, false);
const checkbox = document.getElementById('checkbox');
checkbox.addEventListener('change',
(e) => floodFillMode.turnOn = !floodFillMode.turnOn, false);
const confirmationMessage =
'Do you really want to leave without saving your drawing locally?';
window.addEventListener('beforeunload', function(e) {
e.preventDefault();
if (!ls.getItem(keyDrawing)) {
e.returnValue = confirmationMessage;
return confirmationMessage;
}
});
const clearCanvasButt = document.getElementById('clearCanvas');
clearCanvasButt.addEventListener('click', clearCanvas);
const keyDrawing = 'drawing';
const keySizeCanvas = 'sizeCanvas';
const saveLocallyButton = document.getElementById('saveLocal');
saveLocallyButton.addEventListener('click', saveToLocalStorage);
function saveToLocalStorage() {
if (historyPaint.length === 0) return false;
ls.setItem(keyDrawing, getDrawing(historyPaint));
ls.setItem(keySizeCanvas, sizeCanvas.value);
alert('Done! You\'ve saved a drawing!');
}
function loadDrawingFromLS() {
return JSON.parse(ls.getItem(keyDrawing));
}
function simpleFillCell(target, fillColor) {
if (!target && !fillColor) return false;
target.style.backgroundColor = fillColor;
target.classList.add('filled');
}
function loadCanvas() {
let value = ls.getItem(keyDrawing);
if (ls.hasOwnProperty(keyDrawing) && value !== undefined) {
historyPaint = loadDrawingFromLS();
} else {
return false;
}
let dataOfDrawing = loadDrawingFromLS();
if (!dataOfDrawing || dataOfDrawing.length === 0) return false;
dataOfDrawing.forEach((el) => {
let cell = document.querySelector(
`.cell[x='${getX(el)}'][y='${getY(el)}']`
);
simpleFillCell(cell, el.color);
});
}
/**
* @param {Array} historyPaint
* @return {Array}
*/
function getDrawing(historyPaint) {
let length = historyPaint.length;
let totalCellOfCanvas = Math.pow(sizeCanvas.value, 2);
if (length === 0) return false;
if (length <= totalCellOfCanvas) return JSON.stringify(historyPaint);
let tmpMap = new Map();
historyPaint.forEach((el) => {
el.oldColor = blankColor;
tmpMap.set(el.hashCode, el);
});
let uniqueHistoryPaint = [...tmpMap.values()];
return JSON.stringify(uniqueHistoryPaint);
}
const floodFillMode = new Proxy({turnOn: false}, {
get(target, prop) {
return target[prop];
},
set(target, prop, value) {
canvas.className =
(canvas.className === 'brush') ? 'flood-fill' : 'brush';
target[prop] = value;
return true;
},
});
function colorListener(e) {
e = e || window.event;
let target = e.target;
let newColor = getBGColor(target);
if (e.which !== 3) {
lbmColor.backgroundColor = currentFirstColor = newColor;
} else {
rbmColor.backgroundColor = currentSecondColor = newColor;
}
}
function paintListener(e) {
if (!floodFillMode.turnOn) {
e.preventDefault();
fillColor();
canvas.addEventListener('mouseenter', fillColor, true);
canvas.addEventListener('mouseup', (e) =>
canvas.removeEventListener('mouseenter', fillColor, true)
, false);
} else { // turned flood fill mode on
e = e || window.event;
let target = e.target;
let prev = historyPaint.length;
floodFill(
parseInt(getX(target)),
parseInt(getY(target)),
getBGColor(target),
whichColor(e)
);
/** Add special value for correct 'step back' feature
* when we used the flood fill method.
* ---
* if size of array(historyPaint) did't change =>
* set previous value to the last element of historyPaint otherwise
* subtraction of curr size and prev value
*/
historyPaint[historyPaint.length - 1].countOfFloodFill =
prev !== historyPaint.length ? historyPaint.length - prev : prev;
}
}
function getBGColor(target) {
return getComputedStyle(target).backgroundColor;
}
function getX(target) {
return target.x === 0 || target.x ? target.x : target.getAttribute('x');
}
function getY(target) {
return target.y === 0 || target.y ? target.y : target.getAttribute('y');
}
function fillColor(e) {
e = e || window.event;
let target = e.target;
let newColor;
let oldColor;
if (target.className.includes('cell')) {
oldColor = getBGColor(target);
if (e.which !== 3) {
target.style.backgroundColor = newColor = currentFirstColor;
} else {
target.style.backgroundColor = newColor = currentSecondColor;
}
historyPaint.push({
x: parseInt(getX(target)),
y: parseInt(getY(target)),
color: newColor,
oldColor: oldColor,
hashCode: getX(target) + '-' + getY(target),
});
// console.log(historyPaint.length);
target.classList.add('filled');
}
}
function repaintCanvas(e, size) {
e = e || window.event;
let newCountOfCell = !size ? e.target.value : size;
if (canvas.hasChildNodes()) {
canvas.innerHTML = '';
}
ls.clear();
historyPaint = [];
setTimeout(drawCanvas(newCountOfCell >= 10 ? newCountOfCell : 10), 4);
}
function drawCanvas(size) {
sizeCanvas.value = size;
let containerComputed = getComputedStyle(container);
let dimension = `${parseInt(containerComputed.width, 10) / size}px`;
for (let i = 0; i < size; i++) {
let cell = document.createElement('div');
cell.className = 'cell';
cell.style.width = dimension;
cell.style.height = dimension;
cell.setAttribute('x', i);
for (let j = 0; j < size; j++) {
cell.setAttribute('y', j);
canvas.appendChild(cell.cloneNode(false));
}
}
}
/**
* @param {number} x
* @param {number} y
* @param {Element} oldColor
* @param {Element} newColor
**/
function floodFill(x, y, oldColor, newColor) {
let target = document.querySelector(`.cell[x='${x}'][y='${y}']`);
if (!target || getBGColor(target) !== oldColor || oldColor === newColor) {
return;
}
historyPaint.push(
{
x: x, y: y,
color: newColor,
oldColor: oldColor,
hashCode: x + '-' + y,
}
);
target.style.backgroundColor = newColor;
target.classList.add('filled');
floodFill(x - 1, y, oldColor, newColor);
floodFill(x, y - 1, oldColor, newColor);
floodFill(x + 1, y, oldColor, newColor);
floodFill(x, y + 1, oldColor, newColor);
}
function drawPalette(colors = ['blue', 'red', 'black', 'white']) {
// console.log(colors.length);
for (let i = 0; i < colors.length; i++) {
let colorDiv = document.createElement('div');
colorDiv.className = `color-${i + 1}`;
colorDiv.style.backgroundColor = colors[i];
colorDiv.style.width = `${container.clientWidth / colors.length }px`;
colorDiv.style.height = '40px';
palette.appendChild(colorDiv);
}
}
function cleanCell(target) {
if (!target) return;
let cell = document.querySelector(
`.cell[x='${getX(target)}'][y='${getY(target)}']`
);
cell.style.backgroundColor = target.oldColor;
if (!historyPaint.find(
(el) => el.hashCode === target.hashCode)) {
cell.classList.remove('filled');
}
}
function stepBack(e) {
e = e || window.event;
let code = e.which || e.keyCode;
// press Ctrl + Z
if (code === 90 && e.ctrlKey && historyPaint.length !== 0) {
let target = historyPaint.pop();
if (!target.countOfFloodFill) {
// simple clean cell
cleanCell(target);
} else {
// clean after flood fill
let count = target.countOfFloodFill;
cleanCell(target);
for (let i = 0; i < count - 1; i++) {
target = historyPaint.pop();
cleanCell(target);
}
}
}
}
function whichColor(e) {
let code = e.which || e.keyCode;
if (code !== 3) {
return currentFirstColor;
}
return currentSecondColor;
}
function clearCanvas() {
ls.clear();
historyPaint = [];
repaintCanvas(null, sizeCanvas.value);
}
function isDelete(e) {
e = e || window.event;
return e.keyCode !== 8 ? e.preventDefault() : true;
}
function getSizeCanvas() {
return ls.hasOwnProperty(keySizeCanvas) ? ls.getItem(keySizeCanvas) : 40;
}
document.addEventListener('DOMContentLoaded', () => {
drawCanvas(getSizeCanvas());
drawPalette(COLORS);
loadCanvas();
}, false);