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
|
var frame_editor = {}
frame_editor.init = function(){
frame_editor.bind()
}
frame_editor.bind = function(){
$("#add-frame").click(add_frame)
$("#frames").sortable({
start: drag_start,
stop: drag_stop
});
$(document).on("click","#frames .remove",remove_frame)
$("#framecount").change(function(){
var val = $(this).int()
if (val < 1 || isNaN(val)) $(this).val(val = 1)
if (val == 1) $("#add-frame").html("+add frame")
else $("#add-frame").html("+add frames")
})
$("#frames").disableSelection();
$("#remove-all-frames").click(remove_all_frames)
$("#weave-frames").click(weave_frames)
$("#shuffle-frames").click(shuffle_frames)
$("#reverse-frames").click(reverse_frames)
$("#sort-frames").click(sort_frames)
$("#render").click(render)
$("#save").click(save)
$("#upload").click(upload)
$("#background").change(function(){
document.body.style.backgroundColor = $("#background").string()
})
}
function add_frame(){
var frame_count = $("#framecount").int()
if (frame_count < 2) {
add_single_frame()
}
else {
add_frames(frame_count)
}
}
function add_single_frame(){
var $el = $("<div>")
$el.html( $("#frame-template").html() )
$el.attr('index', $("#frames div").length)
var frame = cc.clone().appendTo($el.find(".frame")[0])
frame.canvas.className = "fullsize"
frame.canvas.style.display = "none"
var thumb = cc.clone().resize(100,100).appendTo($el.find(".frame")[0])
$("#frames").append($el)
$("#render").enable()
}
function add_frames(frame_count){
rendering = true
var t = old_t - start_t - pause_t
var frame_delay = $("#frameinterval").float() * 1000
var frame
for (var i = 0; i < frame_count; i++) {
frame = giveFrame(t)
t += frame_delay
shade(frame, t)
add_single_frame()
}
rendering = false
}
function remove_frame(){
$(this).closest("div").remove()
if ($("#frames div").length == 0) {
$("#render").disable()
}
}
function remove_all_frames(){
$("#frames").empty()
}
function shuffle_frames(){
var shuffled = []
var $frames = $("#frames div")
$("#frames").empty().append(shuffle($frames))
}
function reverse_frames(){
var $frames = $("#frames div")
$("#frames").empty().append(reverse($frames))
}
function weave_frames(){
var $frames = $("#frames div")
$("#frames").empty().append(weave($frames))
}
function sort_frames(){
var $frames = $("#frames div")
var sorted = $frames.map(function(i,el){ console.log(i,el); return [[ el.getAttribute('index'), el ]] })
.sort(function(a,b){ return a[0]-b[0] })
.map(function(i,e){ console.log( e ); return e[1] })
$("#frames").empty().append(sorted)
}
function render (){
if (rendering) return
rendering = true
encoder.reset()
var delay = $("#framedelay").float() * 1000 || 100
$("#frames canvas.fullsize").each(function(){
var frame = cq(this.width, this.height).fillStyle($("#background").string()).fillRect(0,0,this.width, this.height).drawImage(this,0,0)
encoder.addFrame(frame.canvas, delay)
})
$("#pause,#render,#add-frame").disable()
$("#rendered").find("img").remove()
$("#rendered").show()
// really bad results with neuquant?
// status("quantizing")
// encoder.quantize()
status("encoding")
try {
encoder.encode()
} catch (e) {
$("#pause,#render,#add-frame").enable()
rendering = false
status(e)
throw e
}
$("#render").html("rendering")
}
var encoder = new GifEncoder()
encoder.on("quantized", function(url){
status("encoding")
encoder.encode()
})
encoder.on("encoded-frame", function(done,count){
status("encoded " + done + " / " + count)
})
encoder.on("rendered", function(bytes){
status(filesize(bytes.length))
})
encoder.on("rendered-url", function(url){
var image = new Image ()
lastGif = image.src = url
$("#rendered").append(image)
$("#uploaded-url").hide().val("")
$("#save,#upload,#rendered").show()
$("#pause,#render,#add-frame,#save,#upload").enable()
$("#render").html("render")
rendering = false
pause(true)
})
function get_filename(){
var basename = $("#url").val().replace(/^.*\//,"").replace(/\..*$/,"").replace(/[^-_ a-zA-Z0-9]/g,"")
var username = user.username
var filename = basename + "-" + username + "-" + (+new Date()) + ".gif"
return filename.replace(/ /g,"_").replace(/-+/g,"-")
}
function save (){
if (! lastGif) return;
var filename = get_filename()
var blob = dataUriToBlob(lastGif)
saveAs(blob, filename);
}
function saveJSON (data, filename) {
var bytes = JSON.stringify(data)
var buf = new ArrayBuffer(bytes.length);
var arr = new Uint8Array(buf);
for (var i = 0; i < bytes.length; i++) {
arr[i] = bytes.charCodeAt(i);
}
var blob = new Blob([arr], { type: "text/json" });
blob.slice = blob.slice || blob.webkitSlice;
saveAs(blob, filename);
}
function upload(){
var filename = get_filename()
var username = user.username
var blob = dataUriToBlob(lastGif)
uploadImage({
blob: blob,
filename: filename,
username: username,
success: function(data){
// data.url
// data.filesize
// data.success
console.log(data);
status("uploaded");
$("#uploaded-url").show().focus().val(data.url)
},
error: function(data){
console.log(data)
status("error uploading: " + data.error)
}
});
}
|