-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
138 lines (128 loc) · 3.03 KB
/
app.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
$(function(){
var $head = $('head');
var $body = $('body');
var $results = $('#results');
var $frames = $('#frames');
var $done = $('#done');
var $error = $('#error');
var $delay = $('#delay');
var icon;
(function(){
var $c = $('<canvas width=16 height=16/>');
var c = $c.get(0);
var ctx = c.getContext('2d');
var $link = $('<link rel="icon">').appendTo($head);
icon = function(f){
f(ctx,c);
$link.attr('href',c.toDataURL('image/png'));
};
})();
var gif;
var clearIt = false;
var delay = $delay.val();
$delay.on('change',function(){
delay = $delay.val();
});
var newGif = function(){
console.debug('Making a GIF();');
gif = new GIF({
workers: 2,
quality: 10
});
clearIt = true;
};
newGif();
$done.on('click',function(e){
$error.text('');
if(!gif){
console.warn('no gif');
newGif();
return;
}
if(!gif.frames.length){
if($('.frame').length == 0){
$error.text('Add some images first!');
}else{
$error.text("The added frames are part of a GIF that is lost to me..........");
}
return;
}
gif.on('finished', function(blob){
var src = URL.createObjectURL(blob);
var $gif = $('<img>').appendTo($results);
$gif.attr('src',src);
$done.removeClass('working').text('Create GIF');
});
try {
gif.render();
$done.addClass('working').text('Working...');
}catch(e){
$done.addClass('failed').text('Failed');
$error.text(e.message);
console.error(e);
}finally{
newGif();
}
});
//Drag and Drop
var prevent = function(e){
e.preventDefault();
e.stopPropagation();
};
$('body')
.on('dragover dragenter',prevent)
.on('drop',function(e){prevent(e);
$error.text('');
var dt = e.originalEvent.dataTransfer, files;
if(dt && (files=dt.files) && files.length){
if(clearIt){
var $oldFrames = $(".frame");
$oldFrames.addClass("poof");
setTimeout(function(){
$oldFrames.remove();
},1200);
clearIt = false;
}
//console.debug("dropped files: ",files);
var i = 0;
var n_images = 0;
var n_other = 0;
var next = function(){
var file = files[i++];
if(!file){
if(n_other && !n_images){
$error.text(n_other>1?"Those aren't images!":"That's not an image!");
}
return;
}
if(file.type.match(/image/)){
n_images++;
var reader = new FileReader();
reader.onload = function(e){
var $img = $('<img class="frame animated flipInY"/>').appendTo($frames);
$img.attr('src',reader.result);
$img.on('load',function(e){
var img = $img[0];
gif.options.width = img.naturalWidth;
gif.options.height = img.naturalHeight;
gif.addFrame(img, {delay: delay});
icon(function(ctx){
ctx.drawImage(img,0,0,16,16);
});
next();
});
};
reader.onerror = function(e){
$error.text(e.message);
console.error(reader.result,e);
};
reader.readAsDataURL(file);
}else{
n_other++;
next();
}
};
next();
}
});
});