Skip to main content

스프링(spring) 함수는 트윈드(tweened) 대신에 사용할 수 있는 함수로, 값이 자주 바뀌는 경우에 더 좋은 결과를 보여주는 경우가 많습니다.

이 예제에는 두 스토어가 있습니다. 하나는 원의 좌푯값을 표현하고, 다른 하나는 크기를 표현합니다. 이것들을 스프링으로 바꿔 봅시다.

App.svelte
<script>
	import { spring } from 'svelte/motion';

	let coords = spring({ x: 50, y: 50 });
	let size = spring(10);
</script>

두 스프링 다 stiffnessdamping 기본값을 가지고 있습니다. 이 값들은 스프링의, 음, 스프링다움을 조정합니다. 우리가 지정한 초기값으로도 바꿀 수 있습니다.

App.svelte
let coords = spring({ x: 50, y: 50 }, {
	stiffness: 0.1,
	damping: 0.25
});

마우스를 이리저리 움직여 보고, 슬라이더를 드래그해서 스프링의 행동에 어떤 영향을 미치는지 느껴보세요. 스프링 모션이 진행 중인 상황에서도 값을 조정할 수 있다는 걸 명심하세요.

Next: 전환(Transition)

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
<script>
	import { writable } from 'svelte/store';
 
	let coords = writable({ x: 50, y: 50 });
	let size = writable(10);
</script>
 
<svg
	on:mousemove={(e) => {
		coords.set({ x: e.clientX, y: e.clientY });
	}}
	on:mousedown={() => size.set(30)}
	on:mouseup={() => size.set(10)}
	role="presentation"
>
	<circle
		cx={$coords.x}
		cy={$coords.y}
		r={$size}
	/>
</svg>
 
<div class="controls">
	<label>
		<h3>stiffness ({coords.stiffness})</h3>
		<input
			bind:value={coords.stiffness}
			type="range"
			min="0.01"
			max="1"
			step="0.01"
		/>
	</label>
 
	<label>
		<h3>damping ({coords.damping})</h3>
		<input
			bind:value={coords.damping}
			type="range"
			min="0.01"
			max="1"
			step="0.01"
		/>
	</label>
</div>
 
<style>
	svg {
		position: absolute;
		width: 100%;
		height: 100%;
		left: 0;
		top: 0;
	}
 
	circle {
		fill: #ff3e00;
	}
 
	.controls {
		position: absolute;
		top: 1em;
		right: 1em;
		width: 200px;
		user-select: none;
	}
 
	.controls input {
		width: 100%;
	}
</style>
 
initialising