예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
3차 베지어 곡선의 핸들을 끌어 보세요. onPointerDown으로 점을 잡고 svg의 onPointerMove로 옮기면 경로가 바로 뒤따라옵니다.
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), )