-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspark-tween.js
134 lines (91 loc) · 2.69 KB
/
spark-tween.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
// function Tween(startVal, endVal, duration, loopCount, mirror, ease, completeCallback) {}
/*
---NOTES-----
make sure to add the following to which ever scripts need to use SparkTween
const {
Tween,
Ease,
SparkTweener
} = require("./tween.js");
SIMPLE USAGE (using the tween directly)
var sceneObject = Scene.root.find("MySceneObject);
sceneObject.transform.y = Tween(0.35, 2, 4, -1, false, Ease.LINEAR, null).animation;
EXTENDED FUNCIONALITY
var sceneObject = Scene.root.find("MySceneObject);
var tween = Tween(0.35, 2, 4, -1, false, Ease.LINEAR, null);
sceneObject.transform.y = tween.animation;
tween.Kill(); //CALL THIS WHEN YOU WANT TO KILL THE TWEEN
@chrisError 20 / 02 / 2020
*/
const Time = require('Time');
const Reactive = require('Reactive');
const Animation = require('Animation');
export const Ease = {
LINEAR: "linear",
BOUNCE_IN: "easeInBounce",
BOUNCE_OUT: "easeOutBounce",
EASE_IN_BACK: "easeInBack",
EASE_IN_CIRC: "easeInCirc",
EASE_IN_CUBIC: "easeInCubic",
EASE_IN_ELASTIC: "easeInElastic",
EASE_IN_EXPO: "easeInExpo",
EASE_IN_OUT_BACK: "easeInOutBack",
EASE_IN_OUT_BOUNCE: "easeInOutBounce",
EASE_IN_OUT_CIRC: "easeInOutCirc",
EASE_IN_OUT_ELASTIC: "easeInOutElastic",
EASE_IN_OUT_EXPO: "easeInOutExpo",
EASE_IN_OUT_QUAD: "easeInOutQuad",
EASE_IN_OUT_QUART: "easeInOutQuart",
EASE_IN_OUT_SINE: "easeInOutSine",
EASE_IN_QUAD: "easeInQuad",
EASE_IN_QUART: "easeInQuart",
EASE_IN_QUINT: "easeInQuint",
EASE_IN_SINE: "easeInSine",
EASE_OUT_BACK: "easeOutBack",
EASE_OUT_CIRC: "easeOutCirc",
EASE_OUT_CUBIC: "easeOutCubic",
EASE_OUT_ELASTIC: "easeOutElastic",
EASE_OUT_EXPO: "easeOutExpo",
EASE_OUT_QUAD: "easeOutQuad",
EASE_OUT_QUART: "easeOutQuart",
EASE_OUT_QUINT: "easeOutQuint",
EASE_OUT_SINE: "easeOutSine"
};
export class SparkTweener {
constructor(animation, driver, sub) {
this.animation = animation;
this.driver = driver;
this.sub = sub;
}
Kill() {
this.driver.stop();
if (this.sub != null) {
this.sub.unsubscribe();
}
}
}
export function Tween(startVal, endVal, duration, loopCount, mirror, ease, completeCallback) {
if (loopCount == -1) {
loopCount = Infinity;
}
var driver = Animation.timeDriver({
durationMilliseconds: duration * 1000,
loopCount: loopCount,
onComplete: completeCallback,
mirror: mirror
});
var sampler;
try {
sampler = Animation.samplers[ease](startVal, endVal);
} catch (e) {
sampler = Animation.samplers.linear(startVal, endVal);
}
var sub = null;
if (completeCallback != null) {
var sub = driver.onCompleted().subscribe(completeCallback);
driver.callback = sub;
}
driver.start();
var tweener = new SparkTweener(Animation.animate(driver, sampler), driver, sub);
return tweener;
}