-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtween.js
63 lines (52 loc) · 1.37 KB
/
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
// @ts-check
// Tweeeeeening!
kaplay({
background: [141, 183, 255],
});
loadSprite("bean", "/sprites/bean.png");
const duration = 1;
const easeTypes = Object.keys(easings);
let curEaseType = 0;
const bean = add([
sprite("bean"),
scale(2),
pos(center()),
rotate(0),
anchor("center"),
]);
const label = add([
text(easeTypes[curEaseType], { size: 64 }),
pos(24, 24),
]);
add([
text("Click anywhere & use arrow keys", { width: width() }),
anchor("botleft"),
pos(24, height() - 24),
]);
onKeyPress(["left", "a"], () => {
curEaseType = curEaseType === 0 ? easeTypes.length - 1 : curEaseType - 1;
label.text = easeTypes[curEaseType];
});
onKeyPress(["right", "d"], () => {
curEaseType = (curEaseType + 1) % easeTypes.length;
label.text = easeTypes[curEaseType];
});
let curTween = null;
onMousePress(() => {
const easeType = easeTypes[curEaseType];
// stop previous lerp, or there will be jittering
if (curTween) curTween.cancel();
// start the tween
curTween = tween(
// start value (accepts number, Vec2 and Color)
bean.pos,
// destination value
mousePos(),
// duration (in seconds)
duration,
// how value should be updated
(val) => bean.pos = val,
// interpolation function (defaults to easings.linear)
easings[easeType],
);
});