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
|
import { Ease } from './Controller.js'
import {
delimiter,
numberAndUnit,
isPathLetter
} from '../modules/core/regex.js'
import { extend } from '../utils/adopter.js'
import Color from '../types/Color.js'
import PathArray from '../types/PathArray.js'
import SVGArray from '../types/SVGArray.js'
import SVGNumber from '../types/SVGNumber.js'
const getClassForType = (value) => {
const type = typeof value
if (type === 'number') {
return SVGNumber
} else if (type === 'string') {
if (Color.isColor(value)) {
return Color
} else if (delimiter.test(value)) {
return isPathLetter.test(value)
? PathArray
: SVGArray
} else if (numberAndUnit.test(value)) {
return SVGNumber
} else {
return NonMorphable
}
} else if (morphableTypes.indexOf(value.constructor) > -1) {
return value.constructor
} else if (Array.isArray(value)) {
return SVGArray
} else if (type === 'object') {
return ObjectBag
} else {
return NonMorphable
}
}
export default class Morphable {
constructor (stepper) {
this._stepper = stepper || new Ease('-')
this._from = null
this._to = null
this._type = null
this._context = null
this._morphObj = null
}
at (pos) {
const _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)
})
)
}
done () {
const complete = this._context
.map(this._stepper.done)
.reduce(function (last, curr) {
return last && curr
}, true)
return complete
}
from (val) {
if (val == null) {
return this._from
}
this._from = this._set(val)
return this
}
stepper (stepper) {
if (stepper == null) return this._stepper
this._stepper = stepper
return this
}
to (val) {
if (val == null) {
return this._to
}
this._to = this._set(val)
return this
}
type (type) {
// getter
if (type == null) {
return this._type
}
// setter
this._type = type
return this
}
_set (value) {
if (!this._type) {
this.type(getClassForType(value))
}
let result = (new this._type(value))
if (this._type === Color) {
result = this._to
? result[this._to[4]]()
: this._from
? result[this._from[4]]()
: result
}
if (this._type === ObjectBag) {
result = this._to
? result.align(this._to)
: this._from
? result.align(this._from)
: result
}
result = result.toArray()
this._morphObj = this._morphObj || new this._type()
this._context = this._context
|| Array.apply(null, Array(result.length))
.map(Object)
.map(function (o) {
o.done = true
return o
})
return result
}
}
export class NonMorphable {
constructor (...args) {
this.init(...args)
}
init (val) {
val = Array.isArray(val) ? val[0] : val
this.value = val
return this
}
toArray () {
return [ this.value ]
}
valueOf () {
return this.value
}
}
export class TransformBag {
constructor (...args) {
this.init(...args)
}
init (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, TransformBag.defaults, obj)
return this
}
toArray () {
const v = this
return [
v.scaleX,
v.scaleY,
v.shear,
v.rotate,
v.translateX,
v.translateY,
v.originX,
v.originY
]
}
}
TransformBag.defaults = {
scaleX: 1,
scaleY: 1,
shear: 0,
rotate: 0,
translateX: 0,
translateY: 0,
originX: 0,
originY: 0
}
const sortByKey = (a, b) => {
return (a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0))
}
export class ObjectBag {
constructor (...args) {
this.init(...args)
}
align (other) {
for (let i = 0, il = this.values.length; i < il; ++i) {
if (this.values[i] === Color) {
const space = other[i + 6]
const color = new Color(this.values.splice(i + 2, 5))[space]().toArray()
this.values.splice(i + 2, 0, ...color)
}
}
return this
}
init (objOrArr) {
this.values = []
if (Array.isArray(objOrArr)) {
this.values = objOrArr.slice()
return
}
objOrArr = objOrArr || {}
const entries = []
for (const i in objOrArr) {
const Type = getClassForType(objOrArr[i])
const val = new Type(objOrArr[i]).toArray()
entries.push([ i, Type, val.length, ...val ])
}
entries.sort(sortByKey)
this.values = entries.reduce((last, curr) => last.concat(curr), [])
return this
}
toArray () {
return this.values
}
valueOf () {
const obj = {}
const arr = this.values
// for (var i = 0, len = arr.length; i < len; i += 2) {
while (arr.length) {
const key = arr.shift()
const Type = arr.shift()
const num = arr.shift()
const values = arr.splice(0, num)
obj[key] = new Type(values).valueOf()
}
return obj
}
}
const morphableTypes = [
NonMorphable,
TransformBag,
ObjectBag
]
export function registerMorphableType (type = []) {
morphableTypes.push(...[].concat(type))
}
export function makeMorphable () {
extend(morphableTypes, {
to (val) {
return new Morphable()
.type(this.constructor)
.from(this.valueOf())
.to(val)
},
fromArray (arr) {
this.init(arr)
return this
}
})
}
|