Source: raf.js

  1. /**
  2. * Copyright (c) 2015 NAVER Corp.
  3. * egjs projects are licensed under the MIT license
  4. */
  5. eg.module("raf", [eg, window], function(ns, global) {
  6. "use strict";
  7. var raf = global.requestAnimationFrame || global.webkitRequestAnimationFrame ||
  8. global.mozRequestAnimationFrame || global.msRequestAnimationFrame;
  9. var caf = global.cancelAnimationFrame || global.webkitCancelAnimationFrame ||
  10. global.mozCancelAnimationFrame || global.msCancelAnimationFrame;
  11. // https://gist.github.com/paulirish/5438650
  12. (function() {
  13. if ("performance" in global === false) {
  14. global.performance = {};
  15. }
  16. global.Date.now = (global.Date.now || function () { // thanks IE8
  17. return new global.Date().getTime();
  18. });
  19. if ("now" in global.performance === false) {
  20. var nowOffset = global.Date.now();
  21. if (global.performance.timing && global.performance.timing.navigationStart) {
  22. nowOffset = global.performance.timing.navigationStart;
  23. }
  24. global.performance.now = function now() {
  25. return global.Date.now() - nowOffset;
  26. };
  27. }
  28. })();
  29. if (raf && !caf) {
  30. var keyInfo = {};
  31. var oldraf = raf;
  32. raf = function(callback) {
  33. function wrapCallback(timestamp) {
  34. if (keyInfo[key]) {
  35. callback(timestamp);
  36. }
  37. }
  38. var key = oldraf(wrapCallback);
  39. keyInfo[key] = true;
  40. return key;
  41. };
  42. caf = function(key) {
  43. delete keyInfo[key];
  44. };
  45. } else if (!(raf && caf)) {
  46. raf = function(callback) {
  47. return global.setTimeout(function() {
  48. callback(global.performance.now());
  49. }, 16);
  50. };
  51. caf = global.clearTimeout;
  52. }
  53. /**
  54. * A polyfill for the window.requestAnimationFrame() method.
  55. * @ko window.requestAnimationFrame() 메서드의 polyfill 함수다
  56. * @method eg#requestAnimationFrame
  57. * @param {Function} timer The function returned through a call to the requestAnimationFrame() method <ko>requestAnimationFrame() 메서드가 호출할 함수</ko>
  58. * @return {Number} ID of the requestAnimationFrame() method. <ko>requestAnimationFrame() 메서드의 아이디</ko>
  59. * @example
  60. var timerId = eg.requestAnimationFrame(function() {
  61. console.log("call");
  62. });
  63. * @see https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
  64. */
  65. ns.requestAnimationFrame = function(fp) {
  66. return raf(fp);
  67. };
  68. /**
  69. * A polyfill for the window.cancelAnimationFrame() method. It cancels an animation executed through a call to the requestAnimationFrame() method.
  70. * @ko window.cancelAnimationFrame() 메서드의 polyfill 함수다. requestAnimationFrame() 메서드로 실행한 애니메이션을 중단한다
  71. * @method eg#cancelAnimationFrame
  72. * @param {Number} key − The ID value returned through a call to the requestAnimationFrame() method. <ko>requestAnimationFrame() 메서드가 반환한 아이디 값</ko>
  73. * @example
  74. eg.cancelAnimationFrame(timerId);
  75. * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/cancelAnimationFrame
  76. */
  77. ns.cancelAnimationFrame = function(key) {
  78. caf(key);
  79. };
  80. });
comments powered by Disqus