fix: 단풍 지도에서 겹쳐 안 보이고 못 누르던 점 — 겹친 점을 서로 밀어 65곳 모두 보이고 눌리게
This commit is contained in:
1 parent
199e834227
commit
9a75b02d1a
1 file changed
+45
-19
@@ -14,8 +14,9 @@ const MAP = { lon0: 124.5, lat1: 38.75, k: 0.809017, s: 153.5327, ox: 40, oy: 62
|
||||
const FIRST_DAYS = 14
|
||||
const PEAK_DAYS = 3
|
||||
const PEAK_AFTER = 7
|
||||
// 이름표가 피해 갈 점 반지름(px)
|
||||
const DOT_R = 7
|
||||
// 점 반지름(테두리 포함, px)과 점 사이 최소 틈
|
||||
const DOT_R: Record<Stage, number> = { before: 5.5, first: 6.5, peak: 8, gone: 5.5 }
|
||||
const GAP = 1.5
|
||||
|
||||
const STAGE: Record<Stage, string> = { before: '단풍 전', first: '첫 단풍', peak: '절정', gone: '' }
|
||||
const KIND: Record<Kind, string> = { maple: '단풍', ginkgo: '은행' }
|
||||
@@ -72,48 +73,72 @@ export function FoliageCard() {
|
||||
const spots = useMemo(() => (data.spots as Spot[]).map((m) => ({ ...m, stage: stage(m, today) })), [today])
|
||||
const named = useMemo(() => spots.filter((m) => m.kind === 'maple' && m.stage === 'peak').sort((a, b) => b.lat - a.lat), [spots])
|
||||
const mapRef = useRef<HTMLDivElement>(null)
|
||||
const [at, setAt] = useState<Record<string, { x: number; y: number }>>({})
|
||||
const [labelAt, setLabelAt] = useState<Record<string, { left: number; top: number }>>({})
|
||||
|
||||
// 이름표를 점 오른쪽·왼쪽·위·아래 순으로 놓아 보고, 다른 절정 점·이름표와 겹치면 그 이름은 뺀다
|
||||
// 겹친 점은 서로 밀어 떼어 놓고, 이름표는 점 오른쪽·왼쪽·위·아래 순으로 놓아 보다 다른 절정 점·이름표와 겹치면 뺀다
|
||||
useLayoutEffect(() => {
|
||||
const box = mapRef.current
|
||||
if (!box) return
|
||||
const place = () => {
|
||||
const W = box.clientWidth
|
||||
const taken: Box[] = named.map((m) => {
|
||||
const pts = spots.map((m) => {
|
||||
const [x, y] = xy(m)
|
||||
return { l: x * W - DOT_R, t: y * W - DOT_R, r: x * W + DOT_R, b: y * W + DOT_R }
|
||||
return { m, x: x * W, y: y * W, r: DOT_R[m.stage] }
|
||||
})
|
||||
for (let it = 0; it < 80; it++) {
|
||||
let moved = false
|
||||
for (let i = 0; i < pts.length; i++)
|
||||
for (let j = i + 1; j < pts.length; j++) {
|
||||
const a = pts[i]
|
||||
const b = pts[j]
|
||||
let dx = b.x - a.x
|
||||
let dy = b.y - a.y
|
||||
let d = Math.hypot(dx, dy)
|
||||
const need = a.r + b.r + GAP
|
||||
if (d >= need) continue
|
||||
if (d < 0.01) [dx, dy, d] = [Math.cos(i + j), Math.sin(i + j), 1]
|
||||
const push = (need - d) / 2
|
||||
a.x -= (dx / d) * push
|
||||
a.y -= (dy / d) * push
|
||||
b.x += (dx / d) * push
|
||||
b.y += (dy / d) * push
|
||||
moved = true
|
||||
}
|
||||
if (!moved) break
|
||||
}
|
||||
const pos = Object.fromEntries(pts.map((p) => [p.m.name, { x: p.x, y: p.y }]))
|
||||
const dot = (p: (typeof pts)[number]): Box => ({ l: p.x - p.r, t: p.y - p.r, r: p.x + p.r, b: p.y + p.r })
|
||||
const taken = pts.filter((p) => p.m.stage === 'peak').map((p) => ({ name: p.m.name, box: dot(p) }))
|
||||
const out: Record<string, { left: number; top: number }> = {}
|
||||
box.querySelectorAll<HTMLElement>('[data-label]').forEach((el) => {
|
||||
const m = named.find((n) => n.name === el.dataset.label)
|
||||
if (!m) return
|
||||
const [x, y] = xy(m)
|
||||
const p = pts.find((q) => q.m.name === el.dataset.label)
|
||||
if (!p) return
|
||||
const w = el.offsetWidth
|
||||
const h = el.offsetHeight
|
||||
const own = { l: x * W - DOT_R, t: y * W - DOT_R, r: x * W + DOT_R, b: y * W + DOT_R }
|
||||
const tries = [
|
||||
[DOT_R + 1, -h / 2],
|
||||
[-w - DOT_R - 1, -h / 2],
|
||||
[-w / 2, -h - DOT_R],
|
||||
[-w / 2, DOT_R],
|
||||
[p.r + 2, -h / 2],
|
||||
[-w - p.r - 2, -h / 2],
|
||||
[-w / 2, -h - p.r - 1],
|
||||
[-w / 2, p.r + 1],
|
||||
]
|
||||
for (const [dx, dy] of tries) {
|
||||
const r = { l: x * W + dx, t: y * W + dy, r: x * W + dx + w, b: y * W + dy + h }
|
||||
const r = { l: p.x + dx, t: p.y + dy, r: p.x + dx + w, b: p.y + dy + h }
|
||||
if (r.l < 0 || r.t < 0 || r.r > W || r.b > W) continue
|
||||
if (taken.some((o) => !(o.l === own.l && o.t === own.t) && overlaps(r, o))) continue
|
||||
taken.push(r)
|
||||
out[m.name] = { left: r.l, top: r.t }
|
||||
if (taken.some((o) => o.name !== p.m.name && overlaps(r, o.box))) continue
|
||||
taken.push({ name: '', box: r })
|
||||
out[p.m.name] = { left: r.l, top: r.t }
|
||||
break
|
||||
}
|
||||
})
|
||||
setAt(pos)
|
||||
setLabelAt(out)
|
||||
}
|
||||
place()
|
||||
const ro = new ResizeObserver(place)
|
||||
ro.observe(box)
|
||||
return () => ro.disconnect()
|
||||
}, [named])
|
||||
}, [spots, named])
|
||||
|
||||
// 가장 이른 명소가 첫 단풍에 들 때부터 가장 늦은 명소의 절정이 끝날 때까지
|
||||
const peaks = spots.map((m) => m.peak).sort()
|
||||
@@ -130,6 +155,7 @@ export function FoliageCard() {
|
||||
<img src="/foliage/korea-map.webp" alt="단풍 지도" className="absolute inset-0 size-full mix-blend-multiply brightness-[1.08] saturate-[.3]" />
|
||||
{order.map((m) => {
|
||||
const [x, y] = xy(m)
|
||||
const p = at[m.name]
|
||||
return (
|
||||
<button
|
||||
key={m.name}
|
||||
@@ -142,7 +168,7 @@ export function FoliageCard() {
|
||||
m.stage === 'peak' && m.kind === 'ginkgo' && 'bg-[#f2c230]',
|
||||
m.name === picked && 'z-[2] scale-150',
|
||||
)}
|
||||
style={{ left: `${x * 100}%`, top: `${y * 100}%` }}
|
||||
style={p ? { left: p.x, top: p.y } : { left: `${x * 100}%`, top: `${y * 100}%` }}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
|
||||
Reference in new issue
Block a user