frame.ts
6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { M_PER_EQUATOR, TILE_SIZE } from './constants';
/**
* 内核向底图要的四件事。
*
* AMap.Map 结构上就满足这个形状(不要求它 implements 什么)。换 MapLibre / Leaflet 时
* 只要写一个同样形状的适配器 —— 这四下调用是全内核唯一碰底图对象的地方。
*
* ⚠️ 前提是 2D、不旋转、不倾斜:`getZoom()` 要能在动画途中给小数(帧判定要用它),
* `lngLatToContainer` 要按容器像素返回。旋转或倾斜之后屏幕坐标不再是经纬度的这个函数,
* 每帧两次乘加的那套换算就不成立了(见 Frame 的注释)。
*/
export interface OverlayMap {
getSize(): { width: number; height: number };
getCenter(): { getLng(): number; getLat(): number };
/** 当前缩放级别(动画途中是小数) */
getZoom(): number;
/** 经纬度 → 容器像素。每帧只准问一次中心点,见 Frame.set */
lngLatToContainer(lnglat: [number, number]): { x: number; y: number };
/**
* 厂商自己的投影比例尺。可选:高德兼容标准 256 瓦片墨卡托,不提供即走宿主默认口径。
*
* 百度 GL 4.0 的缩放单位与 256 瓦片不同(getZoomUnits(z)=2^(18-z),
* 等效 world/tileSize=40075016/2^18≈152.874),沿用 256 会让点位相对中心放大约 1.675 倍:
* 拖一次错一点、叠出两个地图、缩放停止时错位。厂商门面把真实口径挂在这里,
* 全内核所有 scale 消费点(surface / 各层 frame.set)统一优先读它。
*/
frameScale?: FrameScale;
}
/** 屏幕换算的比例尺口径 */
export interface FrameScale {
/** 一级缩放的世界宽度(CSS 像素),见 constants 里 TILE_SIZE 那段 */
tileSize: number;
/** 米 → 像素的参考纬度:底图覆盖的城市中心纬度 */
referenceLat: number;
/**
* 纬度方向的墨卡托斜率修正(默认 1 = 标准球形墨卡托)。
*
* 百度 GL 用椭球投影,标准 mercY() 在深圳一带的斜率比它大 ~0.576%。
* 不修正时,离中心 0.1° 的南北向点位能差 100px+ —— 拖一次跑一点、叠出两个地图的
* 南北向来源。用百度投影在城市中心标定的单一斜率即可,深圳全境误差 < 1px。
*/
latSlope?: number;
}
/** 深圳纬度。业务层要改参考纬度就自己传一份 FrameScale */
export const DEFAULT_SCALE: FrameScale = { tileSize: TILE_SIZE, referenceLat: 22.5 };
/**
* 墨卡托纬度:tan(π/4 + φ/2) 的自然对数。
*
* 存这个数而不是存像素,是因为它与缩放无关 —— 喂一次数据算一次,缩放时只需换比例尺。
* 纬度截到 ±85°,与墨卡托 Web 瓦片的可用区间一致(超出会趋向无穷)。
*/
export function mercY(latDeg: number): number {
const phi = (Math.max(-85, Math.min(85, latDeg)) * Math.PI) / 180;
return Math.log(Math.tan(Math.PI / 4 + phi / 2));
}
/** 这一级缩放下的世界宽度(CSS 像素):每升一级翻一倍 */
export function worldPx(zoom: number, tileSize: number = TILE_SIZE): number {
return tileSize * Math.pow(2, zoom);
}
/** 这一级缩放下每度经度占多少 CSS 像素(格子的边长、点之间的屏距都从它推) */
export function pxPerDegLng(zoom: number, tileSize: number = TILE_SIZE): number {
return worldPx(zoom, tileSize) / 360;
}
/**
* 一帧的投影基准。
*
* 为什么要有这一层 —— 投影如果逐点问底图(每点一次 `lngLatToContainer`),一帧几百到两千次调用
* 就是几百到两千次「造 LngLat、投影、返回新对象」的分配。拖动时这项是逐帧付的钱,空闲时也在付
* (底图每帧回调覆盖物),于是「地图拖着拖着就越来越沉」。
*
* 改法是把投影拆成「一次换算 + 每帧两次乘加」:
* 墨卡托下屏幕位置只由 (缩放, 中心) 决定,
* x = 中心像素x + (经度 − 中心经度) × 每度像素,
* y = 中心像素y + (中心的那一支墨卡托纬度 − 这一点的) × 每像素纬度因子。
* 每一点的纬度因子(mercY)与缩放无关,所以喂数据时算好存下来,每帧只剩减法和乘法。
*
* ⚠️ 中心那一点的位置仍然向底图问一次(每帧一次,不是每点一次):这样原点由底图自己说,
* 而不是我们假设它在画布正中。地图带偏移、容器尺寸与 getSize() 不一致时,
* 画出来的东西不会整屏错位。
*/
export class Frame {
/** 缩放级别(动画途中是小数) */
zoom = 0;
/** 每度经度的像素 */
pxPerDegLng = 0;
/** 米 → 屏幕像素(按参考纬度:圆盘半径、方格边长这些米制口径都换它) */
pxPerMeter = 0;
private originX = 0;
private originY = 0;
private centerLng = 0;
private centerMercY = 0;
private pxPerMercY = 0;
/** 纬度斜率修正:百度椭球投影 < 1,标准球形墨卡托 = 1 */
private latSlope = 1;
set(
map: OverlayMap,
zoom: number,
centerLng: number,
centerLat: number,
scale: FrameScale,
): void {
this.zoom = zoom;
this.centerLng = centerLng;
this.centerMercY = mercY(centerLat);
this.latSlope = scale.latSlope ?? 1;
const world = worldPx(zoom, scale.tileSize);
this.pxPerDegLng = world / 360;
this.pxPerMercY = world / (2 * Math.PI);
this.pxPerMeter =
this.pxPerDegLng / (M_PER_EQUATOR * Math.cos((scale.referenceLat * Math.PI) / 180));
// 每帧唯一的一次投影调用:把原点交给底图本身
const p = map.lngLatToContainer([centerLng, centerLat]);
this.originX = p.x;
this.originY = p.y;
}
/** 经度 → 屏幕 x */
x(lng: number): number {
return this.originX + (lng - this.centerLng) * this.pxPerDegLng;
}
/** mercY(纬度) → 屏幕 y。纬度值请在喂数据时用 mercY() 算好存下来 */
y(mercLatitude: number): number {
return (
this.originY + (this.centerMercY - mercLatitude) * this.latSlope * this.pxPerMercY
);
}
}
/**
* 量一段文字在画布上占多少像素。
*
* 用的是一块专门的量字画布(64×64,只建一次),不是各层自己那块绘制画布:
* 底衬半径要在喂数据时算好,而那一趟常常发生在绘制画布还没尺寸的时候(层刚建、
* 或收起后交还过 backing store)。拿 0×0 的画布去 measureText 是「也许还给宽度」的灰区,
* 量不到就只能按字数估,表现是底衬集体缩到最小一档 —— 一类没人会去查的视觉退化。
*
* 返回 null 表示量不了(无 canvas 的环境,例如测试里的 jsdom),调用方自备估算值。
*/
let measurer: CanvasRenderingContext2D | null = null;
let measurerTried = false;
export function textWidth(font: string, text: string): number | null {
if (!measurerTried) {
measurerTried = true;
const canvas = document.createElement('canvas');
canvas.width = 64;
canvas.height = 64;
measurer = canvas.getContext('2d');
}
if (!measurer) return null;
measurer.font = font;
return measurer.measureText(text).width;
}