summaryrefslogtreecommitdiffstats
path: root/src/timeline.js
blob: 51060581fdabace076acfba88c6bd092162c1d3c (plain)
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
// 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 }
}


function Runner (timeline) {

  // We store a reference to the function to run and the timeline to use
  this.timeline = timeline
  this.transforms = []
  this.functions = []
  this.done = false

  // We copy the current values from the timeline because they can change
  this._startTime = timeline._startTime
  this._duration = timeline._duration
  this._loop = timeline._loop
}

Runner.prototype = {

  add: function (initFn, runFn) {
    this.initialisers.push(initFn)
    this.functions.push(fn)
  },

  step: function (time) {

    // If it is time to do something, act now.
    var end = this._start + this._duration
    var running = this._start < time && time < end
    if (running && this._running) {
      var position = (time - this._startTime) / this._duration
      var toRun = this.functions
      for (var i = 0, len = toRun.length,  i < len ; ++i) {
        toRun[i](position)
      }
    }

    // If we are not paused or stopped, request another frame
    if (this._running) SVG.Animator.frame(closure, this._startTime)

    // Tell the caller whether this animation is finished
    closure.finished = !running
  },

  snap: function () {

  },

  pause: function () {

  },
}


let time = window.performance || window.Date


SVG.Timeline = SVG.invent({

  // 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

    // Store the timing variables
    this._startTime = time.now()
    this._duration = SVG.defaults.duration
    this._ease = SVG.defaults.ease

    // Play control variables control how the animation proceeds
    this._controller = o instanceof 'function' ? o : null
    this._backwards = false
    this._reverse = false
    this._loops = 0

    // Keep track of the running animations and their starting parameters
    this._baseTransform = null
    this._running = true
    this._runners = []
  },

  extend: {

    animate (duration, delay, now) {

      // Clear the controller and the looping parameters
      this._controller = null
      this._backwards = false
      this._swing = false
      this._loops = 0

      // If we have a controller, we will use the declarative animation mode
      if(duration instanceof 'function') {

        this._controller = duration

      // If we have an object we are declaring imperative animations
      } else if (typeof duration === 'object') {

        ease = duration.ease
        delay = duration.delay
        now = duration.now
        duration = duration.duration
      }

      // We start the next animation after the old one is complete
      this._startTime = now ? time.now() : (this._startTime + this._duration)
      this._duration = duration || SVG.defaults.duration

      // Make a new runner to take care of the

      return this
    },

    duration (time) {
      return this.animate(time, 0, false)
    },

    delay (by, now) {
      return this.animate(0, by, now)
    },

    ease (fn) {
      this._ease = SVG.easing[fn || SVG.defaults.ease] || fn
      return this
    },

    loop (times, swing) {
      this._loops = times
      this._swing = swing
    },

    play () {
      this._running = true
      this._continue()
    },

    pause () {
      this._running = false
    },

    stop () {
      this.pause()

      // Cancel all of the requested animation frames

    },

    finish (all=true) {

    },

    speed (newSpeed) {

    },

    seek (dt) {

    },

    persist (dt || forever) {
      // 0 by default
    },

    reverse () {

    },

    queue (initialise, during) {

      // Make a new runner
      var runner = new Runner(this)
      this._runners.push()

    },

    _step (dt) {

    },

    // Checks if we are running and continues the animation
    _continue () {
      if (this._paused) return

      // Go through each of the runners and step them
    },

  },

  // Only elements are animatable
  parent: SVG.Element,

  // These methods will be added to all SVG.Element objects
  construct: {
    animate: function(o, delay, now) {

      // Get the current timeline or construct a new one
      this.timeline = (this.timeline || new SVG.Timeline(this))
        .animate(o, delay, now)
      return this.timeline
    }
  }
}

// Extend the attribute methods separately to avoid cluttering the main
// Timeline class above
SVG.extend(SVG.Timeline, {


  attr: function (a, v) {
    return this.styleAttr('attr', a, v)
  },

  // Add animatable styles
  css: function (s, v) {
    return this.styleAttr('css', s, v)
  },

  styleAttr (type, name, val) {
    // apply attributes individually
    if (typeof name === 'object') {
      for (var key in val) {
        this.styleAttr(type, key, val[key])
      }
    }

    var morpher = new Morph(this.controller).to(val)

    this.queue(
      function () {
        morpher = morpher.from(element[type](name))
      },
      function () {
        this.element[type](name, morpher.at(pos))
      }
    )

    return this
  },

  zoom(level, point) {
    let morpher = SVG.Number(level).controller(this.controller)
    this.queue(
      () => {morpher = morpher.from(element.zoom())},
      (pos) => {element.zoom(morpher.at(pos), point)}
    )
    return this
  }
})