Skip to content

Commit 68f3ede

Browse files
committed
update spring tutorial
1 parent 5cb0f98 commit 68f3ede

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

apps/svelte.dev/content/tutorial/02-advanced-svelte/03-motion/02-springs/+assets/app-a/src/lib/App.svelte

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
<script>
2-
import { writable } from 'svelte/store';
3-
4-
let coords = writable({ x: 50, y: 50 });
5-
let size = writable(10);
2+
let coords = $state({ x: 50, y: 50 });
3+
let size = $state(10);
64
</script>
75

86
<svg
97
onmousemove={(e) => {
10-
coords.set({ x: e.clientX, y: e.clientY });
8+
coords = { x: e.clientX, y: e.clientY };
119
}}
12-
onmousedown={() => size.set(30)}
13-
onmouseup={() => size.set(10)}
10+
onmousedown={() => (size = 30)}
11+
onmouseup={() => (size = 10)}
1412
role="presentation"
1513
>
1614
<circle
17-
cx={$coords.x}
18-
cy={$coords.y}
19-
r={$size}
20-
/>
15+
cx={coords.x}
16+
cy={coords.y}
17+
r={size}
18+
></circle>
2119
</svg>
2220

2321
<div class="controls">

apps/svelte.dev/content/tutorial/02-advanced-svelte/03-motion/02-springs/+assets/app-b/src/lib/App.svelte

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
<script>
2-
import { spring } from 'svelte/motion';
2+
import { Spring } from 'svelte/motion';
33
4-
let coords = spring({ x: 50, y: 50 }, {
4+
let coords = new Spring({ x: 50, y: 50 }, {
55
stiffness: 0.1,
66
damping: 0.25
77
});
88
9-
let size = spring(10);
9+
let size = new Spring(10);
1010
</script>
1111

1212
<svg
1313
onmousemove={(e) => {
14-
coords.set({ x: e.clientX, y: e.clientY });
14+
coords.target = { x: e.clientX, y: e.clientY };
1515
}}
16-
onmousedown={() => size.set(30)}
17-
onmouseup={() => size.set(10)}
16+
onmousedown={() => (size.target = 30)}
17+
onmouseup={() => (size.target = 10)}
1818
role="presentation"
1919
>
2020
<circle
21-
cx={$coords.x}
22-
cy={$coords.y}
23-
r={$size}
21+
cx={coords.current.x}
22+
cy={coords.current.y}
23+
r={size.current}
2424
/>
2525
</svg>
2626

apps/svelte.dev/content/tutorial/02-advanced-svelte/03-motion/02-springs/index.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,48 @@
22
title: Springs
33
---
44

5-
The `spring` function is an alternative to `tweened` that often works better for values that are frequently changing.
5+
The `Spring` class is an alternative to `Tween` that often works better for values that are frequently changing.
66

7-
In this example we have two stores — one representing the circle's coordinates, and one representing its size. Let's convert them to springs:
7+
In this example we have a circle that follows the mouse, and two values — the circle's coordinates, and its size. Let's convert them to springs:
88

99
```svelte
1010
/// file: App.svelte
1111
<script>
12-
import { +++spring+++ } from 'svelte/+++motion+++';
12+
+++import { Spring } from 'svelte/motion+++';
1313
14-
let coords = +++spring+++({ x: 50, y: 50 });
15-
let size = +++spring+++(10);
14+
let coords = +++new Spring+++({ x: 50, y: 50 });
15+
let size = +++new Spring+++(10);
1616
</script>
1717
```
1818

19+
As with `Tween`, springs have a writable `target` property and a readonly `current` property. Update the event handlers...
20+
21+
```svelte
22+
<svg
23+
onmousemove={(e) => {
24+
coords.+++target+++ = { x: e.clientX, y: e.clientY };
25+
}}
26+
onmousedown={() => (size.+++target+++ = 30)}
27+
onmouseup={() => (size.+++target+++ = 10)}
28+
role="presentation"
29+
>
30+
```
31+
32+
...and the `<circle>` attributes:
33+
34+
```svelte
35+
<circle
36+
cx={coords.+++current+++.x}
37+
cy={coords.+++current+++.y}
38+
r={size.+++current+++}
39+
></circle>
40+
```
41+
1942
Both springs have default `stiffness` and `damping` values, which control the spring's, well... springiness. We can specify our own initial values:
2043

2144
```js
2245
/// file: App.svelte
23-
let coords = spring({ x: 50, y: 50 }, +++{
46+
let coords = new Spring({ x: 50, y: 50 }, +++{
2447
stiffness: 0.1,
2548
damping: 0.25
2649
}+++);

0 commit comments

Comments
 (0)