-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsus.js
352 lines (280 loc) · 10 KB
/
sus.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
import Pixel from '/pixel.js';
// -------------- Caching sprites at page load ------------
/*
cache to hold the different "positions" of impostors so we don't
have to request them from the server every time we color a new impostor
*/
var sprites = {};
var masks;
window.onload = async function() {
// display welcome "alert" on pageload if they haven't seen it before
if (!localStorage.getItem("dontDisplayWelcomeAlert")) {
const { value: checkboxes } = await Swal.fire({
title: "Welcome to DumpyGif!",
icon: "info",
html: `
You can use this website to generate a gif of impostors (like pixel art but with impostors) based on an uploaded image. (This website works best on a PC running Chrome. Not fully tested on Safari/iOS yet, but it might work.)
<br><br>
To view examples and learn what the different settings mean, check out our <a class="alertLink" href="https://github.com/Walker30263/dumpygif" target="_blank" rel="noopener noreferrer">GitHub Repository</a>! You can also find this at any time by clicking the icon on the bottom right of the page.
<br><br>
<input checked id="notificationToggle" type="checkbox" class="welcomeInput">
<label for="notificationToggle" class="welcomeInputLabel">
Allow Notifications (since sometimes it might take a while for gifs to generate)
</label>
<br><br>
<input id="neverSeeThisAgain" type="checkbox" class="welcomeInput">
<label for="neverSeeThisAgain" class="welcomeInputLabel">
Never see this Welcome message again
</label>
`,
focusConfirm: false,
preConfirm: () => {
return [
document.getElementById("notificationToggle").checked,
document.getElementById("neverSeeThisAgain").checked
]
},
iconColor: "#ffffff",
background: "#c51111",
color: "#ffffff"
});
if (checkboxes[0] === true) { // they want notifications
localStorage.setItem("notifications", true);
}
if (checkboxes[1] === true) { // they never want to see this alert again
localStorage.setItem("dontDisplayWelcomeAlert", true);
}
}
for (let i = 1; i <= 6; i++) {
let impostor = await loadImageBitmap(`assets/gif-images/${i}.png`);
sprites[i] = impostor;
}
//if they want notifs and their browser supports notifs and they haven't granted us permission yet to send notifs
if ((localStorage.getItem("notifications") == "true") && (typeof Notification !== "undefined") && (Notification.permission !== "granted")) {
Notification.requestPermission().then(permission => {
if (permission === "granted") {
let notif = new Notification("Thank you for giving us permission to send notifications!", {
body: "We promise not to spam you! We will only send notifs if you're on another tab/window when a gif is finished.",
icon: "assets/logo.png"
});
console.log(notif);
}
});
}
masks = await loadMasks();
btnGenerate.textContent = "Generate";
btnGenerate.disabled = false;
}
async function loadMasks() {
const response = await fetch("assets/masks.json");
const data = response.json();
return data;
}
async function loadImageBitmap(imageUrl) {
try {
const response = await fetch(imageUrl);
const blob = await response.blob();
const imageBitmap = await createImageBitmap(blob);
return imageBitmap;
} catch(error) {
console.error(error);
return null;
}
}
let status = document.getElementById("status");
class Dumpy {
constructor(workers, quality) {
this.gif = new GIF({
workers: workers,
quality: quality
});
}
//units: pixels
setOriginalDimensions(height, width) {
this.originalHeight = height;
this.originalWidth = width;
}
//units: number of impostors
setDimensions(height, width) {
this.height = height;
this.width = width;
this.impostorHeight = this.originalHeight*this.multiplier/height;
this.impostorWidth = this.originalWidth*this.multiplier/width;
}
// increases the output image size by a factor of "factor" compared to the image that was uploaded
setMultiplier(factor) {
this.multiplier = factor;
}
setInterval(interval) {
this.interval = interval;
}
setChoreographySettings(data) {
this.choreographySettings = data;
}
setColorCollapseSettings(data) {
this.colorCollapseSettings = data;
}
setPixels(data) {
this.pixels = data;
let totalUniqueColors = getTotalUniqueColors(this.pixels);
let numberOfUniqueColors = totalUniqueColors.size;
if (this.colorCollapseSettings.collapse) {
let N = Math.round(numberOfUniqueColors * (1 - (this.colorCollapseSettings.compressionPercent / 100)));
//prevent user from compressing image too much
if (N < 70) {
N = numberOfUniqueColors;
}
let topNColors = colorTokenizer(this.pixels, N);
this.pixels = snapImageToNearestColor(this.pixels, topNColors); // bro whaasdlkdsfj
topNColors = topNColors.map(color => JSON.stringify(color));
return this.generateFrames(topNColors);
} else {
return this.generateFrames(Array.from(totalUniqueColors));
}
}
async generateFrames(colors) {
return new Promise(async (resolve) => {
const seedWorker = new Worker("seedBaker.js");
status.textContent = "Baking Animations...";
seedWorker.postMessage([this.height, this.width, this.choreographySettings]);
let dumpy = this;
seedWorker.addEventListener("message", async function(e) {
seedWorker.terminate();
//for status purposes
let totalNumberOfColors = colors.length*6;
let impostorColorsMade = 0;
let framesCreated = 0;
let seed = e.data; //seed from the seed worker
const frameMaker = new Worker("frameMaker.js");
frameMaker.postMessage({
sprites: sprites,
masks: masks,
colors: colors,
frameGenerationData: {
seed: seed,
pixels: dumpy.pixels,
originalWidth: dumpy.originalWidth,
originalHeight: dumpy.originalHeight,
multiplier: dumpy.multiplier,
height: dumpy.height,
width: dumpy.width,
impostorWidth: dumpy.impostorWidth,
impostorHeight: dumpy.impostorHeight
}
});
frameMaker.onmessage = async (e) => {
if (e.data.finished) {
if (e.data.taskFinishedWith === "creatingImpostors") {
status.textContent = `Finished coloring impostors!`;
} else if (e.data.taskFinishedWith === "creatingFrames") {
frameMaker.terminate();
}
} else {
if (e.data.newImpostorsColored) {
impostorColorsMade += 6;
status.textContent = `Coloring impostors... ${impostorColorsMade}/${totalNumberOfColors}`;
} else if (e.data.newFrame) {
let frame = await convertImageBitmapToImage(e.data.newFrame);
dumpy.gif.addFrame(frame, {
delay: dumpy.interval
});
framesCreated++;
status.textContent = `Creating frames...${framesCreated}/6`;
if (framesCreated === 6) {
resolve();
}
}
}
}
});
});
}
async generateGif() {
return new Promise((resolve, reject) => {
this.gif.on('finished', function(blob) {
if (document.visibilityState !== "visible") {
var notification = new Notification("Finished!", {
body: "Your gif has been generated!",
icon: "assets/logo.png"
});
notification.onclick = function() {
window.parent.focus();
notification.close();
}
let notifSound = new Audio("assets/notifSound.mp3");
notifSound.play();
}
resolve(blob);
});
this.gif.render();
});
}
}
async function convertImageBitmapToImage(imageBitmap) {
return new Promise((resolve, reject) => {
let image = new Image();
let canvas = document.createElement('canvas');
let context = canvas.getContext('2d');
canvas.width = imageBitmap.width;
canvas.height = imageBitmap.height;
context.drawImage(imageBitmap, 0, 0);
image.onload = () => {
resolve(image);
};
image.src = canvas.toDataURL();
});
}
export default Dumpy;
// ---------------------------------------------- color collapse -----------------------------------------
//gets the unique colors in the pixels array (which is the expected input)
function getTotalUniqueColors(arr) {
let newArr = arr.map((el) => {
return JSON.stringify(el);
});
return new Set(newArr);
}
function colorTokenizer(image, N) {
let dictionary = {};
for (var i = 0; i < image.length; i++) {
let color = JSON.stringify(image[i]);
if (dictionary[color] == undefined) {
dictionary[color] = 1;
} else {
dictionary[color] += 1;
}
}
// get the top N colors
var topN = [];
for (var key in dictionary) {
topN.push(key);
}
topN.sort(function(a, b) {
return dictionary[b] - dictionary[a];
});
topN = topN.slice(0, N);
topN = topN.map(x => new Pixel(x)) // JSON string -> Pixel
return topN;
}
function snapImageToNearestColor(image, colors) {
let newImage = [];
for (let i = 0; i < image.length; i++) {
newImage.push(snapToNearestColor(image[i], colors));
}
return newImage;
}
function snapToNearestColor(pixel, colors) {
// check if the pixel is already in the list of colors
if (colors.includes(pixel)) {
return pixel;
}
let minDistance = 765; // start out at maximum distance (Pixel.distance between white and black)
let closestColor = null;
for (let i = 0; i < colors.length; i++) {
let distance = Pixel.distance(pixel, colors[i]);
if (distance < minDistance) {
minDistance = distance;
closestColor = colors[i];
}
}
return closestColor;
}