8000 feat(knobs): Ableton-style active-range arc on knobs + FX encoders by johannboehme · Pull Request #58 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions frontend/src/editor/components/FxHardwarePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,10 @@ function Encoder({ kind, param, tint }: EncoderProps) {
Math.min(1, range > 0 ? (value - param.min) / range : 0),
);
const angle = -135 + ratio * 270;
// Active-range arc: bipolar params grow from the centre detent (top),
// linear params from the scale start.
const arcStart = param.kind === "bipolar" ? 0 : -135;
const arcEnd = angle;

const >
(e: ReactPointerEvent<HTMLDivElement>) => {
Expand Down Expand Up @@ -815,6 +819,34 @@ function Encoder({ kind, param, tint }: EncoderProps) {
{param.kind === "linear"
? linearTicks(ratio, tint)
: bipolarTicks(value, param.min, param.max, tint)}
{/* active-range arc hugging the knob body — faint track shows the
unused range, the tinted arc traces from neutral to value. */}
<path
d={encArcPath(-135, 135, ENC_ARC_R)}
fill="none"
stroke="rgba(245,240,225,0.16)"
strokeWidth={1.6}
strokeLinecap="round"
/>
{Math.abs(arcEnd - arcStart) > 0.5 && (
<>
<path
d={encArcPath(arcStart, arcEnd, ENC_ARC_R)}
fill="none"
stroke={tint}
strokeWidth={2.8}
strokeLinecap="round"
opacity={0.3}
/>
<path
d={encArcPath(arcStart, arcEnd, ENC_ARC_R)}
fill="none"
stroke={tint}
strokeWidth={1.6}
strokeLinecap="round"
/>
</>
)}
</svg>

{/* knob body — matte black, fixed (does NOT rotate; the indicator does) */}
Expand Down Expand Up @@ -1069,6 +1101,21 @@ function bipolarTicks(
return out;
}

10000 // SVG arc path between two tick-space angles (0° = scale start sense used by
// the encoder, i.e. -135..+135) at a given radius, with the same -90° SVG
// offset the ticks use (top = 0). Round-capped when stroked.
function encArcPath(startDeg: number, endDeg: number, r: number): string {
const pt = (deg: number): [number, number] => {
const rad = ((deg - 90) * Math.PI) / 180;
return [Math.cos(rad) * r, Math.sin(rad) * r];
};
const [x1, y1] = pt(startDeg);
const [x2, y2] = pt(endDeg);
const large = Math.abs(endDeg - startDeg) > 180 ? 1 : 0;
const sweep = endDeg >= startDeg ? 1 : 0;
return `M ${x1} ${y1} A ${r} ${r} 0 ${large} ${sweep} ${x2} ${y2}`;
}

// — Helpers ————————————————————————————————————————————————

function fmt(param: FxParamDef, value: number): string {
Expand Down Expand Up @@ -2168,6 +2215,9 @@ const ENC_BODY = 36;
const ENC_BODY_INSET = (ENC_OUTER - ENC_BODY) / 2;
const ENC_CAP = 28;
const ENC_CAP_INSET = (ENC_OUTER - ENC_CAP) / 2;
// Active-range arc radius (viewBox units). Sits in the gap between the
// black knob body (r≈36) and the inner ends of the etched scale ticks.
const ENC_ARC_R = 38;

const ALUMINUM_BODY: CSSProperties = {
background:
Expand Down
61 changes: 59 additions & 2 deletions frontend/src/editor/components/Knob.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ interface Props {
disabled?: boolean;
}

// SVG arc path between two value-angles (the same -135..+135 the indicator
// uses, 0° = pointing up) at a given radius. The -90° offset maps that onto
// SVG space (0° = right), so the arc tracks the needle exactly. Round-capped.
function arcPath(startDeg: number, endDeg: number, r: number): string {
const pt = (deg: number): [number, number] => {
const rad = ((deg - 90) * Math.PI) / 180;
return [Math.cos(rad) * r, Math.sin(rad) * r];
};
const [x1, y1] = pt(startDeg);
const [x2, y2] = pt(endDeg);
const large = Math.abs(endDeg - startDeg) > 180 ? 1 : 0;
const sweep = endDeg >= startDeg ? 1 : 0;
return `M ${x1} ${y1} A ${r} ${r} 0 ${large} ${sweep} ${x2} ${y2}`;
}

export function Knob({
value,
min,
Expand All @@ -35,6 +50,13 @@ export function Knob({
// Map value to angle: -135° at min, +135° at max — leaves a "dead zone" at the bottom.
const ratio = Math.max(0, Math.min(1, (value - min) / range));
const angle = -135 + ratio * 270;
// Active-range arc: grows from min for one-sided ranges, or from the
// zero-crossing for bipolar ranges (e.g. PILL OFFSET), so it reads like
// an Ableton knob — fill always starts where "neutral" is.
const bipolar = min < 0 && max > 0;
const centerRatio = bipolar ? (0 - min) / range : 0;
const arcStart = -135 + centerRatio * 270;
const arcEnd = angle;

const >
(e: React.PointerEvent<HTMLDivElement>) => {
Expand Down Expand Up @@ -138,14 +160,20 @@ export function Knob({
>
{Array.from({ length: 25 }).map((_, i) => {
const t = i / 24;
const a = (-135 + t * 270) * (Math.PI / 180);
// -90° puts the scale's dead-zone at the bottom and lines the
// ticks up with the indicator (same convention as the FX encoder).
const a = (-135 + t * 270 - 90) * (Math.PI / 180);
const r1 = 47;
const r2 = i % 6 === 0 ? 41 : 44;
const x1 = Math.cos(a) * r1;
const y1 = Math.sin(a) * r1;
const x2 = Math.cos(a) * r2;
const y2 = Math.sin(a) * r2;
const filled = t <= ratio;
// Bipolar ranges light from the centre detent outward (matching
// the arc); one-sided ranges fill from the minimum.
const filled = bipolar
? t >= Math.min(centerRatio, ratio) && t <= Math.max(centerRatio, ratio)
: t <= ratio;
return (
<line
key={i}
Expand All @@ -159,6 +187,35 @@ export function Knob({
/>
);
})}
{/* active-range arc — Ableton-style, sits just outside the ticks.
Faint full-sweep track shows the unused range; the hot arc
traces from neutral to the current value. */}
<path
d={arcPath(-135, 135, 48.2)}
fill="none"
stroke="#C9BFA6"
strokeWidth={2}
strokeLinecap="round"
/>
{Math.abs(arcEnd - arcStart) > 0.5 && (
<>
<path
d={arcPath(arcStart, arcEnd, 48.2)}
fill="none"
stroke="#FF5722"
strokeWidth={3.6}
strokeLinecap="round"
opacity={0.22}
/>
<path
d={arcPath(arcStart, arcEnd, 48.2)}
fill="none"
stroke="#FF5722"
strokeWidth={2}
strokeLinecap="round"
/>
</>
)}
</svg>
{/* inner knob */}
<div
Expand Down
Loading
0