Skip to content

Commit 6b9f59b

Browse files
committed
final refactor 2
1 parent 9101e83 commit 6b9f59b

File tree

5 files changed

+44
-67
lines changed

5 files changed

+44
-67
lines changed

src/lib/Draggable.svelte

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,16 @@
7777
7878
type SvelteEvent = { currentTarget: EventTarget & HTMLDivElement };
7979
80-
let snapPoints = $derived(_snapPoints?.toSorted((a, b) => a - b));
80+
// Sort snap points and add 0 and 1 to beginning and end of the list
81+
let snapPoints = $derived.by(() => {
82+
if (_snapPoints === undefined) return;
83+
84+
const sorted = _snapPoints.filter((n) => n >= 0.0 && n <= 1.0).toSorted((a, b) => a - b);
85+
if (sorted[0] !== 0.0) sorted.unshift(0.0);
86+
if (sorted[sorted.length - 1] !== 1.0) sorted.push(1.0);
87+
88+
return sorted;
89+
});
8190
8291
let isDragging = false;
8392
let isShieldOn = $state(false);

src/lib/params/lin.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { Param } from './base.js';
22

33
export class LinearParam extends Param<number> {
4-
public min = 0.0;
5-
public max = 1.0;
6-
7-
constructor(min?: number, max?: number) {
4+
constructor(
5+
public min = 0.0,
6+
public max = 1.0
7+
) {
88
super();
9-
if (min) this.min = min;
10-
if (max) this.max = max;
119
}
1210

1311
public normalize(value: number): number {

src/routes/+page.svelte

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,32 @@
4646
<CopyPaste>%import('./examples/ParamKnob.svelte')%</CopyPaste>
4747
</div>
4848

49+
<div class="example">
50+
<h2>Snap points</h2>
51+
52+
<LazyComponent component={() => import('./examples/SnapPoints.svelte')} />
53+
54+
<p>
55+
You can specify snap points and how strong the snapping is for your knob. The knob will
56+
automatically sort and insert 0 and 1 to your snap point list, so <code>[0.6,0.3]</code> will
57+
become <code>[0.0,0.3,0.6,1.0]</code>.
58+
</p>
59+
<p>
60+
When snapPoints are specified, arrow keys on the keyboard will make the knob jump between
61+
them. Pressing alt key will disable the snapping.
62+
</p>
63+
<p>This concept will be importand later in the next example.</p>
64+
65+
<CopyPaste>%import('./examples/SnapPoints.svelte')%</CopyPaste>
66+
</div>
67+
4968
<div class="example">
5069
<h2>Enum-param knobs</h2>
5170

5271
<LazyComponent component={() => import('./examples/EnumParam.svelte')} />
5372

5473
<p>
55-
Enums or, in typescript realm <code>readonly string[]</code> parameter are a special type of
74+
Enums, or in typescript realm <code>readonly string[]</code> parameter are a special type of
5675
parameter that don't denormalize into a number, instead into a string. <code>EnumParam</code> class
5776
comes with helpful properties for knob ui with already calcualted snap points and snap threshold
5877
to make value changes 'instant'.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
import { SvgKnob } from '$lib';
3+
4+
let value = $state(0.0);
5+
</script>
6+
7+
<div>
8+
<SvgKnob bind:value snapPoints={[0.5]} />
9+
<span>{value.toFixed(2)}</span>
10+
</div>

src/routes/v2/+page.svelte

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)