Skip to content

Commit 4505df7

Browse files
Adds coordinates on fish tank examples
1 parent 4fc13c8 commit 4505df7

2 files changed

Lines changed: 32 additions & 25 deletions

File tree

examples/color_fish_tank.html

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,10 @@
1313
overflow: hidden;
1414
}
1515

16-
video {
16+
#video, #canvas {
1717
bottom: 0;
1818
position: absolute;
19-
}
20-
21-
#rectangle {
22-
width: 120px;
23-
height: 80px;
24-
position: absolute;
25-
left: -1000px;
26-
top: -1000px;
27-
z-index: 1;
19+
z-index: 100;
2820
}
2921
</style>
3022
</head>
@@ -33,15 +25,16 @@
3325
<p><a href="http://trackingjs.com">tracking.js</a> - get user's webcam and detect colors to control the scene</p>
3426
</div>
3527

36-
<div id="rectangle"></div>
37-
<video id="video" width="320" height="240" preload autoplay loop muted>
28+
<video id="video" width="320" height="240" preload autoplay loop muted></video>
29+
<canvas id="canvas" width="320" height="240"></canvas>
3830

3931
<script>
4032
var container;
4133
var camera, scene, renderer, group, particle;
4234
var mouseX = 0, mouseY = 0;
43-
var rectangle = document.getElementById('rectangle');
44-
var video = document.querySelector('#video');
35+
var video = document.getElementById('video');
36+
var canvas = document.getElementById('canvas');
37+
var context = canvas.getContext('2d');
4538

4639
var windowHalfX = window.innerWidth / 2;
4740
var windowHalfY = window.innerHeight / 2;
@@ -112,12 +105,12 @@
112105

113106
function onColorMove(event) {
114107
if (event.data.length === 0) {
115-
rectangle.style.top = '-1000px';
116108
return;
117109
}
118110

119-
var maxRectArea = 0;
120111
var maxRect;
112+
var maxRectArea = 0;
113+
121114
event.data.forEach(function(rect) {
122115
if (rect.width * rect.height > maxRectArea){
123116
maxRectArea = rect.width * rect.height;
@@ -131,24 +124,25 @@
131124
mouseX = (rectCenterX - 160) * (window.innerWidth/320) * 10;
132125
mouseY = (rectCenterY - 120) * (window.innerHeight/240) * 10;
133126

134-
rectangle.style.border = '2px solid ' + maxRect.color;
135-
rectangle.style.width = maxRect.width + 'px';
136-
rectangle.style.height = maxRect.height + 'px';
137-
rectangle.style.left = (video.offsetLeft + maxRect.x) + 'px';
138-
rectangle.style.top = (video.offsetTop + maxRect.y) + 'px';
127+
context.clearRect(0, 0, canvas.width, canvas.height);
128+
context.strokeStyle = maxRect.color;
129+
context.strokeRect(maxRect.x, maxRect.y, maxRect.width, maxRect.height);
130+
context.font = '11px Helvetica';
131+
context.fillStyle = "#fff";
132+
context.fillText('x: ' + maxRect.x + 'px', maxRect.x + maxRect.width + 5, maxRect.y + 11);
133+
context.fillText('y: ' + maxRect.y + 'px', maxRect.x + maxRect.width + 5, maxRect.y + 22);
139134
}
140135
}
141136

142137
function animate() {
143-
requestAnimationFrame(animate);
138+
window.requestAnimationFrame(animate);
144139
render();
145140
}
146141

147142
function render() {
148143
camera.position.x += (mouseX - camera.position.x) * 0.05;
149144
camera.position.y += (- mouseY - camera.position.y) * 0.05;
150145
camera.lookAt(scene.position);
151-
152146
renderer.render(scene, camera);
153147
}
154148
</script>

examples/face_fish_tank.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
overflow: hidden;
1717
}
1818

19-
video {
19+
#video, #canvas {
2020
bottom: 0;
2121
position: absolute;
22+
z-index: 100;
2223
}
2324

2425
#viewport {
@@ -34,11 +35,15 @@
3435

3536
<div id="viewport">
3637
<video id="video" width="320" height="240" preload autoplay loop muted></video>
38+
<canvas id="canvas" width="320" height="240"></canvas>
3739
</div>
3840

3941
<script>
4042
var viewport = document.getElementById('viewport');
41-
fishTankRenderer = new FishTankRenderer();
43+
var canvas = document.getElementById('canvas');
44+
var context = canvas.getContext('2d');
45+
46+
var fishTankRenderer = new FishTankRenderer();
4247
fishTankRenderer.init(viewport);
4348

4449
var faceX = 0;
@@ -59,6 +64,14 @@
5964
maxRectArea = rect.width * rect.height;
6065
maxRect = rect;
6166
}
67+
68+
context.clearRect(0, 0, canvas.width, canvas.height);
69+
context.strokeStyle = 'magenta';
70+
context.strokeRect(rect.x, rect.y, rect.width, rect.height);
71+
context.font = '11px Helvetica';
72+
context.fillStyle = "#fff";
73+
context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);
74+
context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);
6275
});
6376

6477
if(maxRectArea > 0) {

0 commit comments

Comments
 (0)