-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
create-animation.js
83 lines (74 loc) · 2.22 KB
/
create-animation.js
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
const defaultOption = {
duration: 400,
timingFunction: 'linear',
delay: 0,
transformOrigin: '50% 50% 0'
}
class MPAnimation {
constructor (option) {
this.actions = []
this.currentTransform = {}
this.currentStepAnimates = []
this.option = Object.assign({}, defaultOption, option)
}
_getOption (option) {
const _option = {
transition: Object.assign({}, this.option, option)
}
_option.transformOrigin = _option.transition.transformOrigin
delete _option.transition.transformOrigin
return _option
}
_pushAnimates (type, args) {
this.currentStepAnimates.push({
type: type,
args: args
})
}
_converType (type) {
return type.replace(/[A-Z]/g, text => {
return `-${text.toLowerCase()}`
})
}
_getValue (value) {
return typeof value === 'number' ? `${value}px` : value
}
export () {
const actions = this.actions
this.actions = []
return {
actions
}
}
step (option) {
this.currentStepAnimates.forEach(animate => {
if (animate.type !== 'style') {
this.currentTransform[animate.type] = animate
} else {
this.currentTransform[`${animate.type}.${animate.args[0]}`] = animate
}
})
this.actions.push({
animates: Object.values(this.currentTransform),
option: this._getOption(option)
})
this.currentStepAnimates = []
return this
}
}
const animateTypes1 = ['matrix', 'matrix3d', 'rotate', 'rotate3d', 'rotateX', 'rotateY', 'rotateZ', 'scale', 'scale3d', 'scaleX', 'scaleY', 'scaleZ', 'skew', 'skewX', 'skewY', 'translate', 'translate3d', 'translateX', 'translateY', 'translateZ']
const animateTypes2 = ['opacity', 'backgroundColor']
const animateTypes3 = ['width', 'height', 'left', 'right', 'top', 'bottom']
animateTypes1.concat(animateTypes2, animateTypes3).forEach(type => {
MPAnimation.prototype[type] = function (...args) {
if (animateTypes2.concat(animateTypes3).includes(type)) {
this._pushAnimates('style', [this._converType(type), animateTypes3.includes(type) ? this._getValue(args[0]) : args[0]])
} else {
this._pushAnimates(type, args)
}
return this
}
})
export function createAnimation (option) {
return new MPAnimation(option)
}