summaryrefslogtreecommitdiffstats
path: root/src/morph.js
blob: acb9e210e6bde4d8a39a7b8873ffb4d96fc02145 (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
SVG.Morphable = SVG.invent({
  create: function (stepper) {
    // FIXME: the default stepper does not know about easing
    this._stepper = stepper || new SVG.Ease('-')

    this._from = null
    this._to = null
    this._type = null
    this._context = null
    this._morphObj = null
  },

  extend: {

    from: function (val) {
      if (val == null) {
        return this._from
      }

      this._from = this._set(val)
      return this
    },

    to: function (val) {
      if (val == null) {
        return this._to
      }

      this._to = this._set(val)
      return this
    },

    type: function (type) {
      // getter
      if (type == null) {
        return this._type
      }

      // setter
      this._type = type
      return this
    },

    _set: function (value) {
      if (!this._type) {
        var type = typeof value

        if (type === 'number') {
          this.type(SVG.Number)
        } else if (type === 'string') {
          if (SVG.Color.isColor(value)) {
            this.type(SVG.Color)
          } else if (SVG.regex.delimiter.test(value)) {
            this.type(SVG.regex.pathLetters.test(value)
              ? SVG.PathArray
              : SVG.Array
            )
          } else if (SVG.regex.numberAndUnit.test(value)) {
            this.type(SVG.Number)
          } else {
            this.type(SVG.Morphable.NonMorphable)
          }
        } else if (SVG.MorphableTypes.indexOf(value.constructor) > -1) {
          this.type(value.constructor)
        } else if (Array.isArray(value)) {
          this.type(SVG.Array)
        } else if (type === 'object') {
          this.type(SVG.Morphable.ObjectBag)
        } else {
          this.type(SVG.Morphable.NonMorphable)
        }
      }

      var result = (new this._type(value)).toArray()
      this._morphObj = this._morphObj || new this._type()
      this._context = this._context ||
        Array.apply(null, Array(result.length)).map(Object)
      return result
    },

    stepper: function (stepper) {
      if (stepper == null) return this._stepper
      this._stepper = stepper
      return this
    },

    done: function () {
      var complete = this._context
        .map(this._stepper.done)
        .reduce(function (last, curr) {
          return last && curr
        }, true)
      return complete
    },

    at: function (pos) {
      var _this = this

      return this._morphObj.fromArray(
        this._from.map(function (i, index) {
          return _this._stepper.step(i, _this._to[index], pos, _this._context[index], _this._context)
        })
      )
    }
  }
})

SVG.Morphable.NonMorphable = SVG.invent({
  create: function (val) {
    val = Array.isArray(val) ? val[0] : val
    this.value = val
  },

  extend: {
    valueOf: function () {
      return this.value
    },

    toArray: function () {
      return [this.value]
    }
  }
})

SVG.Morphable.TransformBag = SVG.invent({
  create: function (obj) {
    if (Array.isArray(obj)) {
      obj = {
        scaleX: obj[0],
        scaleY: obj[1],
        shear: obj[2],
        rotate: obj[3],
        translateX: obj[4],
        translateY: obj[5],
        originX: obj[6],
        originY: obj[7]
      }
    }

    Object.assign(this, SVG.Morphable.TransformBag.defaults, obj)
  },

  extend: {
    toArray: function () {
      var v = this

      return [
        v.scaleX,
        v.scaleY,
        v.shear,
        v.rotate,
        v.translateX,
        v.translateY,
        v.originX,
        v.originY
      ]
    }
  }
})

SVG.Morphable.TransformBag.defaults = {
  scaleX: 1,
  scaleY: 1,
  shear: 0,
  rotate: 0,
  translateX: 0,
  translateY: 0,
  originX: 0,
  originY: 0
}

SVG.Morphable.ObjectBag = SVG.invent({
  create: function (objOrArr) {
    this.values = []

    if (Array.isArray(objOrArr)) {
      this.values = objOrArr
      return
    }

    var entries = Object.entries(objOrArr || {}).sort((a, b) => {
      return a[0] - b[0]
    })

    this.values = entries.reduce((last, curr) => last.concat(curr), [])
  },

  extend: {
    valueOf: function () {
      var obj = {}
      var arr = this.values

      for (var i = 0, len = arr.length; i < len; i += 2) {
        obj[arr[i]] = arr[i + 1]
      }

      return obj
    },

    toArray: function () {
      return this.values
    }
  }
})

SVG.MorphableTypes = [
  SVG.Number,
  SVG.Color,
  SVG.Box,
  SVG.Matrix,
  SVG.Array,
  SVG.PointArray,
  SVG.PathArray,
  SVG.Morphable.NonMorphable,
  SVG.Morphable.TransformBag,
  SVG.Morphable.ObjectBag
]

SVG.extend(SVG.MorphableTypes, {
  to: function (val, args) {
    return new SVG.Morphable()
      .type(this.constructor)
      .from(this.valueOf())
      .to(val, args)
  },
  fromArray: function (arr) {
    this.constructor(arr)
    return this
  }
})