forked from lukechinworth/vue-mixin-tween
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (34 loc) · 1.02 KB
/
index.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
import TWEEN from '@tweenjs/tween.js';
export default (propName, duration = 500, ease = TWEEN.Easing.Quadratic.Out, callback = null) => {
const propNameTween = `${propName}Tween`;
return {
data() {
return {
[propNameTween]: this[propName],
};
},
watch: {
// watch when the prop changes and tween the tween prop from old to new
[propName](newValue, oldValue) {
new TWEEN.Tween({
number: oldValue,
})
.to({
number: newValue,
}, duration)
.easing(ease)
.onUpdate(tween => {
this[propNameTween] = tween.number;
})
.onComplete(this[callback])
.start();
animate();
},
},
};
};
function animate() {
if (TWEEN.update()) {
requestAnimationFrame(animate);
}
}