-
Notifications
You must be signed in to change notification settings - Fork 0
/
glitchjpeg.browser.js
234 lines (201 loc) · 5.99 KB
/
glitchjpeg.browser.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
(function (exports) {
'use strict';
var base64Map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
var reverseBase64Map = reverseArrToObj(base64Map);
var attachedImages = void 0;
function checkCompat() {
var hasQS = 'querySelectorAll' in document;
var hasCanvas = false;
try {
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
hasCanvas = !!context;
} catch (e) {}
return hasQS && hasCanvas;
}
function reverseArrToObj(arr) {
var obj = {};
arr.forEach(function (val, key) {
obj[val] = key;
});
return obj;
}
function detectJpegHeaderSize(data) {
var jpgHeaderLength = 417;
for (var i = 0, l = data.length; i < l; i++) {
if (data[i] == 0xFF && data[i + 1] == 0xDA) {
jpgHeaderLength = i + 2;
return jpgHeaderLength;
}
}
return jpgHeaderLength;
}
// base64 is 2^6, byte is 2^8, every 4 base64 values create three bytes
function base64ToByteArray(str) {
var result = [],
digitNum,
cur,
prev;
for (var i = 23, l = str.length; i < l; i++) {
cur = reverseBase64Map[str.charAt(i)];
digitNum = (i - 23) % 4;
switch (digitNum) {
//case 0: first digit - do nothing, not enough info to work with
case 1:
//second digit
result.push(prev << 2 | cur >> 4);
break;
case 2:
//third digit
result.push((prev & 0x0f) << 4 | cur >> 2);
break;
case 3:
//fourth digit
result.push((prev & 3) << 6 | cur);
break;
}
prev = cur;
}
return result;
}
function byteArrayToBase64(arr) {
var result = ["data:image/jpeg;base64,"],
byteNum,
cur,
prev;
for (var i = 0, l = arr.length; i < l; i++) {
cur = arr[i];
byteNum = i % 3;
switch (byteNum) {
case 0:
//first byte
result.push(base64Map[cur >> 2]);
break;
case 1:
//second byte
result.push(base64Map[(prev & 3) << 4 | cur >> 4]);
break;
case 2:
//third byte
result.push(base64Map[(prev & 0x0f) << 2 | cur >> 6]);
result.push(base64Map[cur & 0x3f]);
break;
}
prev = cur;
}
if (byteNum === 0) {
result.push(base64Map[(prev & 3) << 4]);
result.push("==");
} else if (byteNum == 1) {
result.push(base64Map[(prev & 0x0f) << 2]);
result.push("=");
}
return result.join("");
}
function getCorruptedByteArray(img, percentVal) {
var newByteArray = img.byteArray.slice();
for (var i = 0; i < 10; i++) {
var offset = Math.floor(img.headerLength + Math.random() * (newByteArray.length - img.headerLength - 4));
var newByte = Math.floor(Math.random() * 256);
newByteArray[offset] = newByte;
}
return newByteArray;
}
function glitch(img, percentVal) {
var imageData = percentVal ? getCorruptedByteArray(img, percentVal) : img.byteArray;
var bufferImage = new Image();
bufferImage.onload = function (e) {
img.context.drawImage(bufferImage, 0, 0, bufferImage.width, bufferImage.height);
};
bufferImage.src = byteArrayToBase64(imageData);
}
function wrapImageElement(img, imgDims) {
// create a wrapping span, drop it before the <img>
var wrap = document.createElement('span');
wrap.setAttribute('class', 'glitch__container');
wrap.setAttribute('style', 'position: relative; display: inline-block;');
img.parentNode.insertBefore(wrap, img);
// then put the <img> inside it
wrap.appendChild(img);
// then append a <canvas>, to use as the overlaid draw surface
var canvas = document.createElement('canvas');
canvas.setAttribute('width', imgDims.width);
canvas.setAttribute('height', imgDims.height);
canvas.setAttribute('style', 'position: absolute; top: 0; left: 0; pointer-events: none; z-index: 1;');
wrap.appendChild(canvas);
// save some references on the image, so it holds state
img.canvas = canvas;
img.context = canvas.getContext('2d');
img.wrap = wrap;
return img;
}
function setupImage(img) {
var imgDims = img.getBoundingClientRect();
img = wrapImageElement(img, imgDims);
img.context.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight, 0, 0, imgDims.width, imgDims.height);
img.byteArray = base64ToByteArray(img.canvas.toDataURL("image/jpeg"));
img.headerLength = detectJpegHeaderSize(img.byteArray);
return img;
}
function checkPixelPermissions(img, canvas) {
try {
var success = canvas.getContext('2d').drawImage(img, 0, 0, 10, 10, 0, 0, 10, 10);
return true;
} catch (e) {
console.error("Cannot glitch " + img.getAttribute("src"), e.message);
}
return false;
}
var attachTo = function attachTo(selector) {
if (!checkCompat()) return [];
var images = document.querySelectorAll(selector);
var testCanvas = document.createElement('canvas');
var permittedImages = Array.prototype.filter.call(images, function (img) {
return checkPixelPermissions(img, testCanvas);
});
attachedImages = Array.prototype.map.call(permittedImages, setupImage);
return attachedImages;
};
var apply = function apply(img, percentVal) {
if (!attachedImages.includes(img)) {
console.error('glitchjpeg: unattached image', img);
return;
}
requestAnimationFrame(function () {
return glitch(img, percentVal);
});
};
var applyRandom = function applyRandom(img) {
var percentVal = Math.round(100 * Math.random());
return apply(img, percentVal);
};
var resize = function resize(img) {
var imgDims = img.getBoundingClientRect();
img.canvas.setAttribute('width', imgDims.width);
img.canvas.setAttribute('height', imgDims.height);
};
var resizeAll = function resizeAll() {
attachedImages.forEach(function (img) {
var imgDims = img.getBoundingClientRect();
img.canvas.setAttribute('width', imgDims.width);
img.canvas.setAttribute('height', imgDims.height);
});
};
var autoResize = function autoResize(enabled) {
if (!window) {
console.log('autoResize toggled attempted, but no "window" object found.');
return;
}
if (enabled) {
window.addEventListener('resize', resizeAll);
} else {
window.removeEventListener('resize', resizeAll);
}
};
exports.attachTo = attachTo;
exports.apply = apply;
exports.applyRandom = applyRandom;
exports.resize = resize;
exports.resizeAll = resizeAll;
exports.autoResize = autoResize;
}((this.glitchjpeg = this.glitchjpeg || {})));