Beispiele
Praxisnahe Beispiele. Live-Demos.
Öffne ein Beispiel im Vollbild: Live-Vorschau und Quellcode liegen nebeneinander.
Öffne ein Beispiel im Vollbild: Live-Vorschau und Quellcode liegen nebeneinander.
Zieh an den Griffen einer kubischen Bézierkurve. onPointerDown greift sich einen Punkt, onPointerMove auf dem svg verschiebt ihn, und der Pfad folgt.
import 'nuclo' const pts = [[20, 150], [90, 20], [230, 160], [300, 40]] let drag: number | null = null // The path is one cubic Bézier through the four points. const d = () => `M${pts[0]} C${pts[1]} ${pts[2]} ${pts[3]}` // onPointerDown on a handle remembers which point is held and // captures the pointer; onPointerMove on the svg maps the pointer // to viewBox units and moves it. Everything else is a function of pts. function move(e: PointerEvent) { if (drag === null) return const box = (e.currentTarget as SVGSVGElement).getBoundingClientRect() pts[drag] = [ Math.round(((e.clientX - box.left) / box.width) * 320), Math.round(((e.clientY - box.top) / box.height) * 180), ] update() } export const CurveEditor = () => div( svgSvg( { viewBox: '0 0 320 180', width: '320', height: '180', style: { touchAction: 'none' }, onPointerMove: move, onPointerUp: () => { drag = null }, }, lineSvg({ stroke: '#ccc', 'stroke-dasharray': '3 4', x1: () => pts[0][0], y1: () => pts[0][1], x2: () => pts[1][0], y2: () => pts[1][1], }), lineSvg({ stroke: '#ccc', 'stroke-dasharray': '3 4', x1: () => pts[3][0], y1: () => pts[3][1], x2: () => pts[2][0], y2: () => pts[2][1], }), pathSvg({ d, fill: 'none', stroke: '#ff3f00', 'stroke-width': 3 }), ...pts.map((_, i) => circleSvg({ r: 7, fill: '#fff', 'stroke-width': 2.5, stroke: i === 1 || i === 2 ? '#2563eb' : '#ff3f00', style: { cursor: 'grab' }, cx: () => pts[i][0], cy: () => pts[i][1], onPointerDown: (e) => { drag = i e.currentTarget.setPointerCapture(e.pointerId) }, }), ), ), code(d), )