Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by yj on 16/5/1.
- */
- class Animator{
- constructor(duration,progress,easing){
- this.duration = duration
- this.progress = progress
- this.easing = easing || function(p) { return p}
- }
- start(finished){
- var startTime = Date.now()
- var duration = this.duration,
- self = this;
- requestAnimationFrame(function step(){
- var p = (Date.now() - startTime) / duration;
- var next = true;
- if(p < 1.0){
- self.progress(self.easing(p),p);
- }else{
- if(typeof finished === 'function'){
- next = finished() === false;
- }else{
- next = finished === false;
- }
- if(!next){
- self.progress(self.easing(1.0),1.0)
- }else{
- startTime += duration;
- self.progress(self.easing(p),p)
- }
- }
- if(next){
- requestAnimationFrame(step);
- }
- })
- }
- }
- class AnimationQueue{
- constructor(animators){
- this.animators = animators || []
- }
- append(...args){
- this.animators.push.apply(this.animators,args)
- }
- flush(){
- if(this.animators.length){
- var self = this
- function play(){
- var animator = self.animators.shift()
- if(animator instanceof Animator) {
- animator.start(function () {
- if (self.animators.length) {
- play();
- }
- })
- }else{
- animator.apply(self)
- if(self.animators.length){
- play()
- }
- }
- }
- play()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement