예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
SVG 요소의 포인터 이벤트만으로 만듭니다. onPointerEnter가 활성 포인트를 고르면 툴팁 그룹이 그 위치로 미끄러져 갑니다.
import 'nuclo' const random = () => ({ x: Math.round(Math.random() * 100), y: Math.round(Math.random() * 100), }) let points = Array.from({ length: 24 }, random) let active: number | null = null const sx = (v: number) => 16 + (v / 100) * 288 const sy = (v: number) => 164 - (v / 100) * 148 // onPointerEnter on a circle sets the active index; the svg's // onPointerLeave clears it. The tooltip is a group whose opacity // and transform read the active point, so it slides between them. export const Scatter = () => div( svgSvg( { viewBox: '0 0 320 180', width: '320', height: '180', onPointerLeave: () => { active = null; update() }, }, lineSvg({ x1: 16, y1: 164, x2: 304, y2: 164, stroke: '#ddd' }), lineSvg({ x1: 16, y1: 16, x2: 16, y2: 164, stroke: '#ddd' }), ...points.map((_, i) => circleSvg({ fill: '#ff3f00', style: { cursor: 'pointer', transition: 'r 0.15s, fill-opacity 0.15s', }, cx: () => sx(points[i].x), cy: () => sy(points[i].y), r: () => (active === i ? 7 : 4.5), 'fill-opacity': () => (active === null || active === i ? 0.9 : 0.35), onPointerEnter: () => { active = i; update() }, }), ), gSvg( { 'pointer-events': 'none', opacity: () => (active === null ? 0 : 1), style: () => { const p = points[active ?? 0] return { transition: 'opacity 0.15s, transform 0.15s', transform: `translate(${sx(p.x)}px, ${sy(p.y) - 16}px)`, } }, }, rectSvg({ x: -30, y: -20, width: 60, height: 20, rx: 5, fill: '#222' }), textSvg( { x: 0, y: -6, 'text-anchor': 'middle', fill: '#fff', 'font-size': 11 }, () => (active === null ? '' : `${points[active].x}, ${points[active].y}`), ), ), ), div( { style: { display: 'flex', gap: '8px', justifyContent: 'center' } }, button('New data', { onClick: () => { points = points.map(random); update() }, }), ), )