Clip.js 2.0 KB

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