Exemples
Du concret. Des démos en direct.
Choisissez un exemple pour l'ouvrir en plein écran : l'aperçu en direct et son code source, côte à côte.
Choisissez un exemple pour l'ouvrir en plein écran : l'aperçu en direct et son code source, côte à côte.
Attrapez les poignées d'une courbe de Bézier cubique. onPointerDown choisit un point, onPointerMove sur le svg le déplace, et le path suit.
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), )