feat: 단풍 지도를 점으로 — 단계별 점 색, 절정 단풍은 이름표(겹치면 생략), 지도 색 옅게, 절정 7일 뒤부터는 회색
This commit is contained in:
1 parent
aef8b4a022
commit
8ca38f1806
1 file changed
+108
-41
+108
-41
@@ -1,102 +1,169 @@
|
||||
import { useState } from 'react'
|
||||
import { useLayoutEffect, useMemo, useRef, useState } from 'react'
|
||||
import data from '../../seo/foliage_2026.json'
|
||||
import { cx } from '../lib/utils'
|
||||
import { kst } from './lib'
|
||||
import { PostFrame } from './Post'
|
||||
import { Ic } from './ui'
|
||||
|
||||
type Kind = 'maple' | 'ginkgo'
|
||||
type Spot = { name: string; kind: Kind; region: string; lat: number; lon: number; peak: string; basis: string }
|
||||
type Stage = 'before' | 'first' | 'peak'
|
||||
type Stage = 'before' | 'first' | 'peak' | 'gone'
|
||||
|
||||
// 지도 그림(public/foliage/korea-map.webp, 정사각형)의 위경도 → 그림 위 위치. generated-images/foliage-map/south_outline.json 과 같은 값
|
||||
const MAP = { lon0: 124.5, lat1: 38.75, k: 0.809017, s: 153.5327, ox: 40, oy: 62.92, size: 1024 }
|
||||
// 발표에 시작일이 없어 절정 FIRST_DAYS 일 전부터 첫 단풍, PEAK_DAYS 일 전부터 절정으로 본다
|
||||
// 발표에 시작일이 없어 절정 FIRST_DAYS 일 전부터 첫 단풍, PEAK_DAYS 일 전부터 PEAK_AFTER 일 뒤까지 절정으로 본다
|
||||
const FIRST_DAYS = 14
|
||||
const PEAK_DAYS = 3
|
||||
const PEAK_AFTER = 7
|
||||
const SEASON = { from: '09-20', to: '11-25' }
|
||||
const SEOUL = { lat: 37.566, lon: 126.978 }
|
||||
// 이름표가 피해 갈 점 반지름(px)
|
||||
const DOT_R = 7
|
||||
|
||||
const STAGE: Record<Stage, { label: string; icon: string }> = {
|
||||
before: { label: '단풍 전', icon: 'size-3 opacity-60 grayscale' },
|
||||
first: { label: '첫 단풍', icon: 'size-3.5' },
|
||||
peak: { label: '절정', icon: 'size-[18px]' },
|
||||
}
|
||||
const KIND: Record<Kind, { label: string; icon: string }> = {
|
||||
maple: { label: '단풍', icon: 'maple' },
|
||||
ginkgo: { label: '은행', icon: 'ginkgo' },
|
||||
const STAGE: Record<Stage, string> = { before: '단풍 전', first: '첫 단풍', peak: '절정', gone: '' }
|
||||
const KIND: Record<Kind, string> = { maple: '단풍', ginkgo: '은행' }
|
||||
const DOT: Record<Stage, string> = {
|
||||
before: 'size-2 bg-[#b9bcc6]',
|
||||
first: 'size-2.5 bg-[#f39a3d]',
|
||||
peak: 'size-[13px] bg-[#e0442e]',
|
||||
gone: 'size-2 bg-[#b9bcc6]',
|
||||
}
|
||||
const LEGEND = [
|
||||
{ label: '단풍 전', color: 'bg-[#b9bcc6]' },
|
||||
{ label: '첫 단풍', color: 'bg-[#f39a3d]' },
|
||||
{ label: '절정 단풍', color: 'bg-[#e0442e]' },
|
||||
{ label: '절정 은행', color: 'bg-[#f2c230]' },
|
||||
]
|
||||
|
||||
const pos = (m: Spot) => ({
|
||||
left: `${((MAP.ox + (m.lon - MAP.lon0) * MAP.k * MAP.s) / MAP.size) * 100}%`,
|
||||
top: `${((MAP.oy + (MAP.lat1 - m.lat) * MAP.s) / MAP.size) * 100}%`,
|
||||
})
|
||||
const xy = (m: Spot) => [(MAP.ox + (m.lon - MAP.lon0) * MAP.k * MAP.s) / MAP.size, (MAP.oy + (MAP.lat1 - m.lat) * MAP.s) / MAP.size]
|
||||
|
||||
const DAY = 86_400_000
|
||||
const days = (from: string, to: string) => Math.round((Date.parse(to) - Date.parse(from)) / DAY)
|
||||
|
||||
function stage(m: Spot, today: string): Stage {
|
||||
const d = days(today, m.peak)
|
||||
if (d < -PEAK_AFTER) return 'gone'
|
||||
if (d <= PEAK_DAYS) return 'peak'
|
||||
return d <= FIRST_DAYS ? 'first' : 'before'
|
||||
}
|
||||
|
||||
const md = (iso: string) => `${Number(iso.slice(5, 7))}/${Number(iso.slice(8, 10))}`
|
||||
|
||||
/** 단풍철에만 나오는 단풍 지도 카드. 산마다 절정 예측일로 지금 절정·곧·아직·지남을 점으로 */
|
||||
type Box = { l: number; t: number; r: number; b: number }
|
||||
const overlaps = (a: Box, b: Box) => a.l < b.r && b.l < a.r && a.t < b.b && b.t < a.b
|
||||
|
||||
/** 단풍철에만 나오는 단풍 지도 카드. 명소마다 절정 예측일로 단풍 전·첫 단풍·절정을 점 색으로, 절정 단풍은 이름까지 */
|
||||
export function FoliageCard() {
|
||||
const [now] = useState(() => new Date())
|
||||
const t = kst(now)
|
||||
const today = `${t.y}-${String(t.mo).padStart(2, '0')}-${String(t.d).padStart(2, '0')}`
|
||||
const [picked, setPicked] = useState<string | null>(null)
|
||||
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 [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 [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 }
|
||||
})
|
||||
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 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],
|
||||
]
|
||||
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 }
|
||||
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 }
|
||||
break
|
||||
}
|
||||
})
|
||||
setLabelAt(out)
|
||||
}
|
||||
place()
|
||||
const ro = new ResizeObserver(place)
|
||||
ro.observe(box)
|
||||
return () => ro.disconnect()
|
||||
}, [named])
|
||||
|
||||
const mmdd = today.slice(5)
|
||||
if (mmdd < SEASON.from || mmdd > SEASON.to) return null
|
||||
const spots = (data.spots as Spot[]).map((m) => ({ ...m, stage: stage(m, today) }))
|
||||
const peakNow = spots.filter((m) => m.stage === 'peak').map((m) => m.name)
|
||||
// 발표 지도에 서울 안 산은 없어 서울 시청에서 가장 가까운 단풍 예측 산으로
|
||||
const forecast = spots.filter((m) => m.kind === 'maple' && m.basis.startsWith('산림청'))
|
||||
const seoul = forecast.reduce((a, b) => (Math.hypot(b.lat - SEOUL.lat, b.lon - SEOUL.lon) < Math.hypot(a.lat - SEOUL.lat, a.lon - SEOUL.lon) ? b : a))
|
||||
const sel = spots.find((m) => m.name === picked)
|
||||
const order = [...spots].sort((a, b) => Number(a.stage === 'peak') - Number(b.stage === 'peak'))
|
||||
return (
|
||||
<PostFrame title="단풍 지도" account="단풍 지도" avatar="maple" caption={peakNow.length ? `지금 절정 ${peakNow.length}곳: ${peakNow.slice(0, 5).join(' · ')}${peakNow.length > 5 ? ' 외' : ''}` : undefined}>
|
||||
<div className="relative bg-[#fff6ea] px-4 pt-4 pb-3">
|
||||
<p className="m-0 text-[13px] font-extrabold opacity-75">단풍 지도 · {md(today)}</p>
|
||||
<p className="m-0 mt-1 font-jua text-[24px] leading-tight">서울 근교 {seoul.name} 절정 예상 {md(seoul.peak)}</p>
|
||||
<div className="relative mx-auto mt-1 aspect-square w-full">
|
||||
<img src="/foliage/korea-map.webp" alt="단풍 지도" className="absolute inset-0 size-full" />
|
||||
{spots.map((m) => (
|
||||
<button
|
||||
<div ref={mapRef} className="relative mx-auto mt-1 aspect-square w-full">
|
||||
<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)
|
||||
return (
|
||||
<button
|
||||
key={m.name}
|
||||
type="button"
|
||||
onClick={() => setPicked(m.name === picked ? null : m.name)}
|
||||
aria-label={`${m.name} ${KIND[m.kind]} ${STAGE[m.stage]}, 절정 예상 ${md(m.peak)}`}
|
||||
className={cx(
|
||||
'absolute -translate-x-1/2 -translate-y-1/2 cursor-pointer rounded-full border-[1.5px] border-white p-0 shadow-[0_1px_2px_rgb(40_48_74/0.35)]',
|
||||
DOT[m.stage],
|
||||
m.stage === 'peak' && m.kind === 'ginkgo' && 'bg-[#f2c230]',
|
||||
m.name === picked && 'z-[2] scale-150',
|
||||
)}
|
||||
style={{ left: `${x * 100}%`, top: `${y * 100}%` }}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
{named.map((m) => (
|
||||
<span
|
||||
key={m.name}
|
||||
type="button"
|
||||
onClick={() => setPicked(m.name === picked ? null : m.name)}
|
||||
aria-label={`${m.name} ${KIND[m.kind].label} ${STAGE[m.stage].label}, 절정 예상 ${md(m.peak)}`}
|
||||
className={cx('absolute grid -translate-x-1/2 -translate-y-1/2 cursor-pointer place-items-center p-0.5', m.name === picked && 'z-[2] scale-150')}
|
||||
style={pos(m)}
|
||||
data-label={m.name}
|
||||
aria-hidden="true"
|
||||
className={cx(
|
||||
'pointer-events-none absolute rounded-[7px] bg-white/92 px-[5px] py-px text-[11.5px] leading-[1.35] font-extrabold whitespace-nowrap text-[#b5321f] shadow-[0_1px_3px_rgb(40_48_74/0.18)]',
|
||||
!labelAt[m.name] && 'invisible',
|
||||
)}
|
||||
style={labelAt[m.name] ?? { left: 0, top: 0 }}
|
||||
>
|
||||
<Ic name={KIND[m.kind].icon} className={cx('drop-shadow-[0_0_1.5px_#fff] drop-shadow-[0_1px_1px_rgb(40_48_74/0.4)]', STAGE[m.stage].icon)} />
|
||||
</button>
|
||||
{m.name}
|
||||
</span>
|
||||
))}
|
||||
{sel && (
|
||||
<p className="absolute right-0 bottom-0 z-[3] m-0 max-w-[75%] rounded-[14px] bg-white/95 px-3 py-2 text-[13px] font-bold shadow-[0_4px_12px_rgb(40_48_74/0.15)]">
|
||||
<b className="font-jua text-[16px] font-normal">{sel.name}</b> {sel.region}
|
||||
<br />
|
||||
{KIND[sel.kind].label} {STAGE[sel.stage].label} · 절정 예상 {md(sel.peak)}
|
||||
{KIND[sel.kind]} {STAGE[sel.stage] && `${STAGE[sel.stage]} · `}절정 예상 {md(sel.peak)}
|
||||
{!sel.basis.startsWith('산림청') && <small className="block text-[11px] font-semibold text-(--ink3)">가까운 예측 지점 날짜예요</small>}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="mt-2 flex flex-wrap items-center justify-center gap-x-3 gap-y-1 text-[12.5px] font-bold">
|
||||
{(Object.keys(KIND) as Kind[]).map((k) => (
|
||||
<span key={k} className="flex items-center gap-1">
|
||||
<Ic name={KIND[k].icon} className="size-4" />
|
||||
{KIND[k].label}
|
||||
</span>
|
||||
))}
|
||||
<span className="opacity-30" aria-hidden="true">|</span>
|
||||
{(Object.keys(STAGE) as Stage[]).map((st) => (
|
||||
<span key={st} className="flex items-center gap-1">
|
||||
<Ic name="maple" className={cx('size-4', STAGE[st].icon)} />
|
||||
{STAGE[st].label}
|
||||
{LEGEND.map((l) => (
|
||||
<span key={l.label} className="flex items-center gap-1">
|
||||
<i className={cx('inline-block size-2.5 rounded-full border-[1.5px] border-white shadow-[0_0_0_1px_rgb(40_48_74/0.15)]', l.color)} />
|
||||
{l.label}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user