-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiled.js
447 lines (396 loc) · 14.5 KB
/
compiled.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
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
'use strict';
var _fileSaver = require('file-saver');
var _fileSaver2 = _interopRequireDefault(_fileSaver);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
// Declare component elements
var $body = document.body;
var $canvas = document.querySelector('canvas');
var $avatar = document.createElement('img');
var $prev = document.querySelector('.prev');
var $download = document.querySelector('.download');
var $next = document.querySelector('.next');
// Initialize panning options
var dragStart = { x: 0, y: 0 };
var imgPos = { x: 0, y: 0 };
// Helper function for when downloading
var cloneCanvas = function cloneCanvas(oldCanvas) {
var newCanvas = document.createElement('canvas');
var context = newCanvas.getContext('2d');
newCanvas.width = oldCanvas.width;
newCanvas.height = oldCanvas.height;
context.drawImage(oldCanvas, 0, 0);
return newCanvas;
};
// Apply a filter to a canvas
var applyFilter = function applyFilter(canvas) {
return function (url) {
return new Promise(function (resolve, reject) {
var image = document.createElement('img');
image.src = url;
image.onload = function () {
canvas.getContext('2d').drawImage(image, 0, 0, canvas.width, canvas.height);
resolve(canvas);
};
});
};
};
// Draw an avatar to a canvas
var drawAvatar = function drawAvatar(url) {
imgPos = { x: 0, y: 0 };
$avatar.src = url;
$avatar.onload = function () {
// Set the $canvas size to the $avatars shortest side
$canvas.width = Math.min($avatar.width, $avatar.height);
$canvas.height = Math.min($avatar.width, $avatar.height);
// Draw the avatar $avatar to $canvas
$canvas.getContext('2d').drawImage($avatar, 0, 0, $avatar.width, $avatar.height);
};
};
// Create file blob from dataUrl and initiate a download
var download = function download(dataUrl) {
var c = cloneCanvas($canvas);
var f = document.querySelector('.active').src;
applyFilter(c)(f).then(function (res) {
var dataUrl = res.toDataURL('image/jpg');
var data = atob(dataUrl.substring("data:image/png;base64,".length));
var asArray = new Uint8Array(data.length);
for (var i = 0, len = data.length; i < len; ++i) {
asArray[i] = data.charCodeAt(i);
}var f = new Blob([asArray.buffer], { type: 'application/octet-stream' });
_fileSaver2.default.saveAs(f, "filter.jpg");
});
};
// Move $avatar around canvas according to drag event
var track = function track(e) {
var x = void 0,
y = void 0;
x = Math.max(Math.min(imgPos.x + (e.pageX - dragStart.x), 0), $canvas.width - $avatar.width);
y = Math.max(Math.min(imgPos.y + (e.pageY - dragStart.y), 0), $canvas.height - $avatar.height);
// Draw the avatar on the $canvas
$canvas.getContext('2d').drawImage($avatar, x, y, $avatar.width, $avatar.height);
};
// Setup at the start of a drag event
var start = function start(e) {
dragStart = { x: e.pageX, y: e.pageY };
$body.addEventListener('mousemove', track);
$body.addEventListener('touchmove', touchHandler);
};
// Teardown ar the end of a drag event
var stop = function stop(e) {
$body.removeEventListener('mousemove', track);
$body.removeEventListener('touchmove', touchHandler);
imgPos = {
x: Math.max(Math.min(imgPos.x + (e.pageX - dragStart.x), 0), $canvas.width - $avatar.width),
y: Math.max(Math.min(imgPos.y + (e.pageY - dragStart.y), 0), $canvas.height - $avatar.height)
};
};
// Extract image dataUrl from drop event
var processInput = function processInput(e) {
e.stopPropagation();
e.preventDefault();
// Setup file reader
var reader = new FileReader();
var file = e.dataTransfer.files[0];
var acceptedFiles = ['png', 'bmp', 'jpg', 'jpeg', 'gif'];
// Ensure that the file type
if (!acceptedFiles.includes(file.type.replace('image/', ''))) {
alert('Incompatible file type!');
return;
}
// Initiate reading of file
reader.onload = function (file) {
return drawAvatar(file.srcElement.result);
};
reader.readAsDataURL(file);
};
var nextFilter = function nextFilter() {
var overlay = document.querySelector('overlay-');
var index = parseInt(overlay.dataset.index, 10);
if (index < overlay.children.length - 1) {
overlay.dataset.index = ++index;
overlay.style.transform = 'translateX(-' + index * 62 + 'vmin)';
// Add active class to focus filter
[].concat(_toConsumableArray(overlay.children)).forEach(function (x) {
return x.classList.remove('active');
});
overlay.children[index].classList.add('active');
}
};
var prevFilter = function prevFilter() {
var overlay = document.querySelector('overlay-');
var index = parseInt(overlay.dataset.index, 10);
if (index > 0) {
overlay.dataset.index = --index;
overlay.style.transform = 'translateX(-' + index * 62 + 'vmin)';
// Add active class to focus filter
[].concat(_toConsumableArray(overlay.children)).forEach(function (x) {
return x.classList.remove('active');
});
overlay.children[index].classList.add('active');
}
};
// Create a drag-and-drop interface for images on entire body
$body.addEventListener('dragover', function (e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
// When the user drops or inputs an image process the image
$body.addEventListener('drop', processInput);
// Pan image on drag from $canvas
$canvas.addEventListener('mousedown', start);
// Stop any tracking at end of drag
$body.addEventListener('mouseup', stop);
$body.addEventListener('mouseleave', stop);
//Pan image on touch and drag on $canvas
$canvas.addEventListener('touchstart', touchHandler);
// Stop any tracking at end of touch drag
$canvas.addEventListener('touchend', touchHandler);
// $body.addEventListener('touchcancel', touchHandler);
// When the user uses the arrow keys to change filter
$body.addEventListener('keyup', function (e) {
// Right arrow for next filter
if (e.keyCode === 39) nextFilter();
// Left arrow for previous filter
if (e.keyCode === 37) prevFilter();
// Spacebar from download
if (e.keyCode === 32) download();
});
// Listen for interaction with controls
// $next.addEventListener('click', nextFilter);
$download.addEventListener('click', download);
$download.addEventListener('touchstart', download);
// $prev.addEventListener('click', prevFilter);
// Apply default avatar
drawAvatar('avatar.jpg');
var fileInput = document.querySelector("#file-upload");
console.log(fileInput);
// fileInput.addEventListener('touchstart', ()=>{
// console.log("Tap Registered!");
// })
// fileInput.addEventListener('click', ()=>{
// console.log("Click Registered!");
// })
fileInput.addEventListener('change', function (e) {
e.stopPropagation();
e.preventDefault();
// Setup file reader
var reader = new FileReader();
var file = e.target.files[0];
console.log(file);
var acceptedFiles = ['png', 'bmp', 'jpg', 'jpeg', 'gif'];
// Ensure that the file type
if (!acceptedFiles.includes(file.type.replace('image/', ''))) {
alert('Incompatible file type!');
return;
}
// Initiate reading of file
reader.onload = function (file) {
return drawAvatar(file.srcElement.result);
};
reader.readAsDataURL(file);
});
function touchHandler(event) {
var touches = event.changedTouches,
first = touches[0],
type = "";
switch (event.type) {
case "touchstart":
type = "mousedown";break;
case "touchmove":
type = "mousemove";break;
case "touchend":
type = "mouseup";break;
default:
return;
}
// initMouseEvent(type, canBubble, cancelable, view, clickCount,
// screenX, screenY, clientX, clientY, ctrlKey,
// altKey, shiftKey, metaKey, button, relatedTarget);
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent(type, true, true, window, 1, first.screenX, first.screenY, first.clientX, first.clientY, false, false, false, false, 0 /*left*/, null);
first.target.dispatchEvent(simulatedEvent);
event.preventDefault();
}
},{"file-saver":2}],2:[function(require,module,exports){
/* FileSaver.js
* A saveAs() FileSaver implementation.
* 1.3.2
* 2016-06-16 18:25:19
*
* By Eli Grey, http://eligrey.com
* License: MIT
* See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
*/
/*global self */
/*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
var saveAs = saveAs || (function(view) {
"use strict";
// IE <10 is explicitly unsupported
if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
return;
}
var
doc = view.document
// only get URL when necessary in case Blob.js hasn't overridden it yet
, get_URL = function() {
return view.URL || view.webkitURL || view;
}
, save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
, can_use_save_link = "download" in save_link
, click = function(node) {
var event = new MouseEvent("click");
node.dispatchEvent(event);
}
, is_safari = /constructor/i.test(view.HTMLElement) || view.safari
, is_chrome_ios =/CriOS\/[\d]+/.test(navigator.userAgent)
, throw_outside = function(ex) {
(view.setImmediate || view.setTimeout)(function() {
throw ex;
}, 0);
}
, force_saveable_type = "application/octet-stream"
// the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
, arbitrary_revoke_timeout = 1000 * 40 // in ms
, revoke = function(file) {
var revoker = function() {
if (typeof file === "string") { // file is an object URL
get_URL().revokeObjectURL(file);
} else { // file is a File
file.remove();
}
};
setTimeout(revoker, arbitrary_revoke_timeout);
}
, dispatch = function(filesaver, event_types, event) {
event_types = [].concat(event_types);
var i = event_types.length;
while (i--) {
var listener = filesaver["on" + event_types[i]];
if (typeof listener === "function") {
try {
listener.call(filesaver, event || filesaver);
} catch (ex) {
throw_outside(ex);
}
}
}
}
, auto_bom = function(blob) {
// prepend BOM for UTF-8 XML and text/* types (including HTML)
// note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
}
return blob;
}
, FileSaver = function(blob, name, no_auto_bom) {
if (!no_auto_bom) {
blob = auto_bom(blob);
}
// First try a.download, then web filesystem, then object URLs
var
filesaver = this
, type = blob.type
, force = type === force_saveable_type
, object_url
, dispatch_all = function() {
dispatch(filesaver, "writestart progress write writeend".split(" "));
}
// on any filesys errors revert to saving with object URLs
, fs_error = function() {
if ((is_chrome_ios || (force && is_safari)) && view.FileReader) {
// Safari doesn't allow downloading of blob urls
var reader = new FileReader();
reader.onloadend = function() {
var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;');
var popup = view.open(url, '_blank');
if(!popup) view.location.href = url;
url=undefined; // release reference before dispatching
filesaver.readyState = filesaver.DONE;
dispatch_all();
};
reader.readAsDataURL(blob);
filesaver.readyState = filesaver.INIT;
return;
}
// don't create more object URLs than needed
if (!object_url) {
object_url = get_URL().createObjectURL(blob);
}
if (force) {
view.location.href = object_url;
} else {
var opened = view.open(object_url, "_blank");
if (!opened) {
// Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
view.location.href = object_url;
}
}
filesaver.readyState = filesaver.DONE;
dispatch_all();
revoke(object_url);
}
;
filesaver.readyState = filesaver.INIT;
if (can_use_save_link) {
object_url = get_URL().createObjectURL(blob);
setTimeout(function() {
save_link.href = object_url;
save_link.download = name;
click(save_link);
dispatch_all();
revoke(object_url);
filesaver.readyState = filesaver.DONE;
});
return;
}
fs_error();
}
, FS_proto = FileSaver.prototype
, saveAs = function(blob, name, no_auto_bom) {
return new FileSaver(blob, name || blob.name || "download", no_auto_bom);
}
;
// IE 10+ (native saveAs)
if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
return function(blob, name, no_auto_bom) {
name = name || blob.name || "download";
if (!no_auto_bom) {
blob = auto_bom(blob);
}
return navigator.msSaveOrOpenBlob(blob, name);
};
}
FS_proto.abort = function(){};
FS_proto.readyState = FS_proto.INIT = 0;
FS_proto.WRITING = 1;
FS_proto.DONE = 2;
FS_proto.error =
FS_proto.onwritestart =
FS_proto.onprogress =
FS_proto.onwrite =
FS_proto.onabort =
FS_proto.onerror =
FS_proto.onwriteend =
null;
return saveAs;
}(
typeof self !== "undefined" && self
|| typeof window !== "undefined" && window
|| this.content
));
// `self` is undefined in Firefox for Android content script context
// while `this` is nsIContentFrameMessageManager
// with an attribute `content` that corresponds to the window
if (typeof module !== "undefined" && module.exports) {
module.exports.saveAs = saveAs;
} else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) {
define("FileSaver.js", function() {
return saveAs;
});
}
},{}]},{},[1]);