surface.ts
10.2 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { Frame, type FrameScale, type OverlayMap } from './frame';
/**
* 一块自绘画布:尺寸对齐、空闲跳过、收起时交还显存。
*
* 三件事各自都值一点钱,合在一起才看得出为什么放在一处:
* 1) **空闲不重画**。`alwaysRender: true` 时底图每帧都回调,各层每帧都把整屏
* clearRect + 重投几百个点 + 重绘,页面停在那里不动也在满帧烧 CPU;
* 烧掉的主线程与合成时间正是「拖动之外,操作界面也卡」的来源。
* 现在视图与本层都没变就整帧跳过、画布内容原样留着 —— 这一条是空闲减载的实际落点。
* 会自己动的层走 beginAnimated:它照样出帧,但只清自己那一小块,损坏区域不扩到全屏。
* 2) **画布尺寸**。DPR 上限 2;尺寸没变时不再赋值 width(赋值会清空整块画布并重建 backing store)。
* 3) **收起即交还**。一张全屏画布在 DPR 2 下约占 30MB,几层叠起来就是上百 MB,
* 而「偶尔才出现」的层用完就该把像素还回去。
*/
/** 内核不认识调试面板,只把「要不要给这一帧计时」交给宿主决定 */
export interface SurfaceOptions {
/** 投影口径(瓦片尺寸 + 参考纬度)。宿主传一份,各层共用 */
scale: FrameScale;
/** 底图是否逐帧回调(见 overlay.ts 里那条实测结论) */
alwaysRender: boolean;
/** 返回真值时这一帧才取时钟读数。关掉遥测时一帧只多一个布尔判断 */
shouldTime?: () => boolean;
/** DPR 上限,默认 2。调到 1 省下的是 backing store,代价是字与细环发虚 */
maxDpr?: number;
}
/** 内核知道的这几项读数(层的身份与「建没建、收没收起」由宿主自己补) */
export interface SurfaceReadout {
/** backing store 是否已交还(画布宽高归零) */
released: boolean;
cssW: number;
cssH: number;
dpr: number;
/** 本层数据量 */
total: number;
/** 上一帧真正落笔的个数 */
painted: number;
/** 累计:落笔帧数与被"视图与数据都没变"挡掉的帧数 */
drawn: number;
skipped: number;
/** 上一帧落笔用了多久(宿主没要计时时恒为 0) */
lastMs: number;
/** 上一帧是不是减载帧 */
light: boolean;
}
export class CanvasSurface {
readonly canvas: HTMLCanvasElement;
readonly frame = new Frame();
private ctx: CanvasRenderingContext2D | null = null;
private zoom = Number.NaN;
private lng = Number.NaN;
private lat = Number.NaN;
private w = -1;
private h = -1;
private dpr = -1;
/** 视图没变但数据/选项变了,下一帧仍要重画 */
private dirty = true;
/* 帧计数与耗时用来回答"停着不动到底有没有在画"。painted 与 total 分开记,
是因为减载那一帧 total 一条不少而 painted 是 0 —— 那正是最容易读成"数据没了"的一格。 */
private cntDrawn = 0;
private cntSkipped = 0;
private lastMs = 0;
private lastPainted = 0;
private total = 0;
private lastLight = false;
private t0 = 0;
constructor(private readonly opts: SurfaceOptions) {
const canvas = document.createElement('canvas');
canvas.style.position = 'absolute';
canvas.style.left = '0';
canvas.style.top = '0';
// 必须让地图继续接收鼠标事件,否则 canvas 会把拖动、缩放全部吃掉
canvas.style.pointerEvents = 'none';
this.canvas = canvas;
}
/**
* 数据、选项、可见性这些本层自己的变化:即使视图一动没动,下一帧也得重画。
*
* 底图的逐帧回调在时,标脏就够了(下一帧它自己会来问)。
* `schedule()` 顺手排的那一帧是给 `alwaysRender = false` 那条路备着的。
*
* ⚠️ 只有**这一帧真的会落笔**的层才该走到这里(各层在自己的出帧口开头判掉:
* 收起、无数据、没有标记都算「不能落笔」)。脏标记会一直留到下一次展开,
* 而展开那一路本来就要标脏并整帧重画 —— 收起期间点的名一笔都用不上,
* 只是让「谁在什么状态下欠一帧」这件事变得无法推理。规则集中在这里,各层同一套。
*/
invalidate(): void {
this.dirty = true;
}
/** 本层数据量。喂数据那一次写一次,帧里不再碰(是一次整数赋值,可忽略) */
setTotal(n: number): void {
this.total = n;
}
/**
* 这一帧落笔完了:记下实际画了几笔、是不是减载帧、以及这一帧花了多久。
*
* 各层在自己的 draw 末尾叫一次。耗时只在宿主开了计时时取,生产路径上那两次读数不必付。
*/
finishPaint(painted: number, light: boolean): void {
this.lastPainted = painted;
this.lastLight = light;
if (!this.opts.shouldTime?.()) return;
this.lastMs = performance.now() - this.t0;
}
/**
* 每帧开场。
*
* @returns null 表示这一帧什么都不必重画(视图与本层都没变),调用方直接 return;
* 非 null 时画布已经对齐尺寸、设好 DPR 变换并清空,附带这一帧的投影基准。
*/
begin(map: OverlayMap): Frame | null {
return this.open(map, false)?.frame ?? null;
}
/**
* 每帧开场(会自己动的层专用,见呼吸光圈那一类标记层)。
*
* 与 begin 的差别只有一处:视图没变时也把投影基准交回来,让调用方继续画下一帧动画,
* 但**不清屏** —— 整屏清空会把这块画布的损坏区域扩到全屏,浏览器每帧就得重传一整张
* 全屏纹理,三十帧一秒下来「一个小小的呼吸光圈」就能变成整个页面的合成负担。
* 视图真的变了才整屏清空(那时画面本来就要重排),否则由调用方只清自己那一小块。
*
* @returns null 只在拿不到 2d 上下文时出现;clearedAll 告诉调用方这一帧要不要自己局部清屏。
*/
beginAnimated(map: OverlayMap): { frame: Frame; clearedAll: boolean } | null {
return this.open(map, true);
}
private open(map: OverlayMap, animated: boolean): { frame: Frame; clearedAll: boolean } | null {
const size = map.getSize();
const dpr = Math.min(window.devicePixelRatio || 1, this.opts.maxDpr ?? 2);
const center = map.getCenter();
const zoom = map.getZoom();
const lng = center.getLng();
const lat = center.getLat();
const unchanged =
!this.dirty &&
zoom === this.zoom &&
lng === this.lng &&
lat === this.lat &&
size.width === this.w &&
size.height === this.h &&
dpr === this.dpr;
if (unchanged) {
// 动画层继续用上一帧的投影基准:视图一动没动,原点与比例尺还是那一套
if (!animated || !this.ctx) {
if (this.opts.shouldTime?.()) this.cntSkipped += 1;
return null;
}
return { frame: this.frame, clearedAll: false };
}
if (this.opts.shouldTime?.()) {
this.cntDrawn += 1;
this.lastPainted = 0;
this.t0 = performance.now();
}
this.dirty = false;
this.zoom = zoom;
this.lng = lng;
this.lat = lat;
this.w = size.width;
this.h = size.height;
this.dpr = dpr;
const ctx = this.ctx ?? this.canvas.getContext('2d');
if (!ctx) return null;
this.ctx = ctx;
const px = Math.round(size.width * dpr);
const py = Math.round(size.height * dpr);
if (this.canvas.width !== px || this.canvas.height !== py) {
this.canvas.width = px;
this.canvas.height = py;
this.canvas.style.width = `${size.width}px`;
this.canvas.style.height = `${size.height}px`;
}
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, size.width, size.height);
this.frame.set(map, zoom, lng, lat, this.opts.scale);
return { frame: this.frame, clearedAll: true };
}
/** 画布宽高。裁剪判定用它,省得每层再问一次 getSize */
get width(): number {
return this.w;
}
get height(): number {
return this.h;
}
/** 本层绘制用的 2d 上下文。begin() 给出非 null 的投影基准时,它一定已经就位 */
get context(): CanvasRenderingContext2D | null {
return this.ctx;
}
/** 遥测读数。宿主(调试面板那类东西)按需取,内核自己不注册、不订阅 */
readout(): SurfaceReadout {
return {
released: this.canvas.width === 0,
cssW: this.w < 0 ? 0 : this.w,
cssH: this.h < 0 ? 0 : this.h,
dpr: this.dpr < 0 ? 0 : this.dpr,
total: this.total,
painted: this.lastPainted,
drawn: this.cntDrawn,
skipped: this.cntSkipped,
lastMs: this.lastMs,
light: this.lastLight,
};
}
/**
* 收起这一层时把 backing store 交还给浏览器。
*
* 层的 show/hide 由底图负责,画布像素却还占着内存并参与合成;「偶尔出现」的层
* 用完不放手就会攒出好几张全屏画布。
* 重新展开时 begin() 会因为尺寸对不上而整帧重画,不需要额外处理。
*/
release(): void {
if (this.canvas.width !== 0 || this.canvas.height !== 0) {
this.canvas.width = 0;
this.canvas.height = 0;
this.w = -1;
this.h = -1;
// 屏上一颗像素都不剩了:读数里的"落笔数"跟着归零,别留着上一屏的数字
this.lastPainted = 0;
}
this.dirty = true;
}
/**
* 把「数据变了但地图没动」这类重绘请求合并到同一帧。
*
* 每个层原先各写一份 requestAnimationFrame + 布尔标志,行为必须一致(同一批变化只画一次),
* 所以这里连 invalidate 一起收:调用方只需 surface.schedule()。
*
* ⚠️ `alwaysRender` 为 `true` 时只标脏、不自己排帧:底图每帧都来问,
* 我们再挂一条 rAF 就等于同一帧里两个地方喊画(先到的画完,后到的空转一次)。
*/
private scheduled = false;
schedule(draw: () => void): void {
this.invalidate();
if (this.opts.alwaysRender) return;
if (this.scheduled) return;
this.scheduled = true;
requestAnimationFrame(() => {
this.scheduled = false;
draw();
});
}
destroy(): void {
this.ctx = null;
this.canvas.width = 0;
this.canvas.height = 0;
}
}