-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimagemaker.js
431 lines (400 loc) · 12.5 KB
/
imagemaker.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
window.addEventListener('load', function(ev) {
let parts = [];
// code below this line controls functionality
// dw about if you're just editing visual assets
/* relative path to the folder containing part folders */
const assetsPath = "imagemakerAssets/"
// DOM Elements
const canvas = document.getElementById("my-canvas-object");
const context = canvas.getContext('2d');
const WIDTH = canvas.width;
const HEIGHT = canvas.height;
const randomButton = document.getElementById("random_button");
const infoButton = document.getElementById("info_button");
const paletteButton = document.getElementById("palette_button");
const itemsButton = document.getElementById("items_button");
const saveButton = document.getElementById("save_button");
const loading = document.getElementById("loading");
/* 1d array of part select button DOM elements */
const partsElements = [];
/* 2d array of item select button DOM elements */
const itemsElements = [];
/* Render layers to this 1st and then canvas so that images render all at
once instead of one layer at a time */
const workingCanvas = document.createElement('canvas');
workingCanvas.height = HEIGHT;
workingCanvas.width = WIDTH;
const workingContext = workingCanvas.getContext('2d');
// global state variables
/* Is the extra info screen currently visible? */
let infoVisible = false;
/* Is the palette select menu visible and item select menu invisible? */
let paletteVisible = false;
/* Index of part whose menu is currently displayed */
let selectedPart = 0;
/* 1d array of colors where selectedColors[i] is the color selected for part i */
let selectedColors = []
/* 1d array of indices of items currently selected,
where selectedItemIndex[i] is the index of the selected item for of part i*/
let selectedItemIndex = []
/* 1d array of canvases of items currently selected,
where layerCanvases[i] depicts the selected item of part i in the selected color*/
const layerCanvases = [];
init();
async function init() {
await initParts();
initButtons();
initCanvases()
await initArrays();
initPalette();
await initItemFunctions();
await randomize();
await updateSelectedPart(0);
}
/**
* Fetch parts info from parts.json and initialize the parts variable.
*/
async function initParts (data) {
const response = await fetch("./parts.json");
const json = await response.json();
parts = json.parts;
}
/**
* Assign canvases to list of layer canvases
*/
function initCanvases() {
for (let partIdx = 0; partIdx < parts.length; partIdx++) {
let cnv = document.createElement('canvas');
cnv.height = HEIGHT;
cnv.width = WIDTH;
layerCanvases.push(cnv);
}
}
/**
* Assign functions to buttons.
*/
function initButtons() {
randomButton.addEventListener('click', randomize);
infoButton.addEventListener('click', toggleInfo);
paletteButton.addEventListener('click', togglePalette);
itemsButton.addEventListener('click', toggleItems);
return null;
}
/**
* Initialize partsElements, itemsElements
*/
async function initArrays() {
initPartsElements();
initItemsElements();
return null;
}
/**
* Create color select DOM elements for every part's colors
*/
function initPalette() {
for (let i = 0; i < parts.length; i++) {
for (let j = 0; j < parts[i].colors.length; j++) {
let colorElement = document.createElement('li');
colorElement.style.backgroundColor = "#" + parts[i].colors[j];
colorElement.addEventListener('click', function() {
selectColor(i, j);
});
colorElement.id = "color_" + i.toString() + "_" + j.toString();
colorElement.style.display = "none";
document.getElementById("colorpalette_list").appendChild(colorElement);
}
}
return null;
}
/**
* Update UI to visibly select a part and display that part's items
* @param {number} partId The id of the selected part
*/
async function updateSelectedPart(partId) {
selectedPart = partId;
for (let i = 0; i < parts.length; i++) {
if (i == partId) {
partsElements[i].classList.add('selected');
}
else {
partsElements[i].classList.remove('selected');;
}
for (let j = 0; j < (parts[i].items.length + Number(parts[i].noneAllowed)); j++) {
if (i == partId) {
itemsElements[i][j].style.display = "inline-flex";
}
else {
itemsElements[i][j].style.display = "none";
}
}
}
if (parts[partId].colors.length === 0) {
paletteButton.style.display = "none";
}
else {
paletteButton.style.display = "inline-flex";
}
updatePalette();
toggleItems();
return null;
}
/**
* Display image with randomly selected items
*/
async function randomize() {
for (let i = 0; i < parts.length; i++) {
let noneCount = Number(parts[i].noneAllowed);
let itemRange = parts[i].items.length + noneCount;
let itemIndex = Math.floor(Math.random() * itemRange);
let colorRange = parts[i].colors.length;
let colorIndex = Math.floor(Math.random() * colorRange);
selectedColors[i] = colorIndex;
if (noneCount > 0 && itemIndex === 0) {
selectedItemIndex[i] = null;
}
else {
selectedItemIndex[i] = itemIndex - noneCount;
}
for (j = 0; j < itemRange; j++) {
if (j == itemIndex) {
itemsElements[i][j].classList.add("selected");
}
else {
itemsElements[i][j].classList.remove("selected");
}
}
}
await renderLayerStack();
return null;
}
/**
* Assign item select callback functions to partsElements and itemsElements members
*/
async function initItemFunctions() {
for (let i = 0; i < parts.length; i++) {
partsElements[i].addEventListener('click', function() {
updateSelectedPart(i);
});
for (let j = 0; j < (parts[i].items.length + Number(parts[i].noneAllowed)); j++) {
itemsElements[i][j].addEventListener('click', function() {
updateSelectedItem(i, j);
});
}
}
return null;
}
/**
* Render Images in layerStack to canvas and update save URL
*/
async function renderLayerStack() {
clearCanvas(workingCanvas);
let timer = setTimeout(function(){ loading.style.display = "block"; }, 500);
for (let partId = 0; partId < parts.length; partId++) {
clearCanvas(layerCanvases[partId]);
if (selectedItemIndex[partId] !== null) {
await imageFromIndex(partId, selectedItemIndex[partId], selectedColors[partId]);
}
workingContext.drawImage(layerCanvases[partId], 0, 0);
}
clearCanvas(canvas);
clearTimeout(timer);
loading.style.display = "none";
context.drawImage(workingCanvas, 0, 0);
await updateSave();
return null;
}
/**
* Initialize partsElements
*/
function initPartsElements() {
for (let i = 0; i < parts.length; i++) {
let part = document.createElement('li');
let partIcon = document.createElement('img');
partIcon.src = assetsPath + parts[i].folder + "/icon.png";
part.appendChild(partIcon);
part.id = "part_" + i.toString();
document.getElementById('parts_list').appendChild(part);
partsElements[i] = part;
}
return null;
}
/**
* Initialize itemsElements
*/
function initItemsElements() {
for (let i = 0; i < parts.length; i++) {
itemsElements.push([]);
for (let j = 0; j < parts[i].items.length; j++) {
itemsElements[i].push(null);
}
}
for (let i = 0; i < parts.length; i++) {
if (parts[i].noneAllowed) {
let noneButton = document.createElement('li');
let noneButtonIcon = document.createElement('img');
noneButtonIcon.src = assetsPath + "none_button.svg";
noneButton.appendChild(noneButtonIcon);
document.getElementById("itemlist_list").appendChild(noneButton);
noneButton.style.display = "none";
itemsElements[i][0] = noneButton;
}
for (let j = 0; j < parts[i].items.length; j++) {
let item = document.createElement('li');
let itemIcon = document.createElement('img');
itemIcon.id = "icon_" + i.toString() + "_" + j.toString();
itemIcon.src = (assetsPath +
parts[i].folder + "/" +
parts[i].items[j] + ".png");
item.appendChild(itemIcon);
item.id = "item_" + i.toString() + "_" + j.toString();
item.style.display = "none";
document.getElementById("itemlist_list").appendChild(item);
itemsElements[i][j + Number(parts[i].noneAllowed)] = item;
}
}
return null;
}
function clearCanvas(canvas) {
return (canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height));
}
/**
* Update UI to visibly select a parts[partId].items[itemId] and render it to the canvas
*/
async function updateSelectedItem(partId, itemId) {
for (let j = 0; j < (parts[partId].items.length + Number(parts[partId].noneAllowed)); j++) {
if (j == itemId) {
itemsElements[partId][j].classList.add("selected");
}
else {
itemsElements[partId][j].classList.remove("selected");
}
}
let selectedNone = (parts[partId].noneAllowed && itemId == 0);
if (selectedNone) {
selectedItemIndex[partId] = null;
}
else {
selectedItemIndex[partId] = itemId - Number(parts[partId].noneAllowed);
}
await renderLayerStack();
return null;
}
/**
* Update download save button with latest version of the canvas
*/
async function updateSave() {
save.href = canvas.toDataURL("image/png");
return null;
}
/**
* Create a new Image from a path
*
* @image {string} the path to the Image source .png
*/
async function newLayer(image) {
let myLayer;
if (image === null) {
myLayer = null;
}
else {
myLayer = await(loadImage(image));
}
return myLayer;
}
/**
* Force newLayer to wait until an image is fully loaded before assigning it to layerStack
*
* @path {string} the path to the Image source .png
*/
function loadImage(path) {
return new Promise(resolve => {
const image = new Image();
image.addEventListener('load', () => {
resolve(image);
});
image.src = path;
});
}
/**
* Display info menu if it's visible, hide it if it's invisible
*/
function toggleInfo() {
let infoWrap = document.getElementById("info_wrap");
if (infoVisible) {
infoWrap.style.display = "none";
infoVisible = false;
infoButton.textContent = "?";
}
else {
infoWrap.style.display = "block";
infoVisible = true;
infoButton.textContent = "X";
}
}
/**
* Display palette of selectedPart
*/
function updatePalette() {
for (let i = 0; i < parts.length; i++) {
for (let j = 0; j < parts[i].colors.length; j++) {
if (i === selectedPart) {
document.getElementById("color_" + i.toString()
+ "_" + j.toString()).style.display = "inline-block";
}
else {
document.getElementById("color_" + i.toString()
+ "_" + j.toString()).style.display = "none";
}
}
}
}
/**
* Display palette menu, hide item menu
*/
function togglePalette() {
paletteVisible = true;
document.getElementById("imagemaker_colorpalette").style.display = "flex";
document.getElementById("imagemaker_itemlist").style.display = "none";
}
/**
* Display item menu, hide palette menu
*/
function toggleItems() {
paletteVisible = false;
document.getElementById("imagemaker_colorpalette").style.display = "none";
document.getElementById("imagemaker_itemlist").style.display = "flex";
}
/**
* Change the color of the item selected for part[partId] to part[partId].colors[colorId]
*/
async function selectColor(partId, colorId) {
selectedColors[partId] = colorId;
if (selectedItemIndex[partId] != null) {
await renderLayerStack();
}
return null;
}
/**
* Render parts[partIndex].items[itemIndex] in color
* parts[partIndex].colors[colorIndex] to layerCanvases[partIndex]
*/
async function imageFromIndex(partIndex, itemIndex, colorIndex) {
let imgPath = (parts[partIndex].colors.length > 0)
?
(assetsPath +
parts[partIndex].folder + "/" +
parts[partIndex].items[itemIndex] + "_" +
parts[partIndex].colors[colorIndex] +
".png")
:
(assetsPath +
parts[partIndex].folder + "/" +
parts[partIndex].items[itemIndex] +
".png")
;
let img = await(loadImage(imgPath));
clearCanvas(layerCanvases[partIndex]);
ctx = layerCanvases[partIndex].getContext('2d');
ctx.drawImage(img, 0, 0);
}
} ,false);