aboutsummaryrefslogtreecommitdiffstats
path: root/src/controller.js
blob: 064c4f25d23ad032c3c8588292d1c444efbad916 (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
// c = {
//   finished: Whether or not we are finished
// }

/***
Base Class
==========
The base stepper class that will be
***/

SVG.Stepper = SVG.invent ({

  create: function (fn) {

  },

  extend: {

    step: function (current, target, dt, c) {

    },

    isComplete: function (dt, c) {

    },
  },
})

/***
Easing Functions
================
***/

SVG.Ease = SVG.invent ({

  inherit: SVG.Stepper,

  create: function (fn) {
    SVG.Stepper.call(this, fn)

    this.ease = SVG.easing[fn || SVG.defaults.timeline.ease] || fn
  },

  extend: {

    step: function (from, to, pos) {
      if(typeof from !== 'number') {
        return pos < 1 ? from : to
      }
      return from + (to - from) * this.ease(pos)
    },

    isComplete: function (dt, c) {
      return false
    },
  },
})

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 },
  bezier: function (t0, x0, t1, x1) {
    return function (t) {
      // TODO: FINISH
    }
  },
}


/***
Controller Types
================
***/

SVG.Controller =  SVG.invent ({

  inherit: SVG.Stepper,

  create: function (fn) {
    SVG.Stepper.call(this, fn)
    this.stepper = fn
  },

  extend: {

    step: function (current, target, dt, c) {
      return this.stepper(current, target, dt, c)
    },

    isComplete: function (dt, c) {
      return false
      var result = false
      for(var i = c.length; i--;) {
        result = result || (Math.abs(c[i].error) < 0.01)
      }
      return result
    },
  },
})

SVG.Spring = function spring(duration, overshoot) {

  // Apply the default parameters
  duration = duration || 500
  overshoot = overshoot || 15

  // Calculate the PID natural response
  var eps = 1e-10
  var os = overshoot / 100 + eps
  var zeta = -Math.log(os) / Math.sqrt(Math.PI ** 2 + Math.log(os) ** 2)
  var wn = 4 / (zeta * duration / 1000)

  // Calculate the Spring values
  var D = 2 * zeta * wn
  var K = wn * wn

  // Return the acceleration required
  return new SVG.Controller(
    function (current, target, dt, c) {

      if(dt == Infinity) return target

      // Get the parameters
      var error = target - current
      var lastError = c.error || 0
      var velocity = (error - c.error) / dt

      // Apply the control to get the new position and store it
      var control = -D * velocity - K * error
      var newPosition = current + control
      c.error = error
      return newPosition
  })
}

SVG.PID = function (P, I, D, antiwindup) {
  P = P == null ? 0.1 : P
  I = I == null ? 0.01 : I
  D = D == null ? 0 : D
  antiwindup = antiwindup == null ? 1000 : antiwindup

  // Return the acceleration required
  return new SVG.Controller(
    function (current, target, dt, c) {

      if(dt == Infinity) return target

      var p = target - current
      var i = (c.integral || 0) + p * dt
      var d = (p - (c.error || 0)) / dt

      // antiwindup
      i = Math.max(-antiwindup, Math.min(i, antiwindup))

      c.error = p
      c.integral = i

      return current + (P * p + I * i + D * d)
  })
}