follow-mouse2.js 545 B

12345678910111213141516171819202122232425262728
  1. var stage = new mtm.Stage('c'),
  2. arrow = new Arrow(stage.width/2, stage.height/2),
  3. mx = 0, my = 0,
  4. vx = 0, vy = 0,
  5. force = 0.5;
  6. stage.shapes.push(arrow);
  7. stage.play(function() {
  8. var dx = mx - arrow.x,
  9. dy = my - arrow.y,
  10. angle = Math.atan2(dy, dx),
  11. ax = Math.cos(angle) * force,
  12. ay = Math.sin(angle) * force;
  13. vx += ax;
  14. vy += ay;
  15. arrow.x += vx;
  16. arrow.y += vy;
  17. arrow.radians = angle;
  18. });
  19. document.addEventListener('mousemove', function(e) {
  20. mx = e.offsetX;
  21. my = e.offsetY;
  22. }, false);