-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
264 lines (237 loc) · 10 KB
/
app.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
/* eslint-disable no-undef */
import {
createImage,
getStorage,
renderImage,
setStorage,
titleKey,
updateImage,
} from './utils.js';
let imageObject = {};
const header = document.querySelector('.header');
const canvasModule = document.getElementById('canvas-container');
const welcomeModule = document.querySelector('.welcome-container');
const displayTitle = document.getElementById('display-title');
const startBtn = document.getElementById('submit-title');
const artworkTitle = document.getElementById('artwork-title');
const clearBtn = document.querySelector('.clear-canvas');
const colorSelect = document.getElementById('color-select');
const saveBtn = document.getElementById('save-image');
const pencil = document.getElementById('pencil');
const heightInput = document.getElementById('height-input');
const widthInput = document.getElementById('width-input');
const homePageLogo = document.getElementById('logo');
const rainbowArray = ['#f54242', '#f59642', '#f5e942', '#84f542', '#42ddf5', '#b15beb', '#f779b4'];
const paletteBtn = document.getElementById('color-select-img');
const audio = document.getElementById('audio-bit');
const audioButton = document.getElementById('audio-button');
const playlist = ['http://soundimage.org/wp-content/uploads/2017/07/Arcade-Puzzler.mp3', 'http://soundimage.org/wp-content/uploads/2017/04/Its-Raining-Pixels.mp3', 'http://soundimage.org/wp-content/uploads/2017/03/Arcade-Heroes.mp3', 'http://soundimage.org/wp-content/uploads/2017/03/Pixel-Puppies.mp3'];
let playlistIndex = 0;
audioButton.addEventListener('click', () => {
audio.src = `${playlist[playlistIndex]}`;
playlistIndex++;
if (playlistIndex === playlist.length) {
playlistIndex = 0;
}
});
let rainbowIndex = 0;
let eraserBackgroundCanvas = {};
let mousedown = 0;
document.body.onmousedown = function(){
mousedown++;
};
document.body.onmouseup = function(){
mousedown = 0;
};
document.body.oncontextmenu = function(){
mousedown = 0;
};
if (!localStorage.getItem('ACTIVEIMAGE')) {
localStorage.removeItem('ACTIVEIMAGE');
} else {
const activeImage = getStorage('ACTIVEIMAGE');
const activeImageObject = getStorage(activeImage);
eraserBackgroundCanvas = createImage('eraser background', activeImageObject.height, activeImageObject.width);
welcomeModule.classList.add('hidden');
header.classList.remove('hidden');
canvasModule.classList.remove('hidden');
displayTitle.textContent = activeImageObject.title;
renderImage(activeImageObject);
let canvasDivs = document.querySelectorAll('.pixel-div');
rainbowIndex = 0;
let mousedown = 0;
document.body.onmousedown = function(){
mousedown++;
};
document.body.onmouseup = function(){
mousedown = 0;
};
document.body.oncontextmenu = function(){
mousedown = 0;
};
for (let i = 0; i < canvasDivs.length; i++) {
canvasDivs[i].addEventListener('mouseenter', () => {
const selectedTool = document.querySelector(
'input[type=radio]:checked'
);
if (mousedown) {
if (selectedTool.id === 'pencil') {
canvasDivs[i].style.backgroundColor = colorSelect.value;
} else if (selectedTool.id === 'eraser') {
canvasDivs[i].style.backgroundColor = eraserBackgroundCanvas.colors[i];
} else if (selectedTool.id === 'rainbow') {
if (rainbowIndex === 7) {
rainbowIndex = 0;
canvasDivs[i].style.backgroundColor = rainbowArray[rainbowIndex];
rainbowIndex++;
} else {
canvasDivs[i].style.backgroundColor = rainbowArray[rainbowIndex];
rainbowIndex++;
} }
}});
canvasDivs[i].addEventListener('click', () => {
const selectedTool = document.querySelector(
'input[type=radio]:checked'
);
if (selectedTool.id === 'pencil') {
canvasDivs[i].style.backgroundColor = colorSelect.value;
} else if (selectedTool.id === 'eraser') {
canvasDivs[i].style.backgroundColor = eraserBackgroundCanvas.colors[i];
} else if (selectedTool.id === 'rainbow') {
if (rainbowIndex === 7) {
rainbowIndex = 0;
canvasDivs[i].style.backgroundColor = rainbowArray[rainbowIndex];
rainbowIndex++;
} else {
canvasDivs[i].style.backgroundColor = rainbowArray[rainbowIndex];
rainbowIndex++;
} }
});
}
pencil.checked = true;
}
let canvasDivs = document.querySelectorAll('.pixel-div');
homePageLogo.addEventListener('click', () => {
localStorage.removeItem('ACTIVEIMAGE');
location.reload();
});
startBtn.addEventListener('click', () => {
let alreadyExists = false;
let keysObject = getStorage('KEYS') || { keys: [] };
let keys = keysObject.keys;
let titleString = artworkTitle.value;
const title = titleKey(titleString);
for (let key of keys) {
if (title === key) {
alreadyExists = true;
}
}
if (alreadyExists) {
alert('Image title already in use. Please choose a new image title.');
} else if (title === '') {
alert('Please enter an image title.');
} else if (heightInput.value > 50) {
alert('Please enter a height and width below 50.');
} else if (widthInput.value > 50) {
alert('Please enter a height and width below 50.');
} else {
eraserBackgroundCanvas = createImage('eraser background', Number(heightInput.value), Number(widthInput.value));
const newImage = createImage(titleString, Number(heightInput.value), Number(widthInput.value));
renderImage(newImage);
setStorage(title, newImage);
const keyArrayObject = getStorage('KEYS') || { keys: [] };
const keyArray = keyArrayObject.keys;
keyArray.push(title);
setStorage('KEYS', keyArrayObject);
welcomeModule.classList.add('hidden');
header.classList.remove('hidden');
canvasModule.classList.remove('hidden');
displayTitle.textContent = titleString;
canvasDivs = document.querySelectorAll('.pixel-div');
for (let i = 0; i < canvasDivs.length; i++) {
canvasDivs[i].addEventListener('mouseenter', () => {
const selectedTool = document.querySelector(
'input[type=radio]:checked'
);
if (mousedown) {
if (selectedTool.id === 'pencil') {
canvasDivs[i].style.backgroundColor = colorSelect.value;
} else if (selectedTool.id === 'eraser') {
canvasDivs[i].style.backgroundColor = eraserBackgroundCanvas.colors[i];
} else if (selectedTool.id === 'rainbow') {
if (rainbowIndex === 7) {
rainbowIndex = 0;
canvasDivs[i].style.backgroundColor = rainbowArray[rainbowIndex];
rainbowIndex++;
} else {
canvasDivs[i].style.backgroundColor = rainbowArray[rainbowIndex];
rainbowIndex++;
} }
}});
canvasDivs[i].addEventListener('click', () => {
const selectedTool = document.querySelector(
'input[type=radio]:checked'
);
if (selectedTool.id === 'pencil') {
canvasDivs[i].style.backgroundColor = colorSelect.value;
} else if (selectedTool.id === 'eraser') {
canvasDivs[i].style.backgroundColor = eraserBackgroundCanvas.colors[i];
} else if (selectedTool.id === 'rainbow') {
if (rainbowIndex === 7) {
rainbowIndex = 0;
canvasDivs[i].style.backgroundColor = rainbowArray[rainbowIndex];
rainbowIndex++;
} else {
canvasDivs[i].style.backgroundColor = rainbowArray[rainbowIndex];
rainbowIndex++;
} }
});
}
pencil.checked = true;
}});
clearBtn.addEventListener('click', () => {
const imageObject = document.querySelectorAll('.pixel-div');
let objectArray = Array.from(imageObject);
for (let i = 0; i < objectArray.length; i++) {
objectArray[i].style.backgroundColor = eraserBackgroundCanvas.colors[i];
}
});
saveBtn.addEventListener('click', () => {
let key = titleKey(displayTitle.textContent);
imageObject = getStorage(key);
let colorArray = [];
for (let i = 0; i < canvasDivs.length; i++) {
colorArray.push(canvasDivs[i].style.backgroundColor);
}
const updatedImage = updateImage(imageObject, colorArray);
setStorage(key, updatedImage);
window.location.replace('./gallery/index.html');
});
paletteBtn.addEventListener('click', ()=>{
pencil.checked = true;
});
const renderedImg = document.getElementById('rendered-img');
const div = document.getElementById('canvas');
const downloadContainer = document.getElementById('png-container');
const saveDownload = document.getElementById('save-download');
function takeshot() {
window.scrollTo(0, 0);
html2canvas(div).then(
function(canvas) {
renderedImg.appendChild(canvas);
canvas.setAttribute('id', 'rendered');
});
}
const downloadBtn = document.getElementById('download-image');
downloadBtn.addEventListener('click', ()=>{
takeshot();
downloadContainer.classList.remove('hidden');
saveDownload.classList.remove('hidden');
});
saveDownload.addEventListener('click', ()=>{
const link = document.createElement('a');
link.download = 'download.png';
link.href = rendered.toDataURL();
link.click();
link.delete;
});