follow-mouse.js 496 B

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