Initial commit: Luminary photo catalog and RAW editor
Browser-based photo catalog with an optional team server (SQLite + LibRaw). Local database, uploaded photos, build output and dependencies are gitignored. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { buildSampler, clamp01 } from '../lib/curves';
|
||||
import type { CurvePoint } from '../types';
|
||||
|
||||
interface Props {
|
||||
points: CurvePoint[];
|
||||
onChange: (points: CurvePoint[]) => void;
|
||||
color: string;
|
||||
}
|
||||
|
||||
export default function CurveEditor({ points, onChange, color }: Props) {
|
||||
const svgRef = useRef<SVGSVGElement>(null);
|
||||
const pointsRef = useRef(points);
|
||||
const [drag, setDrag] = useState<number | null>(null);
|
||||
const [hover, setHover] = useState<number | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
pointsRef.current = points;
|
||||
}, [points]);
|
||||
|
||||
const sampler = useMemo(() => buildSampler(points), [points]);
|
||||
|
||||
const path = useMemo(() => {
|
||||
const segs: string[] = [];
|
||||
for (let i = 0; i <= 80; i++) {
|
||||
const x = i / 80;
|
||||
const y = sampler(x);
|
||||
segs.push(`${i === 0 ? 'M' : 'L'}${(x * 100).toFixed(2)},${((1 - y) * 100).toFixed(2)}`);
|
||||
}
|
||||
return segs.join(' ');
|
||||
}, [sampler]);
|
||||
|
||||
const toNorm = useCallback((clientX: number, clientY: number) => {
|
||||
const svg = svgRef.current;
|
||||
if (!svg) return { x: 0, y: 0 };
|
||||
const rect = svg.getBoundingClientRect();
|
||||
return {
|
||||
x: clamp01((clientX - rect.left) / rect.width),
|
||||
y: clamp01(1 - (clientY - rect.top) / rect.height),
|
||||
};
|
||||
}, []);
|
||||
|
||||
const handleDown = (e: React.PointerEvent<SVGSVGElement>) => {
|
||||
e.preventDefault();
|
||||
const idxAttr = (e.target as Element).getAttribute('data-idx');
|
||||
let idx: number;
|
||||
if (idxAttr !== null) {
|
||||
idx = Number(idxAttr);
|
||||
} else {
|
||||
const p = toNorm(e.clientX, e.clientY);
|
||||
const next = [...points, { x: p.x, y: p.y }].sort((a, b) => a.x - b.x);
|
||||
idx = next.findIndex((pt) => pt.x === p.x && pt.y === p.y);
|
||||
onChange(next);
|
||||
}
|
||||
setDrag(idx);
|
||||
e.currentTarget.setPointerCapture(e.pointerId);
|
||||
};
|
||||
|
||||
const handleMove = (e: React.PointerEvent<SVGSVGElement>) => {
|
||||
if (drag === null) return;
|
||||
const pts = pointsRef.current;
|
||||
const idx = drag;
|
||||
if (!pts[idx]) return;
|
||||
const p = toNorm(e.clientX, e.clientY);
|
||||
const lo = idx > 0 ? pts[idx - 1].x + 0.01 : 0;
|
||||
const hi = idx < pts.length - 1 ? pts[idx + 1].x - 0.01 : 1;
|
||||
const next = pts.map((pt, i) => (i === idx ? { x: Math.min(hi, Math.max(lo, p.x)), y: p.y } : pt));
|
||||
onChange(next);
|
||||
};
|
||||
|
||||
const handleUp = () => setDrag(null);
|
||||
|
||||
const removePoint = (idx: number) => {
|
||||
const pts = pointsRef.current;
|
||||
if (pts.length <= 2) return;
|
||||
onChange(pts.filter((_, i) => i !== idx));
|
||||
setDrag(null);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="curve-wrap">
|
||||
<svg
|
||||
ref={svgRef}
|
||||
className="curve"
|
||||
viewBox="0 0 100 100"
|
||||
preserveAspectRatio="none"
|
||||
onPointerDown={handleDown}
|
||||
onPointerMove={handleMove}
|
||||
onPointerUp={handleUp}
|
||||
onPointerCancel={handleUp}
|
||||
onDoubleClick={(e) => {
|
||||
const idxAttr = (e.target as Element).getAttribute('data-idx');
|
||||
if (idxAttr !== null) removePoint(Number(idxAttr));
|
||||
}}
|
||||
>
|
||||
<rect x="0" y="0" width="100" height="100" className="curve-bg" />
|
||||
{[25, 50, 75].map((v) => (
|
||||
<g key={v}>
|
||||
<line x1={v} y1="0" x2={v} y2="100" className="curve-grid" />
|
||||
<line x1="0" y1={v} x2="100" y2={v} className="curve-grid" />
|
||||
</g>
|
||||
))}
|
||||
<line x1="0" y1="100" x2="100" y2="0" className="curve-diag" />
|
||||
<path d={path} fill="none" stroke={color} strokeWidth="1.6" vectorEffect="non-scaling-stroke" />
|
||||
{points.map((p, i) => (
|
||||
<circle
|
||||
key={`${i}-${p.x.toFixed(3)}`}
|
||||
data-idx={i}
|
||||
cx={p.x * 100}
|
||||
cy={(1 - p.y) * 100}
|
||||
r={hover === i || drag === i ? 3 : 2.1}
|
||||
className="curve-point"
|
||||
fill={color}
|
||||
onPointerEnter={() => setHover(i)}
|
||||
onPointerLeave={() => setHover((h) => (h === i ? null : h))}
|
||||
/>
|
||||
))}
|
||||
</svg>
|
||||
<div className="curve-hint">Click to add a point · drag to shape · double-click a point to remove</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user