Skip to content

Commit 5bdcc75

Browse files
committed
Fixing reverse after setting speed on a finished animation
1 parent cbc2fc4 commit 5bdcc75

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<html>
2+
<head>
3+
<style>
4+
body {
5+
margin: 0;
6+
font-family: sans-serif;
7+
display: flex;
8+
gap: 10px;
9+
flex-direction: column;
10+
}
11+
12+
.box {
13+
width: 100px;
14+
height: 100px;
15+
background-color: #0077ff;
16+
display: flex;
17+
align-items: center;
18+
justify-content: center;
19+
text-align: center;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<div class="box" id="mini">mini</div>
25+
<div class="box" id="js">js</div>
26+
<div class="box" id="waapi">waapi</div>
27+
<script type="module" src="/src/inc.js"></script>
28+
<script type="module">
29+
const { animateMini, animate, delay } = window.Motion
30+
31+
const options = {
32+
duration: 1,
33+
ease: "linear",
34+
}
35+
36+
const mini = animateMini(
37+
"#mini",
38+
{
39+
transform: "translateX(100px)",
40+
},
41+
options
42+
)
43+
const js = animate("#js", { x: 100 }, options)
44+
const waapi = animate(
45+
"#waapi",
46+
{ transform: "translateX(100px)" },
47+
options
48+
)
49+
50+
mini.finished.then(() => {
51+
delay(() => {
52+
mini.speed = -1
53+
js.speed = -1
54+
waapi.speed = -1
55+
56+
mini.play()
57+
js.play()
58+
waapi.play()
59+
}, 0.2)
60+
})
61+
62+
delay(() => {
63+
mini.complete()
64+
js.complete()
65+
waapi.complete()
66+
}, 0.1)
67+
</script>
68+
</body>
69+
</html>

packages/motion-dom/src/animation/JSAnimation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,12 @@ export class JSAnimation<T extends number | string>
395395
onPlay && onPlay()
396396

397397
const now = this.driver.now()
398-
if (this.holdTime !== null) {
399-
this.startTime = now - this.holdTime
400-
} else if (this.state === "finished") {
398+
399+
if (this.state === "finished") {
401400
this.updateFinished()
402401
this.startTime = now
402+
} else if (this.holdTime !== null) {
403+
this.startTime = now - this.holdTime
403404
} else if (!this.startTime) {
404405
this.startTime = startTime ?? now
405406
}

tests/animate/animate.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ test.describe("animate() methods", () => {
7474
})
7575
})
7676

77+
test("play after finished and setting speed", async ({ page }) => {
78+
await waitForAnimation(
79+
"animate/animate-play-again-speed.html",
80+
page,
81+
600
82+
)
83+
await eachBox(page, async (box) => {
84+
const boundingBox = await box.boundingBox()
85+
expect(boundingBox?.x).not.toBeCloseTo(0)
86+
const text = await box.innerText()
87+
expect(text).not.toBe("error")
88+
})
89+
})
90+
7791
test("play after setting speed", async ({ page }) => {
7892
await waitForAnimation("animate/animate-play-negative-speed.html", page)
7993
await eachBox(page, async (box) => {

0 commit comments

Comments
 (0)