mui.animationframe.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * mui animationFrame
  3. */
  4. (function($, window) {
  5. var rAF = window.requestAnimationFrame;
  6. var cAF = window.cancelAnimationFrame;
  7. var queues = {};
  8. var animationFrame = {
  9. queue : queue,
  10. queueAfter : queueAfter,
  11. cancel : cancel
  12. };
  13. function animation_id() {
  14. var id;
  15. do {
  16. id = Math.floor(Math.random() * 1E9);
  17. } while (id in queues);
  18. return id;
  19. }
  20. function recursion(callback) {
  21. var qid = animation_id();
  22. (function wrapper() {
  23. callback();
  24. queues[qid] = rAF(function() {
  25. delete queues[qid];
  26. wrapper();
  27. });
  28. })();
  29. return qid;
  30. }
  31. function queue(callback) {
  32. var qid = animation_id();
  33. queues[qid] = rAF(function() {
  34. delete queues[qid];
  35. callback.apply(animationFrame, arguments);
  36. });
  37. return qid;
  38. }
  39. function queueAfter(callback) {
  40. var qid;
  41. qid = queue(function() {
  42. queues[qid] = rAF(function() {
  43. delete queues[qid];
  44. callback.apply(animationFrame, arguments);
  45. });
  46. });
  47. return qid;
  48. }
  49. function cancel(qid) {
  50. if ( qid in queues) {
  51. cAF(queues[qid]);
  52. delete queues[qid];
  53. }
  54. return animationFrame;
  55. }
  56. $.animationFrame = animationFrame;
  57. })(mui, window);