This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
motion.html
175 lines (147 loc) · 5.9 KB
/
motion.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
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
<!doctype html>
<html lang="en">
<head>
<title>Camera</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="js/lib/three.min.js"></script>
<script src="js/shaders/motion.js"></script>
<script src="js/lib/stats.min.js"></script>
<script type="text/javascript" src="js/lib/dat.gui.min.js"></script>
<script src="js/webcam.js"></script>
<script src="js/perspective.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body class="show-camera show-fps">
<div id="menu">
<a href="index.html">single camera</a>
<a href="multiple.html">multiple cameras</a>
<strong>motion</strong>
<a href="threshold.html">threshold</a>
<a href="warp.html">warp</a>
<a href="fx.html">fx</a>
</div>
<div id="camera0" class="camera">
<div class="label">Camera #</div>
<video id="input0" autoplay width="640" height="480" class="webcam"></video>
<canvas id="canvas0" width="640" height="480" class="hidden"></canvas>
</div>
<script>
var SCREEN_HEIGHT = 480;
var SCREEN_WIDTH = 640;
var CONFIG_HIDE_DELAY = 10;
var config = {
showCamera: true,
showFPS: true,
camera: ''
};
$(function() {
init();
animate();
var container, video, renderer, videoTexture, uniforms, camera, scene, prevTexture, videoContext, prevTime;
function init(){
stats = new Stats();
document.body.appendChild( stats.domElement );
var gui = new dat.GUI();
gui.add(config, 'showCamera').onChange(function(value) {
if(value) $('body').addClass("show-camera");
else $('body').removeClass("show-camera");
});
gui.add(config, 'showFPS').onChange(function(value) {
if(value) $('body').addClass("show-fps");
else $('body').removeClass("show-fps");
});
webcam.updateSources(function(s){
var label = [];
var camera = {};
for(var i = 0; i < s.length; i++){
var title = "Camera "+i;
label.push(title);
camera[title] = s[i];
}
webcam.start('input0',s[0]); //start first camera in list
$('#camera0 .label').text(label[0]);
gui.add(config,'camera',label).onChange(function(value){
webcam.start('input0',camera[value]);
$('#camera0 .label').text(value);
});
});
//uncomment to enable config auto-hide
/*var timeoutConfig = setTimeout(hideConfig,CONFIG_HIDE_DELAY*1000);
jQuery('.dg').mouseover(function(e){ clearTimeout(timeoutConfig); });*/
jQuery(".dg .close-button").click(hideConfig);
function hideConfig(){
$('body').removeClass("show-camera");
config['showCamera'] = false;
$('body').removeClass("show-fps");
config['showFPS'] = false;
$('#menu').addClass('hidden');
jQuery(gui.domElement).remove();
$('#github').remove();
}
video = document.getElementById( 'input0' );
videoContext = document.getElementById('canvas0').getContext('2d');
videoTexture = new THREE.DataTexture([],SCREEN_WIDTH,SCREEN_HEIGHT);
prevTexture = new THREE.DataTexture([],SCREEN_WIDTH,SCREEN_HEIGHT);
prevTime = -1;
scene = new THREE.Scene();
camera = new THREE.Camera();
scene.add(camera);
renderer = new THREE.WebGLRenderer();
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
renderer.domElement.width = SCREEN_WIDTH;
renderer.domElement.height = SCREEN_HEIGHT;
renderer.domElement.className = renderer.domElement.className + " render output";
renderer.autoClear = false;
document.body.insertBefore(renderer.domElement, document.body.childNodes[0]);
uniforms = {
tDiffuse: { type: "t", value: videoTexture },
tPrevious: { type: "t", value: prevTexture }
}
var geometry = new THREE.PlaneGeometry( 2, 2);
var material = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: THREE.MotionShader.vertexShader,
fragmentShader: THREE.MotionShader.fragmentShader
})
var mesh = new THREE.Mesh(geometry,material);
mesh.material.depthTest = false;
mesh.material.depthWrite = false;
scene.add(mesh);
}
function animate()
{
if ( video.readyState === video.HAVE_ENOUGH_DATA){
var time = video.currentTime;
if(time !== prevTime){
prevTexture.image.data = videoTexture.image.data;
try {
videoContext.drawImage(video, 0, 0,SCREEN_WIDTH,SCREEN_HEIGHT);
videoTexture.image.data = new Uint8Array(videoContext.getImageData(0,0,SCREEN_WIDTH, SCREEN_HEIGHT).data);
if(prevTexture.image.data.length) {
prevTexture.needsUpdate = true;
}
videoTexture.needsUpdate = true;
if(config['showFPS']) stats.update();
prevTime = time;
}catch (e) {
if (e.name == "NS_ERROR_NOT_AVAILABLE") {
//firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=879717
console.error(e);
} else {
throw e;
}
}
}
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
$("#camera0").draggable();
});
</script>
<a id="github" href="https://github.com/zebradog/camera"><img style="position: absolute; top: 0; left: 0; border: 0;" src="https://camo.githubusercontent.com/567c3a48d796e2fc06ea80409cc9dd82bf714434/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_left_darkblue_121621.png"></a>
</body>
</html>