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
|
(function(){
if ($("gif-recorder")) return;
var canvases = document.getElementsByTagName("canvas")
if (canvases.length == 0) { alert("no canvas found"); return; }
var biggest = 0, biggest_area = 0;
for (var i in canvases) {
var area = canvases[i].width*canvases[i].height
if (area > biggest_area) biggest = i;
}
var source = canvases[biggest]
var encoder = new GifEncoder()
encoder.on("encoded-frame", encoded_frame)
encoder.on("rendered", rendered_bytes)
encoder.on("rendered-url", rendered_url)
var w, h, x, y;
var w_el, h_el, x_el, y_el;
var last_t, frame_t, count, delay, offset, done;
var frames = []
var curtain, outline
var dragging = false
var lastGif
init()
function init(){
document.body.style.width = "100%"
document.body.style.height = "100%"
document.body.parentNode.style.width = "100%"
document.body.parentNode.style.height = "100%"
// var template = $("template").innerHTML
var el = document.createElement("div")
el.id = "gif-recorder"
// el.innerHTML = template
el.innerHTML = UI_TEMPLATE
document.body.appendChild(el)
bind()
activate()
}
function bind(){
curtain = $("curtain")
outline = $("outline")
controls = $("controls")
w_el = $("w_el")
h_el = $("h_el")
x_el = $("x_el")
y_el = $("y_el")
$("record").addEventListener("click", record, false)
$("save").addEventListener("click", save, false)
curtain.addEventListener("mousedown", box_start, false)
curtain.addEventListener("mousemove", box_size, false)
curtain.addEventListener("mouseup", box_end, false)
w_el.addEventListener("change", box_position, false)
h_el.addEventListener("change", box_position, false)
x_el.addEventListener("change", box_position, false)
y_el.addEventListener("change", box_position, false)
window.addEventListener("keydown", keydown, false)
}
var listening = false
function activate(){
listening = true
outline.style.display = "none"
controls.style.display = "block"
curtain.style.display = "block"
}
function deactivate(){
listening = false
console.log("not listening")
outline.style.display = "none"
controls.style.display = "none"
curtain.style.display = "none"
}
function keydown(e){
if (e.keyCode == 27) { // esc
listening ? deactivate() : activate()
}
}
function defer(callback){
var timeout = null
return function(){
clearTimeout(timeout)
timeout = setTimeout(callback, 300)
}
}
function box_start(e){
if (! listening) return
e.stopPropagation()
x = e.pageX
y = e.pageY
w = 1
h = 1
dragging = true
box_resize()
}
function box_position(){
w = _int(w_el)
h = _int(h_el)
x = _int(x_el)
y = _int(y_el)
box_resize()
}
function box_resize(){
if (isNaN(w) || isNaN(h) || isNaN(x) || isNaN(y)) return
w_el.value = ~~(w)
h_el.value = ~~(h)
x_el.value = ~~(x)
y_el.value = ~~(y)
outline.style.left = px(x-1)
outline.style.top = px(y-1)
outline.style.width = px(w)
outline.style.height = px(h)
outline.style.display = "block"
enable("record")
}
function box_size(e){
if (! dragging) return
e.stopPropagation()
w = e.pageX - x
h = e.pageY - y
box_resize()
}
function box_end(e){
e.preventDefault()
dragging = false
controls.style.display = "block"
$("record").focus()
}
function _int(el){ return parseInt(typeof el == "string" ? $(el).value : el.value) }
function _float(el){ return parseFloat(typeof el == "string" ? $(el).value : el.value) }
function show(id){ $(id).style.display="block" }
function hide(id){ $(id).style.display="none" }
function enable(id){ $(id).removeAttribute("disabled") }
function disable(id){ $(id).setAttribute("disabled","disabled") }
function clickable(id){ $(id).style.pointerEvents = "auto" }
function unclickable(id){ $(id).style.pointerEvents = "none" }
function px(n){ return (~~n) + "px" }
function record(e){
if (! listening) return
e.stopPropagation()
count = _int("framecount")
delay = _float("framedelay") * 1000
offset = source.getBoundingClientRect();
console.log(count, delay)
if (isNaN(count) || isNaN(delay)) return
done = 0
frame_t = 0
build()
capture()
last_t = +new Date()
requestAnimationFrame(recordloop)
status("recording")
disable("record")
unclickable("curtain")
unclickable("controls")
}
function recordloop(){
var canvas = document.createElement("canvas")
var t = +new Date()
frame_t += t - last_t
last_t = t
if (frame_t > delay) {
frame_t -= delay
capture()
}
if (done == count) {
render()
}
else {
requestAnimationFrame(recordloop)
}
}
function build(){
frames = new Array(count)
for (var i = 0; i < count; i++){
frames[i] = document.createElement("canvas")
frames[i].height = h
frames[i].width = w
}
}
function backgroundColor(){
var colors = [
source.style.backgroundColor,
document.body.style.backgroundColor,
"white"
]
for (var i in colors) {
if (colors[i] && colors[i] != "") return colors[i]
}
}
function capture(){
var frame = frames[done++]
var ctx = frame.getContext('2d')
ctx.fillStyle = backgroundColor()
ctx.fillRect(0,0,w,h)
ctx.drawImage(source, x-offset.left, y-offset.top, w, h, 0, 0, w, h)
}
function render(){
encoder.reset()
encoder.addFrames(frames, delay)
status("encoding")
try {
encoder.encode()
}
catch (e) {
rendering = false
status(e)
throw e
}
}
function $(s){ return document.getElementById("ge_" + s) }
function status(s){
$("status").innerHTML = s
}
function encoded_frame(done,count){
status("encoded " + done + " / " + count)
}
function rendered_bytes(bytes){
status(filesize(bytes.length))
}
function rendered_url(url){
var image = new Image ()
lastGif = image.src = url
$("preview").innerHTML = ""
$("preview").appendChild(image)
rendering = false
enable("record")
enable("save")
clickable("curtain")
clickable("controls")
}
function save (e){
e.stopPropagation()
if (! lastGif) return;
var filename = (window.location.host + window.location.pathname).replace(/[^a-zA-Z0-9]/g,"-").replace(/-+/,"-")
var blob = dataUriToBlob(lastGif)
saveAs(blob, filename + "-" + (+new Date()) + ".gif");
}
function filesize(n) {
if (n < 1e3) return n + " bytes"
if (n < 1e6) return decimalString(n/1e3) + " kb"
if (n < 1e9) return decimalString(n/1e6) + " mb"
return "WAY TOO BIG DUDE"
}
function decimalString(n){
var m = Math.floor(n);
return m + "." + Math.round((n-m)*10)
}
})()
|