Skip to content

Commit 2bfbbf2

Browse files
committed
update libs
1 parent 6a0a1d9 commit 2bfbbf2

4 files changed

Lines changed: 93 additions & 24 deletions

File tree

19 - Webcam Fun/main.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,35 @@ require.config({
55
paths: {
66
lodash: '../vendors/lodash.js/lodash',
77
},
8-
shim: {
9-
foo: {
10-
deps: ['lodash'],
11-
exports: 'Foo',
12-
},
13-
},
148
});
159

16-
function init($, V) {
10+
function init($, V, Ival) {
11+
1712
var cfg = {
1813
_: 'init()',
1914
$play: $('button.play'),
2015
$photo: $('canvas.photo'),
2116
$video: $('video.player'),
2217
$snap: $('button.shot'),
18+
interval: Ival.create($.noop, 999),
19+
cameraObj: null,
20+
canvasObj: null,
2321
};
2422

25-
cfg.camera2video = V.camera2video(cfg.$video);
23+
cfg.cameraObj = V.pipeCameraTo(cfg.$video);
2624

2725
cfg.$video.addEventListener('canplay', evt => {
28-
if (!cfg.video2canvas) {
29-
cfg.video2canvas = V.video2canvas(cfg.$video, cfg.$photo);
26+
if (!cfg.canvasObj) {
27+
cfg.canvasObj = V.video2canvas(cfg.$video, cfg.$photo);
28+
cfg.interval.setAction(cfg.canvasObj.draw);
3029
}
3130
});
3231

3332
cfg.$play.addEventListener('click', evt => {
34-
if (!cfg.camera2video.playing) {
35-
cfg.camera2video.start();
33+
if (!cfg.cameraObj.playing) {
34+
cfg.cameraObj.start();
3635
} else {
37-
cfg.camera2video.stop();
36+
cfg.cameraObj.stop();
3837
}
3938
});
4039

@@ -49,4 +48,4 @@ function init($, V) {
4948
return cfg;
5049
}
5150

52-
require(['./util', './vid'], init);
51+
require(['util', 'vid', 'interval'], init);

libs/interval.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
define(['util'], {
2+
create: function (_fn, _ms, _once) {
3+
var count, interval, action, rate, self;
4+
5+
function _clear() {
6+
interval = window.clearInterval(interval);
7+
}
8+
9+
function _makeInterval(once) {
10+
interval = window.setInterval((foo) => {
11+
if (once && count) return _clear();
12+
count++;
13+
action(foo);
14+
}, rate > 9 ? rate : 99);
15+
}
16+
17+
function _reload() {
18+
_clear();
19+
_makeInterval();
20+
}
21+
22+
function _create(fn, ms, once) {
23+
count = 0;
24+
_clear();
25+
setAction(fn);
26+
setRate(ms);
27+
_makeInterval(once);
28+
return self;
29+
}
30+
/*
31+
32+
PUBLIC
33+
34+
*/
35+
function setAction(fn) {
36+
action = fn;
37+
}
38+
39+
function setRate(ms) {
40+
rate = ms;
41+
if (interval) _reload();
42+
}
43+
44+
function start() {
45+
_reload();
46+
}
47+
48+
function stop() {
49+
_clear();
50+
}
51+
52+
self = Object.create({
53+
get count() {
54+
return count;
55+
},
56+
setAction,
57+
setRate,
58+
start,
59+
stop,
60+
});
61+
62+
return _create(_fn, _ms, _once);
63+
}
64+
});

libs/util.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
define(function () {
2-
return (sel) => document.querySelector(sel);
3-
});
1+
function PolyJQ() {
2+
let polyjq = (sel) => document.querySelector(sel);
3+
polyjq.noop = () => {};
4+
return polyjq;
5+
};
6+
7+
define(PolyJQ);
8+
9+
polyjq = PolyJQ();

libs/vid.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
define(['util'], function ($) {
22

3-
function camera2video(_video = {}) {
3+
function pipeCameraTo(_video = {}) {
44
if (_video.tagName !== 'VIDEO') {
55
throw Error('Pipes only to video elements');
66
}
@@ -31,19 +31,19 @@ define(['util'], function ($) {
3131
}
3232

3333
function video2canvas(_video, _canvas) {
34-
var context, height, width, interval;
34+
var context, height, width;
3535

3636
context = _canvas.getContext('2d');
3737
height = _canvas.height = _video.videoHeight;
3838
width = _canvas.width = _video.videoWidth;
39-
interval = setInterval(foo => {
40-
context.drawImage(_video, 0, 0, width, height);
41-
});
4239
_video.hidden = true;
4340

4441
return {
4542
_: 'video2canvas',
46-
_video, _canvas, context, height, width, interval,
43+
_video, _canvas, context, height, width,
44+
draw: foo => {
45+
context.drawImage(_video, 0, 0, width, height);
46+
},
4747
};
4848
}
4949

@@ -66,7 +66,7 @@ define(['util'], function ($) {
6666
}
6767

6868
return {
69-
camera2video,
69+
pipeCameraTo,
7070
video2canvas,
7171
snapCanvas,
7272
};

0 commit comments

Comments
 (0)