Clip.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import easingFuncs from './easing';
  2. var Clip = (function () {
  3. function Clip(opts) {
  4. this._initialized = false;
  5. this._startTime = 0;
  6. this._pausedTime = 0;
  7. this._paused = false;
  8. this._life = opts.life || 1000;
  9. this._delay = opts.delay || 0;
  10. this.loop = opts.loop == null ? false : opts.loop;
  11. this.gap = opts.gap || 0;
  12. this.easing = opts.easing || 'linear';
  13. this.onframe = opts.onframe;
  14. this.ondestroy = opts.ondestroy;
  15. this.onrestart = opts.onrestart;
  16. }
  17. Clip.prototype.step = function (globalTime, deltaTime) {
  18. if (!this._initialized) {
  19. this._startTime = globalTime + this._delay;
  20. this._initialized = true;
  21. }
  22. if (this._paused) {
  23. this._pausedTime += deltaTime;
  24. return;
  25. }
  26. var percent = (globalTime - this._startTime - this._pausedTime) / this._life;
  27. if (percent < 0) {
  28. percent = 0;
  29. }
  30. percent = Math.min(percent, 1);
  31. var easing = this.easing;
  32. var easingFunc = typeof easing === 'string'
  33. ? easingFuncs[easing] : easing;
  34. var schedule = typeof easingFunc === 'function'
  35. ? easingFunc(percent)
  36. : percent;
  37. this.onframe && this.onframe(schedule);
  38. if (percent === 1) {
  39. if (this.loop) {
  40. this._restart(globalTime);
  41. this.onrestart && this.onrestart();
  42. }
  43. else {
  44. return true;
  45. }
  46. }
  47. return false;
  48. };
  49. Clip.prototype._restart = function (globalTime) {
  50. var remainder = (globalTime - this._startTime - this._pausedTime) % this._life;
  51. this._startTime = globalTime - remainder + this.gap;
  52. this._pausedTime = 0;
  53. };
  54. Clip.prototype.pause = function () {
  55. this._paused = true;
  56. };
  57. Clip.prototype.resume = function () {
  58. this._paused = false;
  59. };
  60. return Clip;
  61. }());
  62. export default Clip;