color_camera.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>tracking.js - color with camera</title>
  6. <link rel="stylesheet" href="assets/demo.css">
  7. <script src="../build/tracking-min.js"></script>
  8. <script src="../node_modules/dat.gui/build/dat.gui.min.js"></script>
  9. <script src="assets/stats.min.js"></script>
  10. <script src="assets/color_camera_gui.js"></script>
  11. <style>
  12. video, canvas {
  13. margin-left: 100px;
  14. margin-top: 35px;
  15. position: absolute;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="demo-title">
  21. <p><a href="http://trackingjs.com" target="_parent">tracking.js</a> - choose the colors you want to detect through the controls on the right</p>
  22. </div>
  23. <div class="demo-frame">
  24. <div class="demo-container">
  25. <video id="video" width="600" height="450" preload autoplay loop muted controls></video>
  26. <canvas id="canvas" width="600" height="450"></canvas>
  27. </div>
  28. </div>
  29. <script>
  30. window.onload = function() {
  31. var video = document.getElementById('video');
  32. var canvas = document.getElementById('canvas');
  33. var context = canvas.getContext('2d');
  34. var tracker = new tracking.ColorTracker();
  35. tracking.track('#video', tracker, { camera: true });
  36. tracker.on('track', function(event) {
  37. context.clearRect(0, 0, canvas.width, canvas.height);
  38. event.data.forEach(function(rect) {
  39. if (rect.color === 'custom') {
  40. rect.color = tracker.customColor;
  41. }
  42. context.strokeStyle = rect.color;
  43. context.strokeRect(rect.x, rect.y, rect.width, rect.height);
  44. context.font = '11px Helvetica';
  45. context.fillStyle = "#fff";
  46. context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);
  47. context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);
  48. });
  49. });
  50. initGUIControllers(tracker);
  51. };
  52. </script>
  53. </body>
  54. </html>