-
Notifications
You must be signed in to change notification settings - Fork 1
/
clanim.js
221 lines (203 loc) · 6.87 KB
/
clanim.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
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
;(function () {
var vendors = ['moz', 'webkit']
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']
window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame']
}
var transformStyles = [
'WebkitTransform',
'MozTransform',
'msTransform',
'OTransform',
'transform'
]
var transitionEndEvents = {
WebkitTransition: 'webkitTransitionEnd',
MozTransition: 'transitionend',
msTransition: 'MSTransitionEnd',
OTransition: 'oTransitionEnd',
transition: 'transitionend'
}
var transformStyle = getStyle(transformStyles)
var transitionStyle = getStyle(Object.keys(transitionEndEvents))
var transitionEndEvent = transitionEndEvents[transitionStyle]
function getStyle (styles) {
var elt = document.createElement('div')
return styles.reduce(function (result, style) {
return elt.style[style] !== undefined
? style
: result
}, undefined)
}
function identity (x) {
return x
}
function ElementAttribute (name) {
this.name = name
this.setStart = function (animation) {
var value = animation.elt[name]
animation.$start[name] = value
return value !== undefined && animation.$end[name] !== undefined
}
this.applyCurrent = function (animation) {
animation.elt[name] = animation.$current[name]
}
}
function StyleAttribute (name, unit, defaultValue, wrap) {
wrap = wrap || identity
this.name = name
this.setStart = function (animation) {
var value = parseFloat(animation.elt.style[name])
if (isNaN(value)) {
value = animation.$current[name] || defaultValue
}
animation.$start[name] = value
return animation.$end[name] !== undefined
}
this.applyCurrent = function (animation) {
animation.elt.style[name] = wrap(animation.$current[name]) + unit
}
}
function TransformAttribute (name, unit, defaultValue, wrap) {
wrap = wrap || identity
this.name = name
this.setStart = function (animation) {
var value = animation.$current[name]
if (value === undefined) {
value = defaultValue
}
animation.$start[name] = value
if (animation.$end[name] === undefined) {
animation.$end[name] = value
}
return value !== undefined
}
this.applyCurrent = function (animation) {
var value = animation.$current[name]
return value !== defaultValue && name + '(' + wrap(value) + unit + ')'
}
}
var attributes = [
new ElementAttribute('scrollTop'),
new ElementAttribute('scrollLeft'),
new StyleAttribute('opacity', '', 1),
new StyleAttribute('zIndex', '', 0),
new TransformAttribute('translateX', 'px', 0, Math.round),
new TransformAttribute('translateY', 'px', 0, Math.round),
new TransformAttribute('scale', '', 1),
new TransformAttribute('rotate', 'deg', 0)
].concat([
'width',
'height',
'top',
'right',
'bottom',
'left'
].map(function (name) {
return new StyleAttribute(name, 'px', 0, Math.round)
}))
function Animation (elt) {
this.elt = elt
this.$current = {}
this.$pending = {}
}
attributes.map(function (attribute) {
return attribute.name
}).concat('duration', 'easing', 'delay').forEach(function (name) {
Animation.prototype[name] = function (val) {
this.$pending[name] = val
return this
}
})
Animation.prototype.start = function (endCb, stepCb) {
var useTransition = false
if (typeof endCb === 'boolean') {
useTransition = endCb
endCb = arguments[1]
stepCb = arguments[2]
}
var animation = this
animation.stop()
animation.$start = {}
animation.$end = animation.$pending
animation.$pending = {}
animation.$attributes = attributes.filter(function (attribute) {
return attribute.setStart(animation)
})
animation.$end.duration = animation.$end.duration || 0
animation.$end.delay = animation.$end.delay || 0
animation.$end.easing = window.BezierEasing.css[animation.$end.easing] || window.BezierEasing.css['ease-out']
animation.$end.endCb = typeof endCb === 'function' && endCb
animation.$end.stepCb = typeof stepCb === 'function' && stepCb
animation.$startTime = Date.now() + animation.$end.delay
animationLoop.call(animation, animation.$end.duration && useTransition)
return animation.elt
}
Animation.prototype.stop = function () {
window.cancelAnimationFrame(this.$requestId)
}
function animationLoop (useTransition) {
var animation = this
var elt = this.elt
function onTransitionEnd (evt) {
if (evt.target === elt) {
elt.removeEventListener(transitionEndEvent, onTransitionEnd)
var endCb = animation.$end.endCb
animation.$end.endCb = undefined
endCb && endCb()
}
}
var progress = (Date.now() - animation.$startTime) / animation.$end.duration
var transition = ''
if (useTransition) {
progress = 1
var transitions = [
'all',
animation.$end.duration + 'ms',
animation.$end.easing.toCSS()
]
animation.$end.delay && transitions.push(animation.$end.delay + 'ms')
transition = transitions.join(' ')
if (animation.$end.endCb) {
elt.addEventListener(transitionEndEvent, onTransitionEnd)
}
} else if (progress < 1) {
animation.$requestId = window.requestAnimationFrame(animationLoop.bind(animation, false))
if (progress < 0) {
return
}
} else if (animation.$end.endCb) {
animation.$requestId = window.requestAnimationFrame(animation.$end.endCb)
}
var coeff = animation.$end.easing.get(progress)
var transforms = animation.$attributes.reduce(function (transforms, attribute) {
if (progress < 1) {
var diff = animation.$end[attribute.name] - animation.$start[attribute.name]
animation.$current[attribute.name] = animation.$start[attribute.name] + diff * coeff
} else {
animation.$current[attribute.name] = animation.$end[attribute.name]
}
var transform = attribute.applyCurrent(animation)
if (transform) {
transforms.push(transform)
}
return transforms
}, [])
transforms.length && transforms.push('translateZ(0)') // activate GPU
var transform = transforms.join(' ')
elt.style[transformStyle] = transform
elt.style[transitionStyle] = transition
animation.$end.stepCb && animation.$end.stepCb()
}
// Pattern: http://lea.verou.me/2015/04/idea-extending-native-dom-prototypes-without-collisions/
Object.defineProperty(window.Element.prototype, 'clanim', {
get: function () {
Object.defineProperty(this, 'clanim', {
value: new Animation(this)
})
return this.clanim
},
configurable: true,
writeable: false
})
})()