Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. window.addEventListener("load", function () {
  3.   var canvasElement = document.createElement("canvas");
  4.   var context = canvasElement.getContext("2d");
  5.  
  6.   canvasElement.width = 500;
  7.   canvasElement.height = 350;
  8.  
  9.   document.body.appendChild(canvasElement);
  10.  
  11.   var mouseX = 0, mouseY = 0;
  12.   var self = {x: 150, y: 275}, other = {x: 250, y: 175}, target = {x: 0, y: 0};
  13.  
  14.   window.requestAnimationFrame(renderFrame);
  15.  
  16.   document.addEventListener("mousemove", handleMouse);
  17.   document.addEventListener("mousedown", function () { other.x = mouseX; other.y = mouseY });
  18.  
  19.   function renderFrame() {
  20.     target.x = mouseX; target.y = mouseY;
  21.    
  22.     context.clearRect(0, 0, 500, 350);
  23.    
  24.     context.beginPath();
  25.     context.arc(self.x, self.y, 25, 0, 2 * Math.PI, false);
  26.     context.fillStyle = 'red';
  27.     context.fill();
  28.     context.closePath();
  29.    
  30.     context.beginPath();
  31.     context.arc(target.x, target.y, 25, 0, 2 * Math.PI, false);
  32.     context.fillStyle = 'rgba(255, 0, 0, 0.2)';
  33.     context.fill();
  34.     context.closePath();
  35.    
  36.     var dts = distToSegment(other, self, target);
  37.    
  38.     context.beginPath();
  39.     context.arc(other.x, other.y, 25, 0, 2 * Math.PI, false);
  40.     context.fillStyle = dts < 50 ? 'green' : 'blue';
  41.     context.fill();
  42.     context.closePath();
  43.    
  44.     var length = Math.hypot(target.x - self.x, target.y - self.y), normalX = -(target.y - self.y) / length, normalY = (target.x - self.x) / length;
  45.    
  46.     context.beginPath();
  47.     context.moveTo(self.x, self.y);
  48.     context.lineTo(target.x, target.y);
  49.     context.strokeStyle = 'black';
  50.     context.stroke();
  51.     context.closePath();
  52.    
  53.     context.beginPath();
  54.     context.moveTo(self.x - normalX * 25, self.y - normalY * 25);
  55.     context.lineTo(target.x - normalX * 25, target.y - normalY * 25);
  56.     context.strokeStyle = 'gray';
  57.     context.stroke();
  58.     context.closePath();
  59.    
  60.     context.beginPath();
  61.     context.moveTo(self.x + normalX * 25, self.y + normalY * 25);
  62.     context.lineTo(target.x + normalX * 25, target.y + normalY * 25);
  63.     context.strokeStyle = 'gray';
  64.     context.stroke();
  65.     context.closePath();
  66.    
  67.     window.requestAnimationFrame(renderFrame);
  68.   }
  69.  
  70.   function sqr(x) { return x * x }
  71.  
  72.   function dist2(v, w) { return sqr(v.x - w.x) + sqr(v.y - w.y) }
  73.  
  74.   function distToSegmentSquared(p, v, w) {
  75.     var l2 = dist2(v, w);
  76.     if (l2 == 0) return dist2(p, v);
  77.     var t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2;
  78.     if (t < 0) return dist2(p, v);
  79.     if (t > 1) return dist2(p, w);
  80.     return dist2(p, { x: v.x + t * (w.x - v.x),
  81.                       y: v.y + t * (w.y - v.y) });
  82.   }
  83.  
  84.   function distToSegment(p, v, w) { return Math.sqrt(distToSegmentSquared(p, v, w)); }
  85.  
  86.  
  87.   function handleMouse(event) {
  88.     var mouseCoordinates = getMousePosition(canvasElement, event);
  89.    
  90.     mouseX = mouseCoordinates.x;
  91.     mouseY = mouseCoordinates.y;
  92.   }
  93.  
  94.   function getMousePosition(canvasElement, event) {
  95.     var canvasBound = canvasElement.getBoundingClientRect();
  96.    
  97.     return {x: event.clientX - canvasBound.left, y: event.clientY - canvasBound.top};
  98.   }
  99. });
  100. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement