fix: 修复移动端报错
This commit is contained in:
@@ -127,11 +127,25 @@ function toFeature(loc: KeyLoc, side: 'us' | 'iran', status?: BaseStatus) {
|
||||
|
||||
const FLIGHT_DURATION_MS = 2500 // 光点飞行单程时间
|
||||
|
||||
/** 移动端/小屏降低动画更新频率以减轻卡顿;返回最小间隔 ms */
|
||||
function getAnimIntervalMs(): number {
|
||||
try {
|
||||
if (typeof window === 'undefined') return 33
|
||||
const reducedMotion =
|
||||
window.matchMedia('(prefers-reduced-motion: reduce)').matches
|
||||
if (reducedMotion) return 100 // 约 10fps,兼顾可访问性
|
||||
return window.innerWidth <= 768 ? 50 : 33 // 移动端约 20fps,桌面约 30fps
|
||||
} catch {
|
||||
return 33
|
||||
}
|
||||
}
|
||||
|
||||
export function WarMap() {
|
||||
const mapRef = useRef<MapRef>(null)
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const animRef = useRef<number>(0)
|
||||
const startRef = useRef<number>(0)
|
||||
const lastAnimUpdateRef = useRef<number>(0)
|
||||
const attackPathsRef = useRef<[number, number][][]>([])
|
||||
const lincolnPathsRef = useRef<[number, number][][]>([])
|
||||
const fordPathsRef = useRef<[number, number][][]>([])
|
||||
@@ -293,10 +307,15 @@ export function WarMap() {
|
||||
|
||||
const tick = (t: number) => {
|
||||
const elapsed = t - startRef.current
|
||||
const zoom = map.getZoom()
|
||||
const zoomScale = Math.max(0.5, zoom / 4.2) // 随镜头缩放:放大变大、缩小变小(4.2 为默认 zoom)
|
||||
try {
|
||||
// 光点从起点飞向目标的循环动画
|
||||
const intervalMs = getAnimIntervalMs()
|
||||
const shouldUpdate = t - lastAnimUpdateRef.current >= intervalMs
|
||||
if (shouldUpdate) lastAnimUpdateRef.current = t
|
||||
|
||||
if (shouldUpdate) {
|
||||
const zoom = map.getZoom()
|
||||
const zoomScale = Math.max(0.5, zoom / 4.2) // 随镜头缩放:放大变大、缩小变小(4.2 为默认 zoom)
|
||||
try {
|
||||
// 光点从起点飞向目标的循环动画
|
||||
const src = map.getSource('attack-dots') as { setData: (d: GeoJSON.FeatureCollection) => void } | undefined
|
||||
const paths = attackPathsRef.current
|
||||
if (src && paths.length > 0) {
|
||||
@@ -408,7 +427,8 @@ export function WarMap() {
|
||||
map.setPaintProperty('gdelt-events-red-pulse', 'circle-radius', r)
|
||||
map.setPaintProperty('gdelt-events-red-pulse', 'circle-opacity', opacity)
|
||||
}
|
||||
} catch (_) {}
|
||||
} catch (_) {}
|
||||
}
|
||||
animRef.current = requestAnimationFrame(tick)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user