-
Notifications
You must be signed in to change notification settings - Fork 4
/
glitchatron.js
583 lines (411 loc) · 15.6 KB
/
glitchatron.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
(function(){
var glitchatron = {
//Maximum image size
max_width: 1024,
max_height: 768,
//Init image dimensions
img_width: 0,
img_height: 0,
//canvas elements
unglitched_canvas: document.getElementById('unglitched_canvas'),
glitched_canvas: document.getElementById('glitched_canvas'),
//save button
save_button: document.getElementById('saveButton'),
save_button_enabled: false,
//init option vars
glitch_level: 0,
mask_option: '',
text_overlay: '',
text_colour: 'white',
text_align: 'mc',
text_font_size: 74,
options_displayed: false,
glitchometer: 0,
file_changed: true,
//----------------------------------------------------
init: function() {
//Bind buttons, menu actions etc
document.getElementById("glitchButton").addEventListener("click", glitchatron.glitch);
document.getElementById("saveButton").addEventListener("click", glitchatron.saveGlitch);
document.getElementById("files").addEventListener("change", glitchatron.setFileChanged);
document.getElementById("glitch_level").addEventListener("change", glitchatron.setGlitchLevel);
document.getElementById("mask_option").addEventListener("change", glitchatron.setMaskOption);
document.getElementById("text_overlay").addEventListener("change", glitchatron.setTextOverlay);
document.getElementById("text_colour").addEventListener("change", glitchatron.setTextColour);
document.getElementById("text_align").addEventListener("change", glitchatron.setTextAlign);
document.getElementById("text_font_size").addEventListener("change", glitchatron.setTextFontSize);
document.getElementById("more_text_options").addEventListener("click", glitchatron.toggleTextOptions);
//Init options
glitchatron.glitch_level = document.getElementById("glitch_level").value;
glitchatron.mask_option = document.getElementById("mask_option").value;
glitchatron.text_overlay = document.getElementById("text_overlay").value;
glitchatron.text_colour = document.getElementById("text_colour").value;
glitchatron.text_align = document.getElementById("text_align").value;
glitchatron.text_font_size = document.getElementById("text_font_size").value;
//Show options if file is already selected on load (eg, firefox refresh)
if(document.getElementById("files").files.length){
glitchatron.fadeIn(document.getElementById("options_container"));
}
},
//----------------------------------------------------
loadImage: function() {
//Loading cursor
document.body.style.cursor = 'progress';
//Get the file
var files = document.getElementById('files').files;
if (!files.length) {
alert('Please select a file!!');
return;
}
var file = files[0];
//Check type
if (file.type != "image/jpeg" && file.type != "image/jpg") {
alert('Only jpg images are allowed. This image is a ' + file.type);
return;
}
//Read file
var reader = new FileReader();
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) {
//Get file data
var data = evt.target.result;
//Setup unglitched canvas
var canvas = glitchatron.unglitched_canvas;
var ctx = canvas.getContext('2d');
//create image, base64 encode image data, resize and draw into canvas
var img = new Image;
img.src = 'data:image/jpeg;base64,' + btoa(data);
img.onload = function(){
//Set the width / height of the image according to max width/height settings
glitchatron.img_width = img.width;
glitchatron.img_height = img.height;
if (glitchatron.img_width > glitchatron.img_height) {
if (glitchatron.img_width > glitchatron.max_width) {
glitchatron.img_height *= glitchatron.max_width / glitchatron.img_width;
glitchatron.img_width = glitchatron.max_width;
}
} else {
if (glitchatron.img_height > glitchatron.max_height) {
glitchatron.img_width *= glitchatron.max_height / glitchatron.img_height;
glitchatron.img_height = glitchatron.max_height;
}
}
ctx.canvas.width = glitchatron.img_width;
ctx.canvas.height= glitchatron.img_height;
//Draw the unglitched image
ctx.drawImage(img,0,0,glitchatron.img_width,glitchatron.img_height);
console.log('Image Loaded');
glitchatron.glitchImage();
glitchatron.file_changed = false;
//Default cursor
document.body.style.cursor = 'default';
}; //End img.onload
} //end filereader.done
} //end onloadend
reader.readAsBinaryString(file);
}, //end loadimage function
//----------------------------------------------------
glitchImage: function(){
//Loading cursor
document.body.style.cursor = 'progress';
//Char Sets (these characters are found and changed around in the raw image data)
var chars1 = ["0",")","<",">",".","*","&","£","%","~","#","+","a","!","|","-"];
var chars2 = ["a","b","c","d","e","f","z","x","v","n","m","o","i","y","q","w"];
//Select one of the character sets to use
if( Math.floor((Math.random()*2)+1) == 2 )
{ var chars = chars2; }
else
{ var chars = chars1; }
//Check the level of glitch selected
var glitch_levels = ["64","128","256","1024","1024"];
var optionVal = glitchatron.glitch_level;
var splitNum = glitch_levels[optionVal];
//Setup glitched canvas - same dimensions as set by loadImage()
var canvas = glitchatron.glitched_canvas;
var ctx = glitched_canvas.getContext('2d');
ctx.canvas.width = glitchatron.img_width;
ctx.canvas.height = glitchatron.img_height;
//Create mask if required
glitchatron.createMask();
//Get dataUrl from unglitched canvas
var dataurl = glitchatron.unglitched_canvas.toDataURL("image/jpeg");
//Remove the dataurl bit from the start and then base64 unencode
var newdata = dataurl.replace('data:image/jpeg;base64,','');
newdata = atob(newdata);
//Get length of each chunk
var chunkLength = parseInt( (newdata.length - 1) / splitNum);
//put data into chunks
var chunks = [];
for (var i = 0, charsLength = newdata.length; i < charsLength; i += chunkLength) {
chunks.push(newdata.substring(i, i + chunkLength));
}
//Loop through chunks, leaving out the header chunk
for(var i=2;i<=splitNum;i++) {
//Create random number for the glitch decision
var glitchRand = Math.floor((Math.random()*100)+1);
//If the extreme setting is chosen, make this number always odd so glitch is applied to every piece
if(optionVal == 4) {
glitchRand = 1;
}
//create random numbers for selection of the glitch characters
var char1Rand = Math.floor((Math.random()*chars.length));
var char2Rand = Math.floor((Math.random()*chars.length));
if (char2Rand == char1Rand) {
char2Rand = "9";
}
//If random number is odd
if(glitchRand % 2 != 0) {
//glitch the chunk
chunks[i] = chunks[i].replace(chars[char1Rand],chars[char2Rand]);
//Add 1 to the glitchOmeter
glitchatron.glitchOmeter = glitchatron.glitchOmeter + 1;
}
} //End chunk loop
//recombine the chunks to get the glitched data
newdata = chunks.join('');
//Base64 encode the glitched data
var base64data = btoa(newdata);
//Put the glitched image into the glitched canvas
glitched_img = new Image();
glitched_img.src = 'data:image/jpeg;base64,' + base64data;
glitched_img.onload = function() {
ctx.drawImage(glitched_img, 0, 0, ctx.canvas.width, ctx.canvas.height);
glitchatron.createText();
//Enable save button
if(!glitchatron.save_button_enabled) {
var saveButtonContainer = document.getElementById("saveButtonContainer");
glitchatron.fadeIn(saveButtonContainer);
glitchatron.save_button_enabled = true;
}
console.log('GLITCH COMPLETE');
//Default cursor
document.body.style.cursor = 'default';
}
}, //End glitchImage function
//----------------------------------------------------
createMask: function(){
//var optionVal = document.getElementById("glitch_level").value;
if(glitchatron.mask_option == '') {
return;
}
var shape = glitchatron.mask_option;
var canvas = glitchatron.glitched_canvas;
var ctx = canvas.getContext('2d');
//Circle Mask
if(shape == 'circle') {
if(canvas.width > canvas.height) {
var radius = canvas.height / 2;
} else {
var radius = canvas.width / 2;
}
//Resize Canvas to the same size as the circle
ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2); //centre point
ctx.canvas.width = radius*2;
ctx.canvas.height= radius*2;
ctx.arc(ctx.canvas.width/2, ctx.canvas.height/2, radius, 0, 2 * Math.PI, false);
ctx.clip();
console.log('Circle Mask Created');
} //End Circle Mask
//Triangle Mask
if(shape == 'triangle') {
//Work out best side length
if(canvas.width > canvas.height) {
var sideLength = canvas.height;
} else {
var sideLength = canvas.width;
}
var h = sideLength * (Math.sqrt(3)/2);
//Resize Canvas to the same size as triangle
ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2); //centre point
ctx.canvas.width = sideLength;
ctx.canvas.height= sideLength - (h/7);
var halfWidth = canvas.width / 2;
var halfHeight = canvas.height / 2;
ctx.beginPath();
ctx.moveTo(halfWidth, halfHeight - (h/2) ) ;
ctx.lineTo( halfWidth - (sideLength / 2), halfHeight + (h/2) );
ctx.lineTo(halfWidth + (sideLength / 2), halfHeight + (h/2) );
ctx.lineTo(halfWidth, halfHeight - (h/2) );
ctx.closePath();
ctx.clip();
} //End Triangle Mask
//Triangle 2 Mask
if(shape == 'triangle2') {
//Work out best side length
if(canvas.width > canvas.height) {
var sideLength = canvas.height;
} else {
var sideLength = canvas.width;
}
var h = sideLength * (Math.sqrt(3)/2);
//Resize Canvas to the same size as triangle
ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2); //centre point
ctx.canvas.width = sideLength;
ctx.canvas.height= sideLength - (h/7);
var halfWidth = canvas.width / 2;
var halfHeight = canvas.height / 2;
ctx.beginPath();
ctx.moveTo(halfWidth, halfHeight + (h/2) ) ;
ctx.lineTo( halfWidth - (sideLength / 2), halfHeight - (h/2) );
ctx.lineTo(halfWidth + (sideLength / 2), halfHeight - (h/2) );
ctx.lineTo(halfWidth, halfHeight + (h/2) );
ctx.closePath();
ctx.clip();
} //End Triangle2 Mask
},
//----------------------------------------------------
createText: function(){
var text_content = glitchatron.text_overlay;
if(text_content != ''){
var canvas = glitchatron.glitched_canvas;
var ctx = canvas.getContext('2d');
var h_align = 0;
var v_align = 0;
var text_align = 'start';
var padding = 16;
var base_line = 'middle';
//Set text alignment vars
if(glitchatron.text_align == 'tl'){
h_align = padding;
v_align = padding;
base_line = 'top';
}
else if(glitchatron.text_align == 'tc'){
h_align = ctx.canvas.width/2;
v_align = padding;
text_align = 'center';
base_line = 'top';
}
else if(glitchatron.text_align == 'tr'){
h_align = ctx.canvas.width - padding;
v_align = padding;
text_align = 'end';
base_line = 'top';
}
else if(glitchatron.text_align == 'ml'){
h_align = padding;
v_align = ctx.canvas.height/2;
text_align = 'start';
base_line = 'middle';
}
else if(glitchatron.text_align == 'mc'){
h_align = ctx.canvas.width/2;
v_align = (ctx.canvas.height/2);
text_align = 'center';
base_line = 'middle';
}
else if(glitchatron.text_align == 'mr'){
h_align = ctx.canvas.width - padding;
v_align = (ctx.canvas.height/2);
text_align = 'end';
base_line = 'middle';
}
else if(glitchatron.text_align == 'bl'){
h_align = padding;
v_align = ctx.canvas.height;
text_align = 'start';
base_line = 'bottom';
}
else if(glitchatron.text_align == 'bc'){
h_align = ctx.canvas.width/2;
v_align = ctx.canvas.height;
text_align = 'center';
base_line = 'bottom';
}
else if(glitchatron.text_align == 'br'){
h_align = ctx.canvas.width - padding;
v_align = ctx.canvas.height;
text_align = 'end';
base_line = 'bottom';
}
ctx.textAlign = text_align;
ctx.textBaseline = base_line;
ctx.font="bold " + glitchatron.text_font_size + "px sans-serif";
ctx.fillStyle = glitchatron.text_colour;
ctx.fillText(text_content,h_align,v_align);
console.log('TEXT DRAWN');
}
},
//----------------------------------------------------
saveGlitch: function(){
//Enable save button
document.body.style.cursor = 'progress';
var savedata = glitchatron.glitched_canvas.toDataURL("image/png");
var timestamp = new Date().getTime();
glitchatron.save_button.href = savedata;
glitchatron.save_button.download = 'Glitchatron_' + timestamp + '.png';
document.body.style.cursor = 'default';
},
//----------------------------------------------------
setGlitchLevel: function(){
glitchatron.glitch_level = document.getElementById("glitch_level").value;
},
//----------------------------------------------------
setMaskOption: function(){
glitchatron.mask_option = document.getElementById("mask_option").value;
},
//----------------------------------------------------
setFileChanged: function(){
glitchatron.file_changed = true;
if(!glitchatron.options_displayed){
glitchatron.fadeIn(document.getElementById("options_container"));
glitchatron.options_displayed = true;
}
},
//----------------------------------------------------
setTextOverlay: function(){
glitchatron.text_overlay = document.getElementById("text_overlay").value;
},
//----------------------------------------------------
setTextColour: function(){
glitchatron.text_colour = document.getElementById("text_colour").value;
},
//----------------------------------------------------
setTextAlign: function(){
glitchatron.text_align = document.getElementById("text_align").value;
},
//----------------------------------------------------
setTextFontSize: function(){
glitchatron.text_font_size = document.getElementById("text_font_size").value;
},
//----------------------------------------------------
toggleTextOptions: function(){
var container = document.getElementById("text_options_container");
var link = document.getElementById("more_text_options");
if(container.className == 'hide') {
container.className = '';
glitchatron.fadeIn(container);
link.innerHTML = '▲ Hide Text Options';
} else {
container.className = 'hide';
link.innerHTML = '▼ Text Options';
}
},
//----------------------------------------------------
fadeIn: function(el) {
el.style.display = 'block';
el.style.opacity = 0;
var last = +new Date();
var tick = function() {
el.style.opacity = +el.style.opacity + (new Date() - last) / 400;
last = +new Date();
if (+el.style.opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16)
}
};
tick();
},
//----------------------------------------------------
glitch: function(){
//If a new file has been selected call loadImage, otherwise call glitchImage and use currently loaded image
if(glitchatron.file_changed){
glitchatron.loadImage();
} else {
glitchatron.glitchImage();
}
},
//----------------------------------------------------
};
//Run!
glitchatron.init();
}());