forceRender.test.ts
4.68 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
/**
* 落定帧强制重画守卫(2026-11-24)。
*
* 现场:百度 zoomend 之后还有一帧自己的收尾渲染,会把自绘画布盖住;而此时视图参数
* 与上一帧相同,Surface.begin 会判"没变"直接跳过 —— 表现就是"滚轮滚动时能看到点,
* 一停下来点必然消失"。修法是给 render 一个 force 通道,先标脏再走正常绘制。
*
* 这里钉住两件事:常规帧在视图不变时确实跳过(空闲减载不能退化),
* 而 force 帧必须无视这个跳过、真的把画布重画一遍。
*/
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { createGestureState } from '../src/gesture';
import { createBucketsLayer, type BucketsLayer } from '../src/layers/buckets';
import { FakeCustomLayer, fakeCtx, fakeMap } from './fakes';
import type { FrameScale } from '../src/frame';
const SCALE: FrameScale = { tileSize: 256, referenceLat: 22.5 };
const ctx = fakeCtx();
const alive: BucketsLayer[] = [];
let restoreCanvas: (() => void) | null = null;
let restoreAmap: (() => void) | null = null;
beforeEach(() => {
ctx.reset();
FakeCustomLayer.created = [];
const proto = window.HTMLCanvasElement.prototype as unknown as {
getContext: (id: string) => unknown;
};
const original = proto.getContext;
proto.getContext = () => ctx;
restoreCanvas = () => {
proto.getContext = original;
};
const g = globalThis as Record<string, unknown>;
const previous = g.AMap;
g.AMap = { CustomLayer: FakeCustomLayer };
restoreAmap = () => {
g.AMap = previous;
};
});
afterEach(() => {
while (alive.length) alive.pop()?.destroy();
restoreCanvas?.();
restoreAmap?.();
});
function setup() {
const { map } = fakeMap({ zoom: 11, lng: 114.06, lat: 22.62, w: 800, h: 600 });
const layer = createBucketsLayer(map, {
zIndex: 130,
scale: SCALE,
gesture: createGestureState(),
style: {
font: 'bold 11px sans-serif',
plate: 'rgba(255,255,255,0.4)',
halo: 'rgba(255,255,255,0.9)',
haloWidth: 3,
ink: '#222B5F',
minRadius: 9,
padX: 3,
},
});
alive.push(layer);
layer.setData({
lng: [114.0, 114.1],
lat: [22.6, 22.65],
count: [5, 12],
});
const fn = FakeCustomLayer.created.at(-1)?.render;
if (!fn) throw new Error('假 CustomLayer 没拿到 render 回调');
return { layer, fn };
}
describe('落定帧强制重画', () => {
it('视图不变时常规帧跳过,不重复清屏', () => {
const { fn } = setup();
fn(); // 第一帧落笔
const afterFirst = ctx.calls.get('clearRect') ?? 0;
expect(afterFirst).toBe(1);
fn(); // 视图没动 → begin 返回 null,整帧跳过
expect(ctx.calls.get('clearRect') ?? 0).toBe(afterFirst);
});
it('force 帧无视"视图没变",重新清屏并重画(滚轮停下点不消失)', () => {
const { fn } = setup();
fn();
const afterFirst = ctx.calls.get('clearRect') ?? 0;
fn(true); // 落定帧:标脏后重画
expect(ctx.calls.get('clearRect') ?? 0).toBe(afterFirst + 1);
// 数字重新落笔(不是只清屏不画)
expect(ctx.texts.length).toBeGreaterThan(0);
});
});
describe('底图出帧能力(perFrameRender)', () => {
it('百度运行时(不逐帧回调):数据更新时 schedule 自己排 rAF 兑现重绘', async () => {
const { map } = fakeMap({ zoom: 11, lng: 114.06, lat: 22.62, w: 800, h: 600 });
const g = globalThis as Record<string, unknown>;
const previous = g.AMap;
// 百度运行时:显式声明底图不逐帧回调
const baiduRuntime = { CustomLayer: FakeCustomLayer, perFrameRender: false };
g.AMap = undefined;
const rafs: Array<() => void> = [];
const origRaf = window.requestAnimationFrame;
window.requestAnimationFrame = ((cb: FrameRequestCallback) => {
rafs.push(() => cb(0));
return rafs.length;
}) as typeof window.requestAnimationFrame;
const layer = createBucketsLayer(map, {
zIndex: 130,
scale: SCALE,
gesture: createGestureState(),
runtime: baiduRuntime,
style: {
font: 'bold 11px sans-serif',
plate: 'rgba(255,255,255,0.4)',
halo: 'rgba(255,255,255,0.9)',
haloWidth: 3,
ink: '#222B5F',
minRadius: 9,
padX: 3,
},
});
alive.push(layer);
layer.setData({ lng: [114.0], lat: [22.6], count: [5] });
// 建层即会 invalidate → 应排一条 rAF(百度不逐帧,必须自己排)
expect(rafs.length).toBeGreaterThan(0);
const before = ctx.calls.get('clearRect') ?? 0;
rafs.shift()?.();
expect(ctx.calls.get('clearRect') ?? 0).toBe(before + 1);
window.requestAnimationFrame = origRaf;
g.AMap = previous;
});
});