UNPKG

3.75 kBJavaScriptView Raw
1import easings from './easings';
2import ease from './ease';
3import * as is from '../../is';
4import {bound} from '../../math';
5
6function step( self, ani, now, isCore ){
7 let isEles = !isCore;
8 let _p = self._private;
9 let ani_p = ani._private;
10 let pEasing = ani_p.easing;
11 let startTime = ani_p.startTime;
12 let cy = isCore ? self : self.cy();
13 let style = cy.style();
14
15 if( !ani_p.easingImpl ){
16
17 if( pEasing == null ){ // use default
18 ani_p.easingImpl = easings[ 'linear' ];
19
20 } else { // then define w/ name
21 let easingVals;
22
23 if( is.string( pEasing ) ){
24 let easingProp = style.parse( 'transition-timing-function', pEasing );
25
26 easingVals = easingProp.value;
27
28 } else { // then assume preparsed array
29 easingVals = pEasing;
30 }
31
32 let name, args;
33
34 if( is.string( easingVals ) ){
35 name = easingVals;
36 args = [];
37 } else {
38 name = easingVals[1];
39 args = easingVals.slice( 2 ).map( function( n ){ return +n; } );
40 }
41
42 if( args.length > 0 ){ // create with args
43 if( name === 'spring' ){
44 args.push( ani_p.duration ); // need duration to generate spring
45 }
46
47 ani_p.easingImpl = easings[ name ].apply( null, args );
48 } else { // static impl by name
49 ani_p.easingImpl = easings[ name ];
50 }
51 }
52
53 }
54
55 let easing = ani_p.easingImpl;
56 let percent;
57
58 if( ani_p.duration === 0 ){
59 percent = 1;
60 } else {
61 percent = (now - startTime) / ani_p.duration;
62 }
63
64 if( ani_p.applying ){
65 percent = ani_p.progress;
66 }
67
68 if( percent < 0 ){
69 percent = 0;
70 } else if( percent > 1 ){
71 percent = 1;
72 }
73
74 if( ani_p.delay == null ){ // then update
75
76 let startPos = ani_p.startPosition;
77 let endPos = ani_p.position;
78
79 if( endPos && isEles && !self.locked() ){
80 let newPos = {};
81
82 if( valid( startPos.x, endPos.x ) ){
83 newPos.x = ease( startPos.x, endPos.x, percent, easing );
84 }
85
86 if( valid( startPos.y, endPos.y ) ){
87 newPos.y = ease( startPos.y, endPos.y, percent, easing );
88 }
89
90 self.position( newPos );
91 }
92
93 let startPan = ani_p.startPan;
94 let endPan = ani_p.pan;
95 let pan = _p.pan;
96 let animatingPan = endPan != null && isCore;
97 if( animatingPan ){
98 if( valid( startPan.x, endPan.x ) ){
99 pan.x = ease( startPan.x, endPan.x, percent, easing );
100 }
101
102 if( valid( startPan.y, endPan.y ) ){
103 pan.y = ease( startPan.y, endPan.y, percent, easing );
104 }
105
106 self.emit( 'pan' );
107 }
108
109 let startZoom = ani_p.startZoom;
110 let endZoom = ani_p.zoom;
111 let animatingZoom = endZoom != null && isCore;
112 if( animatingZoom ){
113 if( valid( startZoom, endZoom ) ){
114 _p.zoom = bound( _p.minZoom, ease( startZoom, endZoom, percent, easing ), _p.maxZoom );
115 }
116
117 self.emit( 'zoom' );
118 }
119
120 if( animatingPan || animatingZoom ){
121 self.emit( 'viewport' );
122 }
123
124 let props = ani_p.style;
125 if( props && props.length > 0 && isEles ){
126 for( let i = 0; i < props.length; i++ ){
127 let prop = props[ i ];
128 let name = prop.name;
129 let end = prop;
130 let start = ani_p.startStyle[ name ];
131 let propSpec = style.properties[ start.name ];
132 let easedVal = ease( start, end, percent, easing, propSpec );
133
134 style.overrideBypass( self, name, easedVal );
135 } // for props
136
137 self.emit('style');
138
139 } // if
140
141 }
142
143 ani_p.progress = percent;
144
145 return percent;
146}
147
148function valid( start, end ){
149 if( start == null || end == null ){
150 return false;
151 }
152
153 if( is.number( start ) && is.number( end ) ){
154 return true;
155 } else if( (start) && (end) ){
156 return true;
157 }
158
159 return false;
160}
161
162export default step;