aboutsummaryrefslogtreecommitdiffstats
path: root/src/morph.js
blob: 7e99fef44ee2bd08e52cb6f5759b2e6a9c49146b (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
SVG.Morphable = SVG.invent{
  create: function (controller) {
    this.controller = controller || function (from, to, pos) {
      return pos < 1 ? from : to
    }
  },

  extend: {

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

    to: function (val, modifier) {
      this._to = this._set(val)
      this.modifier = modifier
      return this
    }

    type: function (type) {
      this._type = type
      return this
    }

    _set: function (value) {

      if(!this._type)  {
        if (SVG.Color.isColor(val)) {
          this._type = SVG.Color

        } else if (SVG.regex.delimiter.test(val)) {

          this._type = SVG.regex.pathLetters.test(val)
            ? SVG.PathArray
            : SVG.Array

        } else if (SVG.regex.numberAndUnit.test(val)) {
          this._type = SVG.Number

        } else if (value in SVG.MorphableTypes) {
          this._type = value.constructor

        // } else if (typeof value == 'object') {
        //   this._type = SVG.Morphable.TransformBag
        } else {
          this._type = SVG.Morphable.NonMorphable
        }
      }

      return (new this._type(value)).toArray()
    }

    controller: function (controller) {
      this._controller = controller
    }

    at: function (pos) {

      var _this = this

      // for(var i = 0, len = this._from.length; i < len; ++i) {
      //   arr.push(this.controller(this._from[i], this._to[i]))
      // }

      return this.type.fromArray(this.modifier(this._from.map(function (i, index) {
        return _this.controller(i, _this._to[i], pos)
      })))
    },

    valueOf: function () {
      return this._value
    }
  }
}

SVG.Morphable.NonMorphable = SVG.invent({
  create: function (val) {
    this.value = val
  },

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

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

    fromArray: function (arr) {
      return new SVG.Morphable.NonMorphable(arr[0])
    }
  }
})

SVG.Morphable.TransformBag = SVG.invent({
  create: function (val) {
    this.value = new Matrix(val).decompose()
  },

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

    toArray: function (){
      var v = this.value

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

    fromArray: function (arr) {
      return new SVG.Morphable.TransformBag({
        scaleX: arr[0],
        scaleY: arr[1],
        shear: arr[2],
        rotate: arr[3],
        translateX: arr[4],
        translateY: arr[5]
      })
  }
})

SVG.MorphableTypes = [SVG.Number, SVG.Color, SVG.Box, SVG.Matrix, SVG.Morphable.NonMorphable, SVG.Morphable.TransformBag]
SVG.extend(SVG.MorphableTypes, {
  to: (item, args) => {
    let a = new SVG.Morphable().type(this.constructor).to(item, args)
  },
})




// - Objects are just variable bags
// - morph rerutrns a morphable. No state on normal objects (like SVG.Color)
// - Objects can be represented as Array (with toArray())
// - Objects have an unmorph/fromarray function which converts it back to a normal object

// var b = new Color('#fff')
// b.morph('#000') === new Morph(b).to('#000')

// sweet = Color('#fff')
// dark = Color('#fef')
// sweet.to(dark, 'hsl')

// angle = Number(30)
// lastAngle = Number(300)
// angle.to(lastAngle, cyclic)

// mat1 = Matrix().transform({rotation: 30, scale: 0})
// mat2 = Matrix(30, 40, 50, 60, 10, 20)
// mat1.to(mat2)



/**
 ** absolute transformations
 **/

// M v -----|-----(D M v = I v)------|----->  T v
//
// 1. define the final state (T) and decompose it (once) t = [tx, ty, the, lam, sy, sx]
// 2. on every frame: pull the current state of all previous transforms (M - m can change)
//   and then write this as m = [tx0, ty0, the0, lam0, sy0, sx0]
// 3. Find the interpolated matrix I(pos) = m + pos * (t - m)
//   - Note I(0) = M
//   - Note I(1) = T
// 4. Now you get the delta matrix as a result: D = I * inv(M)




el.animate().trasform({rotate: 720, scale: 2}, true)

el.animate().scale(2).rotate(720)

el.animate().transform({origin: traslate, })

absolute -> start at current - {affine params}
relative -> start at 0 always - {random stuff}


 |> object.toArray()
 |> (_) => _.map(() => {})
 |> modifier
 |> fromArray

function transform(transforms, relative, affine) {
  affine = transforms.affine || affine
  relative = transforms.relative || relative

  // 1.  define the final state (T) and decompose it (once) t = [tx, ty, the, lam, sy, sx]
  var morpher = new SVG.Morphable.TransformBag().to(transforms)

  // make sure you have an identity matrix defined as default for relative transforms
  var morpher.from()
  var el = this.target()

  var initFn = relative ? function() {} : function() {
    // 2. on every frame: pull the current state of all previous transforms (M - m can change)
    morpher.from(el.transform())
  }

  this.queue(initFn, function (pos) {
    // 3. Find the interpolated matrix I(pos) = m + pos * (t - m)
    //   - Note I(0) = M
    //   - Note I(1) = T
    var matrix = morpher.at(pos)

    if(!relative) {
      // 4. Now you get the delta matrix as a result: D = I * inv(M)
      matrix = matrix.multiply(el.transform().inverse())
    }

    el.pushTransform(matrix)
  })
}


SVG.Morphable.TransformList = Object

  if(affine) {
    var morpher = new Matrix().to(transforms)
  }

  if(input is typeof plain object) {
    // deal with a ttransformList
    this.type = SVG.Morphable.TransformList
  }

  var morpher = new Morphable(modifier).to(transforms)

  this.queue(() => {
    morpher.from(this.transform())
  }, (pos) => {
    var matrix = morpher.at(pos)
    el.transform(matrix)
  })

el.transform({rotate: 720, sclae: 2, })

el.scale(2)
  .rotate(720)

from -> 300
to -> [295, 305]

from -> 358
to -> 1



from -> 300
to -> 30

function transform(transforms, affine) {

  if(relative) {

    var morpher = new Morphable().to(transforms, affine)
    this.queue(() => {}, (pos) => {
      var matrix = morpher.at(pos)
      el.transform(matrix)
    })

  } else {

    this.queue(() => {
      morpher
    }, (pos) => {
      var matrix = morpher.at(pos)
      el.transform(matrix)
    })

  }
}



// el.animate().rotate(480)
function rotate(val) { // Relative
  var morpher = new Morphable().from(0).to(val)
  this.queue(() => {}, (pos)=> {
    var rotation = morpher.at(pos)
    el.rotate(rotation)
  })
}
// morph = new Morphable(0).to(50, [0, 360]) -> in timeline
//
//
//
// on each frame
// el.rotate(morph.at(pos))


Morph.modifiers = {

  hsb:


}


new Color(#fff).to('#000', 'hsb')

at returns a matrix anyway

new Number()


el.animate().fill('#000', 'hsb')
function fill(val, colorspace) {
  var morpher = new Morphable().to(val, colorspace || 'rgb')

  this.queue((val) => {
    morpher.from(val)
  }, (pos)=> {
    var color = morpher.at(pos)
    el.fill(color)
  })
}

//
// Number.toArray() -> [3]
// Color.toArray() -> [red, green, blue]
//
//
//
//
//
//
//
//
//









new Color(30, 50, 40).toArray()




new PathArray([['M', 0, 3], ['L', 4, 5]]).morph(5, 3, 2, 8, 5)

controller = (s, e, p)=> {return s + (e-s) * p}

[['M', 0, 3], ['L', 4, 5], ['A', 120, 120, 1, 0]]


['1', '2', '3'] => parseFloat()


rect.anim()
  .color('blue')
  .anim()
  .color(new Color('red'))



a = new SVG.Color('#3f2').to('#5f4').at(0.3)


new Morphable('#3f2').to('#5f4').at(0.4)







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