forked from conorbuck/canvas-video-effects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
77 lines (70 loc) · 2.38 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>WebCam Effects</title>
<style>
.hidden {
/*display: none;*/
}
</style>
</head>
<body>
<video id="video-stream" style="display:none;" width=320 height=240></video>
<canvas id="canvas-video" width=320 height=240></canvas>
<canvas id="canvas-small" width=80 height=60 class="hidden"></canvas>
<canvas id="canvas-effects" width=320 height=240></canvas>
<script type="text/javascript">
(function(){
var video = document.getElementById('video-stream'),
canvas = document.getElementById('canvas-video'),
canvasSmall = document.getElementById('canvas-small'),
canvasEffect = document.getElementById('canvas-effects'),
ctx = canvas.getContext('2d'),
ctxSmall = canvasSmall.getContext('2d');
ctxEffects = canvasEffect.getContext('2d'),
processor = new Worker('image-worker.js');
w = video.width,
h = video.height,
ws = canvasSmall.width,
hs = canvasSmall.height,
imgData = ctxSmall.getImageData(0,0,ws,hs);
/* Setup WebWorker messaging */
processor.onmessage = function(event){
// Now we're getting back an array buffer
// that corresponds to the smaller image
var buf8 = new Uint8ClampedArray(event.data);
// Set that as the data for the ctxEffects context
imgData.data.set(buf8);
ctxSmall.putImageData(imgData, 0, 0);
// TODO - How do we merge this data with the video?
//ctxEffects.drawImage(video, 0, 0, w, h);
ctxEffects.drawImage(canvasSmall, 0, 0, w, h);
};
/* Setup video stream and canvas */
navigator.getUserMedia =
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
navigator.getUserMedia({video:true}, function(stream){
video.src = URL.createObjectURL(stream);
video.play();
setInterval(render, 10);
}, function(error){
console.log('error', error);
});
var render = function(){
ctx.drawImage(video, 0, 0, w, h);
ctxSmall.drawImage(video, 0, 0, canvasSmall.width, canvasSmall.height);
// Showing the larger video, but we're passing the smaller image data
var srcData = ctxSmall.getImageData(0,0,ws,hs);
// Don't post the imageData
// Instead post the data directly
// It's an arrayBuffer, so it's a transferable object
var ab = srcData.data;
processor.postMessage(ab.buffer, [ab.buffer]);
};
})();
</script>
</body>
</html>