1
- ( function ( ) {
1
+ ( function ( global ) {
2
2
"use strict"
3
+ function currentTime ( ) {
4
+ return global . $time
5
+ }
6
+
7
+ function $default_access ( target , key ) {
8
+ var h = target [ "Set" + key ]
9
+ if ( typeof h == 'function' ) return h . bind ( target )
10
+ }
3
11
4
- function animationDriver ( ) {
5
- function currentTime ( ) {
6
- return new Date ( ) . getTime ( ) / 1000
7
- }
8
-
9
- let alive = true
10
- let running = false
11
- let animations = [ ]
12
- function applyAnim ( target , meta , anim ) {
13
- let duration = meta . duration || 0.25
14
- let loop = meta . loop || 1
15
- let started = currentTime ( )
16
-
17
- let $default_access = ( target , key ) => {
18
- let h = target [ "Set" + k ]
19
- if ( typeof h == 'function' ) return h . bind ( target )
20
- }
21
- let $access = meta . $access || $default_access
22
-
23
- let tracks = [ ]
24
- for ( var k in anim ) {
25
- let fn = anim [ k ]
26
- let h = $access ( target , k )
12
+ function animationDriver ( ) {
13
+ var alive = true
14
+ var running = false
15
+ var animations = [ ]
16
+
17
+ class Anim {
18
+ constructor ( target , meta , anim ) {
19
+ var $access = meta . $access || $default_access
20
+ var tracks = this . tracks = [ ]
21
+ for ( var k in anim ) {
22
+ var fn = anim [ k ]
23
+ var h = $access ( target , k )
27
24
28
- if ( typeof h == 'function' ) {
29
- tracks . push ( t => {
30
- let value = fn ( t )
31
- if ( value != undefined ) {
32
- h . call ( null , value )
33
- }
34
- } )
35
- } else {
36
- console . error ( `No such track{${ k } }` )
25
+ if ( typeof h == 'function' ) {
26
+ tracks . push ( [ fn , h ] )
27
+ } else {
28
+ console . error ( `No such track{${ k } }` )
29
+ }
37
30
}
31
+ this . duration = meta . duration || 0.25
32
+ this . loop = meta . loop || 1
33
+ this . started = currentTime ( )
34
+ this . tracks = tracks
35
+ this . added = false
36
+ this . warm = meta . warm
38
37
}
39
-
40
- let instance = t => {
41
- let alpha = ( t - started ) / duration
42
- let lap = Math . floor ( alpha )
43
- let shouldQuit = ( loop != 0 && lap >= loop )
38
+ tick ( t ) {
39
+ var alpha = ( t - this . started ) / this . duration
40
+ var lap = Math . floor ( alpha )
41
+ var shouldQuit = ( this . loop != 0 && lap >= this . loop )
44
42
if ( shouldQuit ) {
45
43
alpha = 1
46
44
} else {
47
45
alpha -= lap
48
46
}
49
- tracks . forEach ( track => track ( alpha ) )
47
+ this . tracks . forEach ( track => {
48
+ let fn = track [ 0 ]
49
+ let h = track [ 1 ]
50
+ let value = fn ( alpha )
51
+ if ( value != undefined ) {
52
+ h . call ( null , value )
53
+ }
54
+ } )
50
55
if ( shouldQuit ) {
51
- remove ( )
56
+ this . remove ( )
52
57
}
53
58
}
54
- let added = false
55
- function add ( ) {
56
- if ( added ) return
57
- added = true
58
- animations . push ( instance )
59
+ add ( ) {
60
+ if ( this . added ) return
61
+ this . added = true
62
+ animations . push ( this )
59
63
60
- if ( meta . warm ) {
61
- instance ( 0 )
64
+ if ( this . warm ) {
65
+ this . tick ( 0 )
62
66
}
63
67
}
64
- function remove ( ) {
65
- if ( ! added ) return
66
- added = false
67
- animations . splice ( animations . indexOf ( instance ) , 1 )
68
+ remove ( ) {
69
+ if ( ! this . added ) return
70
+ this . added = false
71
+ animations . splice ( animations . indexOf ( this ) , 1 )
68
72
if ( animations . length == 0 ) {
69
73
stop ( )
70
74
}
71
75
}
76
+ }
77
+ function applyAnim ( target , meta , anim ) {
78
+ let I = new Anim ( target , meta , anim )
79
+
72
80
if ( ! running ) {
73
81
run ( )
74
82
}
75
- add ( )
76
- return remove
83
+ I . add ( )
84
+ return I . remove
77
85
}
78
86
79
87
function loop ( ) {
80
88
if ( ! running ) return
81
- let t = currentTime ( )
82
- animations . forEach ( anim => anim ( t ) )
89
+ var t = currentTime ( )
90
+ animations . forEach ( anim => anim . tick ( t ) )
83
91
process . nextTick ( loop )
84
92
}
85
93
105
113
}
106
114
107
115
module . exports = animationDriver
108
- } ) ( )
116
+ } ) ( this )
0 commit comments