|
13 | 13 | overflow: hidden; |
14 | 14 | } |
15 | 15 |
|
16 | | - video { |
| 16 | + #video, #canvas { |
17 | 17 | bottom: 0; |
18 | 18 | 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; |
28 | 20 | } |
29 | 21 | </style> |
30 | 22 | </head> |
|
33 | 25 | <p><a href="http://trackingjs.com">tracking.js</a> - get user's webcam and detect colors to control the scene</p> |
34 | 26 | </div> |
35 | 27 |
|
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> |
38 | 30 |
|
39 | 31 | <script> |
40 | 32 | var container; |
41 | 33 | var camera, scene, renderer, group, particle; |
42 | 34 | 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'); |
45 | 38 |
|
46 | 39 | var windowHalfX = window.innerWidth / 2; |
47 | 40 | var windowHalfY = window.innerHeight / 2; |
|
112 | 105 |
|
113 | 106 | function onColorMove(event) { |
114 | 107 | if (event.data.length === 0) { |
115 | | - rectangle.style.top = '-1000px'; |
116 | 108 | return; |
117 | 109 | } |
118 | 110 |
|
119 | | - var maxRectArea = 0; |
120 | 111 | var maxRect; |
| 112 | + var maxRectArea = 0; |
| 113 | + |
121 | 114 | event.data.forEach(function(rect) { |
122 | 115 | if (rect.width * rect.height > maxRectArea){ |
123 | 116 | maxRectArea = rect.width * rect.height; |
|
131 | 124 | mouseX = (rectCenterX - 160) * (window.innerWidth/320) * 10; |
132 | 125 | mouseY = (rectCenterY - 120) * (window.innerHeight/240) * 10; |
133 | 126 |
|
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); |
139 | 134 | } |
140 | 135 | } |
141 | 136 |
|
142 | 137 | function animate() { |
143 | | - requestAnimationFrame(animate); |
| 138 | + window.requestAnimationFrame(animate); |
144 | 139 | render(); |
145 | 140 | } |
146 | 141 |
|
147 | 142 | function render() { |
148 | 143 | camera.position.x += (mouseX - camera.position.x) * 0.05; |
149 | 144 | camera.position.y += (- mouseY - camera.position.y) * 0.05; |
150 | 145 | camera.lookAt(scene.position); |
151 | | - |
152 | 146 | renderer.render(scene, camera); |
153 | 147 | } |
154 | 148 | </script> |
|
0 commit comments