forked from zenorocha/tracking.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor_fish_tank.html
More file actions
150 lines (120 loc) · 4.28 KB
/
Copy pathcolor_fish_tank.html
File metadata and controls
150 lines (120 loc) · 4.28 KB
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>tracking.js - color tracking fish tank</title>
<link rel="stylesheet" href="assets/demo.css">
<script src="../build/tracking-min.js"></script>
<script src="../../threejs/build/three.min.js"></script>
<style>
body {
overflow: hidden;
}
#video, #canvas {
bottom: 0;
position: absolute;
z-index: 100;
}
</style>
</head>
<body>
<div class="demo-title">
<p><a href="http://trackingjs.com" target="_parent">tracking.js</a> - use a magenta colored object to control the scene</p>
</div>
<video id="video" width="320" height="240" preload autoplay loop muted></video>
<canvas id="canvas" width="320" height="240"></canvas>
<script>
var container;
var camera, scene, renderer, group, particle;
var mouseX = 0, mouseY = 0;
var video = document.getElementById('video');
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
init();
animate();
window.onload = function() {
var tracker = new tracking.ColorTracker();
tracker.setMinDimension(5);
tracker.setMinGroupSize(10);
tracking.track('#video', tracker, { camera: true });
tracker.on('track', onColorMove);
};
function init() {
container = document.createElement('div');
document.body.appendChild(container);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3000);
camera.position.z = 1000;
scene = new THREE.Scene();
var PI2 = Math.PI * 2;
var program = function (context) {
context.beginPath();
context.arc(0, 0, 0.5, 0, PI2, true);
context.fill();
}
group = new THREE.Object3D();
scene.add(group);
for (var i = 0; i < 1000; i++) {
var material = new THREE.SpriteCanvasMaterial({
color: Math.random() * 0x808008 + 0x808080,
program: program
});
particle = new THREE.Sprite(material);
particle.position.x = Math.random() * 2000 - 1000;
particle.position.y = Math.random() * 2000 - 1000;
particle.position.z = Math.random() * 2000 - 1000;
particle.scale.x = particle.scale.y = Math.random() * 20 + 10;
group.add(particle);
}
renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function onColorMove(event) {
if (event.data.length === 0) {
return;
}
var maxRect;
var maxRectArea = 0;
event.data.forEach(function(rect) {
if (rect.width * rect.height > maxRectArea){
maxRectArea = rect.width * rect.height;
maxRect = rect;
}
});
if (maxRectArea > 0) {
var rectCenterX = maxRect.x + (maxRect.width/2);
var rectCenterY = maxRect.y + (maxRect.height/2);
mouseX = (rectCenterX - 160) * (window.innerWidth/320) * 10;
mouseY = (rectCenterY - 120) * (window.innerHeight/240) * 10;
context.clearRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = maxRect.color;
context.strokeRect(maxRect.x, maxRect.y, maxRect.width, maxRect.height);
context.font = '11px Helvetica';
context.fillStyle = "#fff";
context.fillText('x: ' + maxRect.x + 'px', maxRect.x + maxRect.width + 5, maxRect.y + 11);
context.fillText('y: ' + maxRect.y + 'px', maxRect.x + maxRect.width + 5, maxRect.y + 22);
}
}
function animate() {
window.requestAnimationFrame(animate);
render();
}
function render() {
camera.position.x += (mouseX - camera.position.x) * 0.05;
camera.position.y += (- mouseY - camera.position.y) * 0.05;
camera.lookAt(scene.position);
renderer.render(scene, camera);
}
</script>
</body>
</html>