You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 年之前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # raf
  2. request animation frame
  3. ## Installation
  4. $ component install component/raf
  5. ## Example
  6. Request the animation frame with `raf(fn)`, cancel with `raf.cancel(id)`.
  7. ```js
  8. var x = 0;
  9. var y = 50;
  10. var canvas = document.querySelector('canvas');
  11. var ctx = canvas.getContext('2d');
  12. var raf = require('raf');
  13. function animate() {
  14. raf(animate);
  15. draw();
  16. }
  17. var prev = Date.now();
  18. function draw() {
  19. var curr = Date.now();
  20. var diff = curr - prev;
  21. var p = diff / 16;
  22. ctx.clearRect(0, 0, 900, 300);
  23. ctx.beginPath();
  24. ctx.globalAlpha = .5;
  25. ctx.arc(x, y, 10, 0, Math.PI * 2, false);
  26. ctx.fill();
  27. x += 2;
  28. y += Math.sin(x/20) * 5;
  29. prev = curr;
  30. }
  31. animate();
  32. ```
  33. ## License
  34. MIT