1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
// Must Change ....
SVG.easing = {
'-': function (pos) { return pos },
'<>': function (pos) { return -Math.cos(pos * Math.PI) / 2 + 0.5 },
'>': function (pos) { return Math.sin(pos * Math.PI / 2) },
'<': function (pos) { return -Math.cos(pos * Math.PI / 2) + 1 }
}
var time = performance || Date
var makeSchedule = function (time) {
return function (runner) {
// FIXME: This returns a wrong value when the runner is finished
// or it will break, because the runner clips his tim to duration
var start = time - runner.time()
var duration = runner.duration()
var end = start + duration
return {start: start, duration: duration, end: end, runner: runner}
}
}
SVG.Timeline = SVG.invent({
inherit: SVG.EventTarget,
// Construct a new timeline on the given element
create: function (element) {
// Store a reference to the element to call its parent methods
this._element = element || null
this._timeSource = function () {
return time.now()
}
this._dispatcher = document.createElement('div')
// Store the timing variables
this._startTime = 0
this._speed = 1.0
// Play control variables control how the animation proceeds
this._reverse = false
this._persist = 0
// Keep track of the running animations and their starting parameters
this._baseTransform = null
this._nextFrame = null
this._paused = false
this._runners = new Set()
this._time = 0
this._lastSourceTime = 0
this._lastStepTime = 0
},
extend: {
getEventTarget () {
return this._dispatcher
},
// FIXME: there is no need anymore to save the element on the timeline
element (element) {
if(element == null) return this._element
this._element = element
},
/**
*
*/
// schedules a runner on the timeline
schedule (runner, delay, when) {
// FIXME: Sets do not support map which makes this super ugly
if(runner == null) {
var ret = []
var fn = makeSchedule(this._time)
this._runners.forEach(function (runner) {
ret.push(fn(runner))
})
ret.sort(function (a, b) {
return (a.start - b.start) || (a.duration - b.duration)
})
return ret
}
// The start time for the next animation can either be given explicitly,
// derived from the current timeline time or it can be relative to the
// last start time to chain animations direclty
var absoluteStartTime
delay = delay || 0
// Work out when to start the animation
if (when == null || when === 'last' || when === 'after') {
// Take the last time and increment
absoluteStartTime = this._startTime + delay
} else if (when === 'absolute' || when === 'start' ) {
absoluteStartTime = delay
} else if (when === 'now') {
absoluteStartTime = this._time + delay
} else if ( when === 'relative' ) {
absoluteStartTime = delay
if(this._runners.has(runner)) {
absoluteStartTime += this._time - runner.time()
}
} else {
// TODO: Throw error
}
runner.unschedule()
runner.timeline(this)
runner.time(-absoluteStartTime)
this._startTime = absoluteStartTime + runner.duration()
this._runners.add(runner)
this._continue()
return this
},
// remove the runner from this timeline
unschedule (runner) {
this._runners.delete(runner)
runner.timeline(null)
return this
},
play () {
// Now make sure we are not paused and continue the animation
this._paused = false
return this._continue()
},
pause () {
// Cancel the next animation frame and pause
this._nextFrame = null
this._paused = true
return this
},
stop () {
// Cancel the next animation frame and go to start
this.seek(-this._time)
return this.pause()
},
finish () {
this.seek(Infinity)
return this.pause()
},
speed (speed) {
if(speed == null) return this._speed
this._speed = speed
return this
},
reverse (yes) {
var currentSpeed = this.speed()
if(yes == null) return this.speed(-currentSpeed)
var positive = Math.abs(currentSpeed)
return this.speed(yes ? positive : -positive)
},
seek (dt) {
this._time += dt
return this._continue()
},
time (time) {
if(time == null) return this._time
this._time = time
return this
},
persist (dtOrForever) {
if (tdOrForever == null) return this._persist
this._persist = dtOrForever
return this
},
source (fn) {
if (fn == null) return this._timeSource
this._timeSource = fn
return this
},
_step () {
// If the timeline is paused, just do nothing
if (this._paused) return
// Get the time delta from the last time and update the time
// TODO: Deal with window.blur window.focus to pause animations
var time = this._timeSource()
var dtSource = time - this._lastSourceTime
var dtTime = this._speed * dtSource + (this._time - this._lastStepTime)
this._lastSourceTime = time
// Update the time
this._time += dtTime
this._lastStepTime = this._time
// this.fire('time', this._time)
// Run all of the runners directly
var runnersLeft = false
var eachFn = function (runner) {
if(!runner.active) return
// FIXME: runner is always called even when done
// run the runner with delta time
var finished = runner.step(dtTime).done
// when finished set flag and return
if (!finished) {
runnersLeft = true
return
}
// Remove the runner from the set
// this._runner.
// Runner is finished and might get removed
if(this._persist !== true) {
// Figure out end time of the runner
var endTime = runner.duration() - runner.time() + this._time
// Remove runner if too old
if(endTime + this._persist < this._time) {
runner.unschedule()
}
}
}.bind(this)
this._runners.forEach(eachFn)
// Get the next animation frame to keep the simulation going
if (runnersLeft)
this._nextFrame = SVG.Animator.frame(this._step.bind(this))
else this._nextFrame = null
return this
},
// Checks if we are running and continues the animation
_continue () {
if (this._paused) return this
if (!this._nextFrame)
this._nextFrame = SVG.Animator.frame(this._step.bind(this))
return this
}
},
// These methods will be added to all SVG.Element objects
parent: SVG.Element,
construct: {
timeline: function () {
this._timeline = (this._timeline || new SVG.Timeline(this))
return this._timeline
},
}
})
|