Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. /**
  2. * Created by yj on 16/5/1.
  3. */
  4. class Animator{
  5. constructor(duration,progress,easing){
  6. this.duration = duration
  7. this.progress = progress
  8. this.easing = easing || function(p) { return p}
  9. }
  10. start(finished){
  11. var startTime = Date.now()
  12. var duration = this.duration,
  13. self = this;
  14. requestAnimationFrame(function step(){
  15. var p = (Date.now() - startTime) / duration;
  16. var next = true;
  17.  
  18. if(p < 1.0){
  19. self.progress(self.easing(p),p);
  20. }else{
  21. if(typeof finished === 'function'){
  22. next = finished() === false;
  23. }else{
  24. next = finished === false;
  25. }
  26. if(!next){
  27. self.progress(self.easing(1.0),1.0)
  28. }else{
  29. startTime += duration;
  30. self.progress(self.easing(p),p)
  31. }
  32. }
  33. if(next){
  34. requestAnimationFrame(step);
  35. }
  36. })
  37. }
  38. }
  39. class AnimationQueue{
  40. constructor(animators){
  41. this.animators = animators || []
  42. }
  43. append(...args){
  44. this.animators.push.apply(this.animators,args)
  45. }
  46. flush(){
  47. if(this.animators.length){
  48. var self = this
  49. function play(){
  50. var animator = self.animators.shift()
  51. if(animator instanceof Animator) {
  52. animator.start(function () {
  53. if (self.animators.length) {
  54. play();
  55. }
  56. })
  57. }else{
  58. animator.apply(self)
  59. if(self.animators.length){
  60. play()
  61. }
  62. }
  63. }
  64. play()
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement