Advertisement
jusasv

Untitled

Dec 10th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Masina</title>
  6. <script src=build/tracking-min.js></script>
  7. <style>
  8. body {
  9. background-color: #fff;
  10. margin: 0px;
  11. font-family: "Arial", monospace;
  12. font-size: 30px;
  13. color: #888ed1;
  14. overflow: hidden;
  15. myVideo {
  16. margin-left: 100px;
  17. margin-top: 35px;
  18. position: absolute;
  19. }
  20.  
  21. }
  22. </style>
  23. <script src="/build/tracking-min.js"></script>
  24. </head>
  25. <body>
  26. <video id="myVideo" width="400" height="300" preload autoplay loop muted></video>
  27. <canvas id="cvs"></canvas>
  28. <script>
  29. var fps = 60; // Kadrai per sekunde
  30. var video = document.getElementById("myVideo")
  31. var canvas = document.getElementById("cvs");
  32. var ctx = canvas.getContext("2d");
  33. var m_img = new Image();
  34. m_img.src = "masina.png";
  35. var mx = 0;
  36. var my = 0;
  37. var md = false; //md - mouse down
  38. //var colors = new tracking.ColorTracker(['magenta', 'cyan', 'yellow']);
  39.  
  40. var tracker = new tracking.ColorTracker();
  41.  
  42. tracking.track("#myVideo", tracker, { camera: true });
  43.  
  44. tracker.on("track", function(event) {
  45. context.clearRect(0, 0, canvas.width, canvas.height);
  46.  
  47. event.data.forEach(function(rect) {
  48. if (rect.color === "#fff") {
  49. rect.color = tracker.customColor;
  50. }
  51.  
  52. context.strokeStyle = rect.color;
  53. context.strokeRect(rect.x, rect.y, rect.width, rect.height);
  54. context.fillStyle = "#fff";
  55. context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);
  56. context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);
  57. });
  58. });
  59.  
  60.  
  61.  
  62.  
  63. function Masina(xas, yas, spd){
  64. this.x = xas;
  65. this.y = yas;
  66. this.angle = 0; //pasisukimo kampas
  67. this.speed = spd;
  68.  
  69. this.step = function(){
  70. var dx = mx - this.x; //atstumas tarp tanko ir peles X asyje
  71. var dy = my - this.y; //atstumas Y asije
  72. this.angle = Math.atan2(dy, dx);
  73.  
  74. if(md){
  75. this.x += Math.cos(this.angle) * this.speed;
  76. this.y += Math.sin(this.angle) * this.speed;
  77. }
  78.  
  79.  
  80.  
  81. }
  82.  
  83. this.draw = function(){
  84. ctx.save();
  85. ctx.translate(this.x, this.y);
  86. ctx.rotate(this.angle);
  87. ctx.drawImage(m_img, -25, -25);
  88. ctx.restore();
  89. }
  90. }
  91.  
  92. function frame(){ // Kartoja pati save
  93. setTimeout(frame, 1000/fps);
  94. calc();
  95. requestAnimationFrame(render);
  96. }
  97.  
  98. function calc(){ // Skaiciavimai
  99. car.step();
  100. }
  101.  
  102. function render(){ // Piesimas ekrane
  103. ctx.clearRect(0,0, canvas.width, canvas.height);
  104. car.draw();
  105. }
  106.  
  107. function resize() { //canvas prisitaiko prie lango dydzio
  108. canvas.width = window.innerWidth;
  109. canvas.height = window.innerHeight;
  110. }
  111.  
  112. function mmove(evt){//kai juda pele
  113. mx = evt.clientX;
  114. my = evt.clientY;
  115. }
  116.  
  117. function mdown(){ //kai pele paspausta md = true
  118. md = true;
  119.  
  120. }
  121.  
  122. function mup(){ //kai pele paspausta md = false
  123. md = false;
  124. }
  125.  
  126. resize();
  127. var car = new Masina(canvas.width/2, canvas.height/2, 4);
  128. window.addEventListener("resize", resize);
  129. document.addEventListener("mousemove", mmove);
  130. document.addEventListener("mousedown", mdown);
  131. document.addEventListener("mouseup", mup);
  132.  
  133.  
  134. //tracking.track('#myVideo', colors);
  135.  
  136. frame(); // Kadru laikmacio startas
  137. </script>
  138. </body>
  139. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement