-
Notifications
You must be signed in to change notification settings - Fork 0
/
GIFacer.js
489 lines (350 loc) · 11.1 KB
/
GIFacer.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
var image;
var mouseX = 0, mouseY = 0;
var _video, _outputCanvas, _ctx, _localMediaStream;
var _frames;
var _curFrame;
var gifEncoder;
var gifFrameNum = 0;
var gifFrames = 5;
var gifEncoding;
var _captureRate = 0;
var _playbackRate = 0;
var _canvasAngle = 0;
var _numOfSlices = 9;
var _numOfFrames = 9;
var _encodeRate = 1;
var _captureTL;
var _playbackTL;
$( document ).ready(function() {
loadAssets();
});
Filters = {};
Filters.getPixels = function(img) {
// console.log('get pixels');
// console.log(img);
var c = this.getCanvas(img.width, img.height);
//console.log(c);
var ctx = c.getContext('2d');
ctx.drawImage(img,0,0);
return ctx.getImageData(0,0,c.width,c.height);
};
Filters.getCanvas = function(w,h) {
var c = document.createElement('canvas');
c.width = w;
c.height = h;
return c;
};
Filters.filterImage = function(filter, image, var_args) {
//console.log(image);
var args = [this.getPixels(image)];
for (var i=2; i<arguments.length; i++) {
args.push(arguments[i]);
}
return filter.apply(null, args);
};
Filters.threshold = function(pixels, threshold) {
var d = pixels.data;
for (var i=0; i<d.length; i+=4) {
var r = d[i];
var g = d[i+1];
var b = d[i+2];
var v = (0.2126*r + 0.7152*g + 0.0722*b >= threshold) ? 255 : 0;
d[i] = d[i+1] = d[i+2] = v
}
return pixels;
};
Filters.thresholdRange = function(pixels, min, max, value, cutout) {
var d = pixels.data;
for (var i=0; i<d.length; i+=4) {
var r = d[i];
var g = d[i+1];
var b = d[i+2];
var a = d[i+3];
var val = 0.2126*r + 0.7152*g + 0.0722*b;
if (cutout)
{
d[i+3] = 100;
}
else
{
var v = (val >= min && val <= max) ? value : val;
}
d[i] = d[i+1] = d[i+2] = v
}
return pixels;
}
Filters.grayscale = function(pixels, args) {
// console.log('grayscale')
var d = pixels.data;
for (var i=0; i<d.length; i+=4) {
var r = d[i];
var g = d[i+1];
var b = d[i+2];
// CIE luminance for the RGB
// The human eye is bad at seeing red and blue, so we de-emphasize them.
var v = 0.2126*r + 0.7152*g + 0.0722*b;
d[i] = d[i+1] = d[i+2] = v
}
return pixels;
};
function __applyFilter(cnv){
var ctx = cnv.getContext("2d");
var lMin = $('#lMin').val();
var lMax = $('#lMax').val();
var lValue = $('#lValue').val();
var mMin = $('#mMin').val();
var mMax = $('#mMax').val();
var mValue = $('#mValue').val();
var hMin = $('#hMin').val();
var hMax = $('#hMax').val();
var hValue = $('#hValue').val();
var data = Filters.filterImage(Filters.thresholdRange, cnv, lMin, lMax, lValue);
ctx.putImageData(data,0,0);
var data = Filters.filterImage(Filters.thresholdRange, cnv, mMin, mMax, mValue);
ctx.putImageData(data,0,0);
var data = Filters.filterImage(Filters.thresholdRange, cnv, hMin, hMax, hValue);
//var data = Filters.filterImage(Filters.grayscale, cnv);
ctx.putImageData(data,0,0);
// console.log(cnv);
}
function onAssetsLoaded() {
init();
animate();
}
function loadAssets() {
image = new Image();
image.src = "img/face.png";
image.onload = onAssetsLoaded;
}
function __onCameraDetectionError(e)
{
console.log('Browser failed to receive a camera object', e);
}
function __onCameraSuccess(e)
{
console.log('Browser camera detection successful');
}
function __onGifEncoder()
{
gifEncoder = new GIF({
workers: 1,
quality: 50
});
gifEncoder.on('finished', function(blob) {
console.log('all set');
window.open(URL.createObjectURL(blob));
});
gifFrameNum = 0;
_encodeRate = $('#encodeRate').val();
gifEncoding = true;
}
function __onSnapshot() {
console.log('taking snapshot');
TweenMax.to($('#countDown'),.2,{opacity:1});
_curFrame = 0;
_playbackTL.pause();
_canvasAngle = $('#captureRate').val();
_numOfSlices = $('#slices').val();
_numOfFrames = $('#frames').val();
_captureRate = $('#captureRate').val();
__initCanvases();
console.log(_canvasAngle+' - '+_numOfSlices+' - '+_numOfFrames+'- '+_captureRate+' -'+_playbackRate+' -'+_localMediaStream);
if (_localMediaStream) {
//_ctx.drawImage(_video, 0, 0, _video.clientWidth, 100, 0, 0, _video.clientWidth, 100);
//_ctx.drawImage(_video, 0, 120, _video.clientWidth, 100, 0, 120, _video.clientWidth, 100);
//_ctx.drawImage(_video, 0, 240, _video.clientWidth, 100, 0, 240, _video.clientWidth, 100);
//_ctx.drawImage(_video, 0, 360, _video.clientWidth, 100, 0, 360, _video.clientWidth, 100);
//{repeat:-1, yoyo:true}
_captureTL.clear();
var timing = 1;
var i;
var limit = _frames.length;
for (i = 0; i<limit; ++i)
{
var frame = _frames[i];
console.log('capture'+i)
//tl.call(__videoGrab,[frame.getContext('2d'), 0, 150*i, _video.clientWidth, 150], this, "+=1");
_captureTL.call(__videoGrab,[frame, 0, 0, _video.clientWidth, _video.clientHeight], this, "+="+_captureRate)
_captureTL.call(__onSnapShotPreview,[frame], this, "+=.01");
}
//playback.call(__spliceFrames,[], this, "+=.5");
// tl.to(lT.sprite,timing,{ease:Strong.easeInOut, rotation:sA*.4+'deg', transformOrigin:lT.pivotX+'px '+lT.pivotY+'px'},'startRight'),
//TweenMax.delayedCall(1, __videoGrab, [v]);
// TweenMax.
// "image/webp" works in Chrome 18. In other browsers, this will fall back to image/png.
}
}
function __onSnapShotPreview(frame)
{
console.log('preview')
_ctx.clearRect ( 0 , 0 , 320 , 240 );
_ctx.drawImage(frame, 0, 0,640,480,0,0,320,240);
__onProcessCanvas();
}
function __onFramesBatched()
{
console.log('batching');
TweenMax.to($('#countDown'),.2,{opacity:0});
$('#countdownTimer').text('');
_playbackTL.play();
}
/* Initialize canvases for multiple frames */
function __initCanvases()
{
_frames = [];
var i;
var limit = _numOfFrames;
console.log(limit+'---'+_numOfFrames);
$('#frames').text("");
for (i = 0; i<limit; ++i)
{
console.log("canvas "+i);
frameID = 'frame_'+i
$('#frames').append('<canvas width="640" height="480" id="'+frameID+'" class="grayscale"></canvas>');
frame = document.getElementById(frameID);
_frames[i] = frame;
}
}
/* draw setion to canvas */
function __videoGrab(c,x,y,w,h)
{
console.log('video Grab');
_curFrame++;
$('#countdownTimer').text('-'+_curFrame+'-');
if (_localMediaStream) {
//ctx.drawImage(_video,x,y-10,w,h+20,x,y-10,w,h+20);
//ctx.globalAlpha=1;
var ctx = c.getContext('2d');
ctx.drawImage(_video,x,y,w,h,x,y,w,h);
__applyFilter(c);
}
}
function __spliceFrames()
{
_playbackRate = $('#playbackRate').val();
_numOfSlices = $('#slices').val();
_canvasAngle = $('#angle').val();
if(_playbackRate <=0)
{
_playbackRate = 0;
}
_playbackTL.timeScale(_playbackRate);
var s;
var sLimit = _numOfSlices;//_frames.length;
var frameHeight = _video.clientHeight;
var frameWidth = _video.clientWidth;
var sliceSize = frameHeight / sLimit;
var slices = sLimit;
// loop for determining the faded edge thickness of each slice.
for (s = 0; s<slices; ++s)
{
var frame = _frames[Math.floor(Math.random()*_frames.length)];
var sY = sliceSize*s;
//console.log(s);
_ctx.globalAlpha=.2;
var edge;
var edgeLimit = 20;
var edgeThickness = 2;
var x = _outputCanvas.width>>1;//Math.random()*.5;//_outputCanvas.width;
var y = _outputCanvas.height>>1;//_outputCanvas.height;
var angleInRadians = (Math.random()*_canvasAngle - _canvasAngle/2) * Math.PI/180;
_ctx.translate(x, y);
_ctx.rotate(angleInRadians);
for (edge=edgeLimit; edge > 0; edge -= edgeThickness)
{
eY = sY - edge;
if (eY < 0) eY = 0;
eH = sliceSize + edge*2;
if (eY + eH > frameHeight) eH = frameHeight - eY;
//draws faded edges on each frame to blend better
_ctx.drawImage(frame, 0, eY,frameWidth,eH,-x,(eY)/2 - y,frameWidth/2,sliceSize/2 + edge*2)
}
_ctx.globalAlpha=.8;
_ctx.drawImage(frame, 0, sY,frameWidth,sliceSize,-x,sY/2 - y,frameWidth/2,sliceSize/2)
_ctx.rotate(-angleInRadians);
_ctx.translate(-x, -y);
}
__onProcessCanvas();
}
function __onProcessCanvas()
{
document.querySelector('img').src = _outputCanvas.toDataURL('image/webp');
if (gifEncoding)
{
console.log('FRAME='+gifFrameNum);
gifFrameNum++;
gifEncoder.addFrame(_outputCanvas, {delay: _encodeRate * 1000});
if(gifFrameNum > gifFrames)
{
gifEncoding = false;
gifEncoder.render();
}
}
}
// initialize page after image load.
function init()
{
container = document.createElement( 'div' );
document.body.appendChild( container );
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
_video = document.querySelector('video');
_outputCanvas = document.getElementById('output');
_ctx = _outputCanvas.getContext('2d');
_localMediaStream = null;
_frames = [];
__initCanvases();
_video.addEventListener('click', __onSnapshot, false);
if (navigator.getUserMedia) {
navigator.getUserMedia({ video: true}, function(stream) {
_video.src = window.URL.createObjectURL(stream);
_localMediaStream = stream;
}, __onCameraDetectionError);
} else {
console.log('no getUserMedia, sucks bro');
//_video.src = 'somevideo.webm'; // fallback.
}
_captureTL = new TimelineMax({onComplete:__onFramesBatched});
_playbackTL = new TimelineMax({repeat:-1});
_playbackTL.call(__spliceFrames,[], this, "+=.05");
_playbackTL.pause();
var recordButton = document.getElementById('recordButton')
recordButton.addEventListener('click', __onSnapshot, false);
var gifButton = document.getElementById('gifButton');
gifButton.addEventListener('click', __onGifEncoder,false);
_stats = new Stats();
_stats.domElement.style.position = 'absolute';
_stats.domElement.style.top = '0px';
_stats.domElement.style.left = '500px';
container.appendChild( _stats.domElement );
_stats.update();
}
function animate() {
requestAnimationFrame( animate );
//render();
_stats.update();
}
function getRotationDegrees(obj)
{
var matrix = obj.css("-webkit-transform") ||
obj.css("-moz-transform") ||
obj.css("-ms-transform") ||
obj.css("-o-transform") ||
obj.css("transform");
if(matrix !== 'none') {
var values = matrix.split('(')[1].split(')')[0].split(',');
var a = values[0];
var b = values[1];
var angle = Math.round(Math.atan2(b, a) * (180/Math.PI));
} else { var angle = 0; }
return (angle < 0) ? angle +=360 : angle;
}
function PixelSlider(id, width, height, min, max, steps)
{
this._id = id;
// this._bar =
}
PixelSlider.prototype.Init = function()
{
}