feat: 단풍 지도 페이지(/foliage) — 카드는 절정만 잎 아이콘, 누르면 페이지에서 확대·이동·목록, 확대할수록 이름 더, 뒤로 가면 피드 위치 그대로
This commit is contained in:
1 parent
446af2e9b5
commit
d75144a6ac
6 files changed
+652
-211
No files matched your search
@@ -0,0 +1,150 @@
|
||||
import { useLayoutEffect, useMemo, useState, type RefObject } from 'react'
|
||||
import data from '../../seo/foliage_2026.json'
|
||||
import { kst } from './lib'
|
||||
|
||||
export type Kind = 'maple' | 'ginkgo'
|
||||
export type Stage = 'before' | 'first' | 'peak' | 'gone'
|
||||
type Raw = { name: string; kind: Kind; region: string; lat: number; lon: number; peak: string; basis: string }
|
||||
/** x·y 는 지도 그림 안 위치(0~1), own 은 산림청 예측 지점 자기 날짜인지(아니면 가까운 지점에서 빌린 날짜) */
|
||||
export type Spot = Omit<Raw, 'basis' | 'lat' | 'lon'> & { stage: Stage; d: number; x: number; y: number; own: boolean }
|
||||
|
||||
// 지도 그림(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 }
|
||||
export const MAP_SRC = '/foliage/korea-map.webp'
|
||||
// 발표에 시작일이 없어 절정 FIRST_DAYS 일 전부터 첫 단풍, PEAK_DAYS 일 전부터 PEAK_AFTER 일 뒤까지 절정으로 본다
|
||||
const FIRST_DAYS = 14
|
||||
const PEAK_DAYS = 3
|
||||
const PEAK_AFTER = 7
|
||||
|
||||
export const STAGE: Record<Stage, string> = { before: '단풍 전', first: '첫 단풍', peak: '절정', gone: '절정 지남' }
|
||||
export const KIND: Record<Kind, string> = { maple: '단풍', ginkgo: '은행' }
|
||||
|
||||
const DAY = 86_400_000
|
||||
const days = (from: string, to: string) => Math.round((Date.parse(to) - Date.parse(from)) / DAY)
|
||||
|
||||
function stage(peak: string, today: string): Stage {
|
||||
const d = days(today, peak)
|
||||
if (d < -PEAK_AFTER) return 'gone'
|
||||
if (d <= PEAK_DAYS) return 'peak'
|
||||
return d <= FIRST_DAYS ? 'first' : 'before'
|
||||
}
|
||||
|
||||
export const md = (iso: string) => `${Number(iso.slice(5, 7))}/${Number(iso.slice(8, 10))}`
|
||||
const province = (m: Spot) => m.region.split(' ')[0].split('·')
|
||||
|
||||
/** 오늘(한국 시각) 기준 명소 65곳과 단계. 단풍철이 아니면 inSeason 이 false */
|
||||
export function useFoliage() {
|
||||
const [today] = useState(() => {
|
||||
const t = kst(new Date())
|
||||
return `${t.y}-${String(t.mo).padStart(2, '0')}-${String(t.d).padStart(2, '0')}`
|
||||
})
|
||||
return useMemo(() => {
|
||||
const spots: Spot[] = (data.spots as Raw[]).map(({ basis, lat, lon, ...m }) => ({
|
||||
...m,
|
||||
stage: stage(m.peak, today),
|
||||
d: days(today, m.peak),
|
||||
x: (MAP.ox + (lon - MAP.lon0) * MAP.k * MAP.s) / MAP.size,
|
||||
y: (MAP.oy + (MAP.lat1 - lat) * MAP.s) / MAP.size,
|
||||
own: basis.startsWith('산림청'),
|
||||
}))
|
||||
// 가장 이른 명소가 첫 단풍에 들 때부터 가장 늦은 명소의 절정이 끝날 때까지
|
||||
const peaks = spots.map((m) => m.peak).sort()
|
||||
const inSeason = days(peaks[0], today) >= -FIRST_DAYS && days(peaks[peaks.length - 1], today) <= PEAK_AFTER
|
||||
return { today, spots, inSeason }
|
||||
}, [today])
|
||||
}
|
||||
|
||||
/** 절정 명소가 많은 시·도 3곳, 절정이 없으면 가장 먼저 올 절정, 다 지났으면 지는 중 */
|
||||
export function headline(spots: Spot[]) {
|
||||
const peak = spots.filter((m) => m.stage === 'peak')
|
||||
if (peak.length) {
|
||||
const count = new Map<string, number>()
|
||||
for (const p of peak.flatMap(province)) count.set(p, (count.get(p) ?? 0) + 1)
|
||||
return `지금 절정: ${[...count].sort((a, b) => b[1] - a[1]).slice(0, 3).map(([p]) => p).join('·')}`
|
||||
}
|
||||
if (spots.some((m) => m.stage === 'gone')) return '올해 단풍이 지고 있어요'
|
||||
const first = spots.reduce((a, b) => (b.peak < a.peak ? b : a))
|
||||
return `첫 절정은 ${province(first)[0]}, ${md(first.peak)}쯤`
|
||||
}
|
||||
|
||||
export type Pt = { m: Spot; x: number; y: number; r: number }
|
||||
export 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
|
||||
|
||||
/** 화면 위 점끼리 겹치면 서로 밀어 떼어 놓는다 (pts 를 고쳐서 돌려줌) */
|
||||
export function spread(pts: Pt[], gap = 1.5) {
|
||||
for (let it = 0; it < 60; 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
|
||||
}
|
||||
return pts
|
||||
}
|
||||
|
||||
/** 이름표를 점 오른쪽·왼쪽·위·아래 순으로 놓아 보고, 점·다른 이름표·막힌 칸과 겹치거나 영역 밖이면 그 이름은 뺀다. 앞 순서가 먼저 자리를 잡는다 */
|
||||
export function placeLabels(order: Pt[], all: Pt[], size: Record<string, { w: number; h: number }>, area: Box, blocked: Box[] = []) {
|
||||
const dots = all.map((p) => ({ name: p.m.name, box: { l: p.x - p.r, t: p.y - p.r, r: p.x + p.r, b: p.y + p.r } }))
|
||||
const taken = [...blocked.map((box) => ({ name: '', box })), ...dots]
|
||||
const out: Record<string, { left: number; top: number }> = {}
|
||||
for (const p of order) {
|
||||
const s = size[p.m.name]
|
||||
if (!s || p.x < area.l || p.x > area.r || p.y < area.t || p.y > area.b) continue
|
||||
const tries = [
|
||||
[p.r + 2, -s.h / 2],
|
||||
[-s.w - p.r - 2, -s.h / 2],
|
||||
[-s.w / 2, -s.h - p.r - 1],
|
||||
[-s.w / 2, p.r + 1],
|
||||
]
|
||||
for (const [dx, dy] of tries) {
|
||||
const r = { l: p.x + dx, t: p.y + dy, r: p.x + dx + s.w, b: p.y + dy + s.h }
|
||||
if (r.l < area.l || r.t < area.t || r.r > area.r || r.b > area.b) continue
|
||||
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
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** 이름표 크기를 재 둔다: host 안 data-label 이 붙은 요소의 폭·높이 (host 크기가 바뀌면 다시) */
|
||||
export function useLabelSize(host: RefObject<HTMLElement | null>, key: unknown) {
|
||||
const [size, setSize] = useState<Record<string, { w: number; h: number }>>({})
|
||||
useLayoutEffect(() => {
|
||||
const el = host.current
|
||||
if (!el) return
|
||||
const ro = new ResizeObserver(() => {
|
||||
const out: Record<string, { w: number; h: number }> = {}
|
||||
el.querySelectorAll<HTMLElement>('[data-label]').forEach((l) => {
|
||||
out[l.dataset.label ?? ''] = { w: l.offsetWidth, h: l.offsetHeight }
|
||||
})
|
||||
setSize(out)
|
||||
})
|
||||
ro.observe(el)
|
||||
return () => ro.disconnect()
|
||||
}, [host, key])
|
||||
return size
|
||||
}
|
||||
|
||||
export const LEAF_SHADOW = 'drop-shadow-[0_0_1.5px_#fff] drop-shadow-[0_1px_1px_rgb(40_48_74/0.45)]'
|
||||
export const LABEL = 'absolute rounded-[7px] bg-white/92 px-[5px] py-px text-[11.5px] leading-[1.35] font-extrabold whitespace-nowrap shadow-[0_1px_3px_rgb(40_48_74/0.18)]'
|
||||
export const LABEL_INK: Record<Kind, string> = { maple: 'text-[#b5321f]', ginkgo: 'text-[#8a6508]' }
|
||||
// 지도 그림 색을 빼서 단풍 아이콘이 묻히지 않게
|
||||
export const MAP_IMG = 'pointer-events-none absolute inset-0 size-full select-none mix-blend-multiply brightness-[1.08] saturate-[.3]'
|
||||
export const ICON_R = 10
|
||||
Reference in new issue
Block a user