writable および readable ストアに加えて、Svelte はユーザーインターフェースにモーションを追加するためのストアを提供しています。
まずは progress ストアを tweened ストアに変更してみましょう。
アプリ
<script>
import { tweened } from 'svelte/motion';
const progress = tweened(0);
</script>ボタンをクリックすると、プログレスバーが新しい値にアニメーションします。しかし、少しロボット的で物足りません。イージング関数を追加する必要があります。
アプリ
<script>
import { tweened } from 'svelte/motion';
import { cubicOut } from 'svelte/easing';
const progress = tweened(0, {
duration: 400,
easing: cubicOut
});
</script>
svelte/easingモジュールには、Penner イージング方程式が含まれています。または、独自のp => t関数を提供することもできます。ここで、pとtはどちらも 0 から 1 の間の値です。
tweened で利用可能なオプションの完全なセット
delay— tween が開始されるまでのミリ秒数duration— tween の期間(ミリ秒単位)、または(from, to) => milliseconds関数。これにより、値の大きな変化に対してより長い tween を指定できます(例)。easing—p => t関数interpolate— 任意の値間を補間するためのカスタム(from, to) => t => value関数。デフォルトでは、Svelte は数値、日付、および同じ形状の配列とオブジェクトを補間します(数値と日付、またはその他の有効な配列とオブジェクトのみが含まれている場合)。色文字列や変換行列などを補間する場合は、カスタム補間器を提供します。
これらのオプションは、2 番目の引数として progress.set および progress.update に渡すこともできます。その場合、デフォルト値がオーバーライドされます。set メソッドと update メソッドはどちらも、tween が完了すると解決される Promise を返します。
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
<script>
import { writable } from 'svelte/store';const progress = writable(0.5);
</script>
<progress value={$progress}></progress><button onclick={() => progress.set(0)}>0%
</button>
<button onclick={() => progress.set(0.25)}>25%
</button>
<button onclick={() => progress.set(0.5)}>50%
</button>
<button onclick={() => progress.set(0.75)}>75%
</button>
<button onclick={() => progress.set(1)}>100%
</button>
<style>
progress {display: block;
width: 100%;
}
</style>