|
1 | 1 | <script lang="ts"> |
2 | | - // remove next |
3 | 2 | import CopyPaste from './CopyPaste.svelte'; |
4 | | - import { |
5 | | - createEnumParam, |
6 | | - createFloatParam, |
7 | | - createRange, |
8 | | - type Variant, |
9 | | - Knob |
10 | | - } from '$lib/index.js'; |
11 | | -
|
12 | | - const basicParam = createFloatParam(createRange('lin', 0, 100)); |
13 | | - const freqParam = createFloatParam(createRange('log', 20, 20_000)); |
14 | | - const gainParam = createFloatParam(createRange('log', -30, 30, Math.E)); |
15 | | - const qParam = createFloatParam(createRange('log', 0.01, 30, 2)); |
16 | | - const smoothParam = createFloatParam(createRange('lin', 0, 100)); |
17 | | - const snapParam = createFloatParam(createRange('lin', 0, 2000)); |
18 | | - const enumParam = createEnumParam(['🍍', '🍉', '🍌', '🍎', '🥭', '🍇', '🥝', '🍋'] as const); |
19 | | - const filterParam = createEnumParam(['LP 12dB', 'HP 12dB', 'HP 24dB', 'BP 24dB'] as const); |
20 | | - const oscParam = createEnumParam(['Sine', 'Triangle', 'Square', 'Sawtooth', 'Noise'] as const); |
21 | | -
|
22 | | - const snapPoints = Array.from({ length: 5 }, (_, i) => i * 500); |
23 | | -
|
24 | | - let basicValue = 0; |
25 | | - let freqValue = 20; |
26 | | - let gainValue = 20; |
27 | | - let qValue = 0; |
28 | | - let smoothValue = 0; |
29 | | - let snapValue = 0; |
30 | | - let enumValue: Variant<typeof enumParam> = '🍎'; |
31 | | - let filterValue: Variant<typeof filterParam> = 'HP 24dB'; |
32 | | - let oscValue: Variant<typeof oscParam> = 'Square'; |
| 3 | + import LazyComponent from './LazyComponent.svelte'; |
33 | 4 | </script> |
34 | 5 |
|
35 | 6 | <div class="grid"> |
36 | 7 | <h1 class="example"># svelte-knobs</h1> |
37 | 8 |
|
38 | 9 | <div class="example"> |
39 | 10 | <h2>Basic</h2> |
40 | | - <Knob param={basicParam} bind:value={basicValue} label="Volume" unit="%" /> |
| 11 | + <LazyComponent component={() => import('./examples/BasicKnob.svelte')} /> |
41 | 12 |
|
42 | 13 | <p>A basic knob with a linear parameter.</p> |
| 14 | + <CopyPaste>%import('./examples/BasicKnob.svelte')%</CopyPaste> |
43 | 15 | </div> |
44 | 16 |
|
45 | 17 | <div class="example"> |
46 | 18 | <h2>Logarithmic</h2> |
47 | | - <Knob param={freqParam} bind:value={freqValue} label="Frequency" unit="hz" /> |
48 | | - <Knob param={gainParam} bind:value={gainValue} label="Gain" unit="dB" decimalDigits={1} /> |
49 | | - <Knob param={qParam} bind:value={qValue} label="Q" unit="dB" decimalDigits={2} /> |
| 19 | + <LazyComponent component={() => import('./examples/LogarithmicKnob.svelte')} /> |
50 | 20 |
|
51 | 21 | <p>A knob with logarithmic scaling (default base is 10).</p> |
| 22 | + <CopyPaste>%import('./examples/LogarithmicKnob.svelte')%</CopyPaste> |
52 | 23 | </div> |
53 | 24 |
|
54 | 25 | <div class="example"> |
55 | 26 | <h2>Smoothness</h2> |
56 | 27 |
|
57 | | - <h3>Smooth</h3> |
58 | | - <Knob param={smoothParam} bind:value={smoothValue} label="Amount" unit="%" stiffness={0.1} /> |
59 | | - |
60 | | - <h3>Stiff</h3> |
61 | | - <Knob param={smoothParam} bind:value={smoothValue} label="Amount" unit="%" stiffness={1} /> |
| 28 | + <LazyComponent component={() => import('./examples/SmoothKnob.svelte')} /> |
62 | 29 |
|
63 | 30 | <p> |
64 | 31 | Knobs use <code>spring</code> function from <code>svelte/motion</code> under the hood, |
|
69 | 36 | Additionally, due to how Svelte's binding works, you can use the same parameter and value for |
70 | 37 | multiple knobs simultaneously. |
71 | 38 | </p> |
| 39 | + |
| 40 | + <CopyPaste>%import('./examples/SmoothKnob.svelte')%</CopyPaste> |
72 | 41 | </div> |
73 | 42 |
|
74 | 43 | <div class="example"> |
75 | 44 | <h2>Snap Points</h2> |
76 | | - <Knob |
77 | | - param={snapParam} |
78 | | - bind:value={snapValue} |
79 | | - snapValues={snapPoints} |
80 | | - snapThreshold={0.1} |
81 | | - label="Release" |
82 | | - unit="ms" |
83 | | - /> |
| 45 | + |
| 46 | + <LazyComponent component={() => import('./examples/SnapPoints.svelte')} /> |
84 | 47 |
|
85 | 48 | <p> |
86 | 49 | You can define specific points where the knob should snap and control the snapping strength by |
87 | 50 | adjusting the <code>snapThreshold</code> property. |
88 | 51 | </p> |
89 | 52 | <p><i>Note: This works only with </i><code>FloatParam</code><i> values.</i></p> |
| 53 | + |
| 54 | + <CopyPaste>%import('./examples/SnapPoints.svelte')%</CopyPaste> |
90 | 55 | </div> |
91 | 56 |
|
92 | 57 | <div class="example"> |
93 | 58 | <h2>Enum Parameter</h2> |
94 | | - <Knob param={enumParam} bind:value={enumValue} label="Fruit" /> |
95 | | - <Knob param={filterParam} bind:value={filterValue} label="Filter Type" /> |
96 | | - <Knob param={oscParam} bind:value={oscValue} label="Oscillator Type" /> |
| 59 | + |
| 60 | + <LazyComponent component={() => import('./examples/EnumParam.svelte')} /> |
97 | 61 |
|
98 | 62 | <p> |
99 | 63 | An example of how <code>EnumParam</code> works: due to the way TypeScript functions, we need |
|
106 | 70 | </p> |
107 | 71 | <p>For instance:</p> |
108 | 72 | <code>const fruitParam = createEnumParam(['🍍', '🍉', '🍌', '🥝', '🍋'] as const);</code> |
| 73 | + |
| 74 | + <CopyPaste>%import('./examples/EnumParam.svelte')%</CopyPaste> |
109 | 75 | </div> |
110 | 76 |
|
111 | 77 | <div class="example"> |
112 | 78 | <h2>Disabled</h2> |
113 | 79 |
|
114 | | - <Knob param={basicParam} value={58} disabled /> |
| 80 | + <LazyComponent component={() => import('./examples/DisabledKnob.svelte')} /> |
115 | 81 |
|
116 | 82 | <p> |
117 | 83 | Just like <code>{'<button>'}</code> and other interactive HTML elements, |
118 | 84 | <code>{'<Knob />'}</code> can also be disabled. |
119 | 85 | </p> |
| 86 | + |
| 87 | + <CopyPaste>%import('./examples/DisabledKnob.svelte')%</CopyPaste> |
120 | 88 | </div> |
121 | 89 |
|
122 | 90 | <div class="example"> |
123 | 91 | <h2>Colors</h2> |
124 | 92 |
|
125 | | - <Knob param={basicParam} value={24} label="Svelte theme" colors={{ arc: '#ff3e00' }} /> |
126 | | - <Knob |
127 | | - param={basicParam} |
128 | | - value={48} |
129 | | - label="Light theme" |
130 | | - colors={{ |
131 | | - arc: '#4292d3', |
132 | | - bg: '#eef' |
133 | | - }} |
134 | | - style="color:#000" |
135 | | - /> |
136 | | - <Knob |
137 | | - onChange={console.log} |
138 | | - param={basicParam} |
139 | | - value={64} |
140 | | - label="Disabled color" |
141 | | - colors={{ disabled: '#ccc' }} |
142 | | - disabled |
143 | | - /> |
| 93 | + <LazyComponent component={() => import('./examples/ColoredKnobs.svelte')} /> |
144 | 94 |
|
145 | 95 | <p> |
146 | 96 | Of course, <code>{'<Knob />'}</code> colors can be customized to look however you want. |
147 | 97 | <b>Disclaimer</b>: Font and thumb colors are based on the current font color. |
148 | 98 | </p> |
149 | | - </div> |
150 | | -</div> |
151 | 99 |
|
152 | | -<div class="code"> |
153 | | - <h2>Code for this Demo:</h2> |
154 | | - <CopyPaste>%self%</CopyPaste> |
| 100 | + <CopyPaste>%import('./examples/ColoredKnobs.svelte')%</CopyPaste> |
| 101 | + </div> |
155 | 102 | </div> |
156 | 103 |
|
157 | 104 | <style> |
|
168 | 115 | padding: 2rem; |
169 | 116 | display: flex; |
170 | 117 | flex-direction: column; |
171 | | - max-width: 600px; |
| 118 | + max-width: 720px; |
172 | 119 | margin: 0 auto; |
173 | 120 | gap: 8px; |
174 | 121 | } |
|
0 commit comments