Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. var canv = document.getElementById('canv'),
  2. ctx = canv.getContext('2d');
  3. var x = 10,
  4. y = 10;
  5. var dx = 1,
  6. dy = 3;
  7. var running = false;
  8. var anim;
  9.  
  10. var fps = 60;
  11. var delay = 1000 / 60;
  12.  
  13. function draw() {
  14. ctx.clearRect(0, 0, canv.width, canv.height);
  15. ctx.save();
  16. ctx.translate(x, y);
  17. ctx.beginPath();
  18. ctx.arc(0, 0, 5, 0, Math.PI * 2);
  19. ctx.fill();
  20. ctx.restore();
  21. }
  22.  
  23. function move() {
  24. x += dx;
  25. y += dy;
  26.  
  27. if (x >= canv.width || x <= 0) {
  28. dx = -dx;
  29. }
  30. if (y <= 0 || y >= canv.height) {
  31. dy = -dy;
  32. }
  33. }
  34. var start = 0;
  35.  
  36. (function animate() {
  37. running = true;
  38. var current = new Date().getTime(),
  39. delta = current - start;
  40.  
  41. if (delta >= delay) {
  42. move();
  43. draw();
  44. start = new Date().getTime();
  45. }
  46.  
  47. anim = requestAnimationFrame(animate);
  48. })();
  49.  
  50. window.addEventListener('click', function() {
  51. if (running == true) {
  52. cancelAnimationFrame(anim);
  53. running = false;
  54. } else {
  55. anim = requestAnimationFrame(animate);
  56. running = true;
  57. }
  58. });
  59.  
  60. function animate() {
  61. running = true;
  62. var current = new Date().getTime(),
  63. delta = current - start;
  64.  
  65. if (delta >= delay) {
  66. move();
  67. draw();
  68. start = new Date().getTime();
  69. }
  70.  
  71. anim = requestAnimationFrame(animate);
  72. }
  73. animate();
  74.  
  75. function animate() {
  76. ...
  77. window.animation = requestAnimationFrame(animate);
  78. };
  79. animate();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement