hotspotLayer.test.ts
8.81 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
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { HotspotLayer, type HotspotGrid } from './HotspotLayer';
import type { HotspotRow } from '../aggregate/types';
/**
* 黑点图形的绘制守卫。
*
* 这一组只盯一件事:一片黑点的范围是**一条闭合轮廓**画出来的,不是一堆格子边。
*
* 为什么值得钉死:逐条格边 stroke 时,每段都是独立子路径、butt 端点,90° 台阶拐角
* 外侧缺一个三角。格子越密缺口越多,屏幕上就是一堆没连起来的短线(实测截图如此,
* 用户的原话是「线条杂乱 都没有连接起来」)。闭合环一次 stroke 后,拐角由 lineJoin
* 连起来,这个失效模式就回不来了 —— 而它只在真实地图上肉眼可见,typecheck 与
* 数字类断言都发现不了,所以只能把「画了几条子路径」记下来守。
*/
interface FakeCtx {
calls: Map<string, number>;
[key: string]: unknown;
}
function fakeCtx(): FakeCtx {
const calls = new Map<string, number>();
const hit = (name: string) => calls.set(name, (calls.get(name) ?? 0) + 1);
const ctx: FakeCtx = { calls };
for (const name of [
'setTransform',
'clearRect',
'beginPath',
'closePath',
'moveTo',
'lineTo',
'rect',
'arc',
'arcTo',
'quadraticCurveTo',
'fill',
'stroke',
'fillRect',
'fillText',
'strokeText',
'save',
'restore',
'translate',
'setLineDash',
'clip',
]) {
ctx[name] = () => hit(name);
}
ctx.measureText = (text: string) => ({ width: String(text).length * 6 });
/**
* fill 单独记一笔:光雾朝内的那一半靠 destination-out 擦掉,是**用 fill 擦的**。
* 只看 fill 的总数分不出「擦」和「填」,所以按调用那一刻的合成模式再记一个计数。
*/
ctx.fill = () => {
hit('fill');
if (ctx.globalCompositeOperation === 'destination-out') hit('erase');
};
return ctx;
}
class FakeCustomLayer {
static created: FakeCustomLayer[] = [];
render: (() => void) | null = null;
constructor(readonly canvas: HTMLCanvasElement) {
FakeCustomLayer.created.push(this);
}
setMap() {}
show() {}
hide() {}
}
const VIEW = { zoom: 13, lng: 114.0015, lat: 22.5015, w: 800, h: 600 };
function fakeMap() {
return {
getSize: () => ({ width: VIEW.w, height: VIEW.h }),
getCenter: () => ({ getLng: () => VIEW.lng, getLat: () => VIEW.lat }),
getZoom: () => VIEW.zoom,
lngLatToContainer: () => ({ x: VIEW.w / 2, y: VIEW.h / 2 }),
containerToLngLat: () => ({ getLng: () => VIEW.lng, getLat: () => VIEW.lat }),
} as unknown as AMap.Map;
}
const ctx = fakeCtx();
let restoreCanvas: (() => void) | null = null;
let restoreAmap: (() => void) | null = null;
beforeEach(() => {
ctx.calls.clear();
FakeCustomLayer.created = [];
const proto = window.HTMLCanvasElement.prototype as unknown as { getContext: () => unknown };
const original = proto.getContext;
proto.getContext = () => ctx;
restoreCanvas = () => {
proto.getContext = original;
};
const g = globalThis as Record<string, unknown>;
const prev = g.AMap;
g.AMap = { CustomLayer: FakeCustomLayer };
restoreAmap = () => {
g.AMap = prev;
};
});
afterEach(() => {
restoreCanvas?.();
restoreAmap?.();
});
/** 100m 见方的栅格,域是一块 4×4(16 格) */
const GRID: HotspotGrid = {
originLng: 114.0,
originLat: 22.5,
dLng: 0.001,
dLat: 0.001,
w: 40,
h: 40,
};
function squareDomain(): { mask: Uint8Array; row: HotspotRow } {
const mask = new Uint8Array(GRID.w * GRID.h);
for (let y = 0; y < 4; y += 1) {
for (let x = 0; x < 4; x += 1) mask[y * GRID.w + x] = 1;
}
const row = {
id: 0,
domainId: 1,
lng: 114.0015,
lat: 22.5015,
count: 42,
cells: 16,
radiusM: 113,
cellMeters: 100,
} as unknown as HotspotRow;
return { mask, row };
}
describe('黑点图形 · 一片一条闭合轮廓', () => {
it('4×4 的域只产生 1 条闭合子路径,而不是 16 个格子的边', () => {
const layer = new HotspotLayer(fakeMap());
const { mask, row } = squareDomain();
layer.setData([row], GRID, mask);
const render = FakeCustomLayer.created[0]?.render;
expect(render).toBeTypeOf('function');
render!();
// 一条闭合环 = 一次 closePath;旧的逐格描边根本不 closePath。
// 注意排名牌也走 roundRectPath(内部会 closePath),所以不能断言「恰好 1 次」,
// 改用 moveTo 的次数来区分:整片轮廓只有 1 个起点,旧画法每条格边都是一个起点。
expect(ctx.calls.get('closePath') ?? 0).toBeGreaterThanOrEqual(1);
expect(ctx.calls.get('moveTo') ?? 0).toBeLessThanOrEqual(5);
// 旧画法每格一个 ctx.rect;闭合环用 lineTo 连边,一次 rect 都不该有
expect(ctx.calls.get('rect') ?? 0).toBe(0);
expect(ctx.calls.get('stroke') ?? 0).toBeGreaterThanOrEqual(1);
expect(ctx.calls.get('fill') ?? 0).toBeGreaterThanOrEqual(1);
// 圆润:轮廓的每个顶点一次二次贝塞尔(4×4 方块去共线后 4 个角)。
// 现在整圈走 quadraticCurveTo(见 roundedPath),不再是 arcTo 的小倒角;排名牌另走 roundRectPath。
expect(ctx.calls.get('quadraticCurveTo') ?? 0).toBeGreaterThanOrEqual(4);
// 外发光 = 同一路径两层宽描边 + 一层主边,至少 3 次 stroke
expect(ctx.calls.get('stroke') ?? 0).toBeGreaterThanOrEqual(3);
layer.destroy();
});
it('轮廓内的点能命中这一片,轮廓外的点不命中', () => {
const layer = new HotspotLayer(fakeMap());
const { mask, row } = squareDomain();
layer.setData([row], GRID, mask);
FakeCustomLayer.created[0]?.render?.();
// 视图中心就是这一片的中心 → 屏幕中心必落在格子内
expect(layer.hitAt(VIEW.w / 2, VIEW.h / 2)).toBe(0);
// 域只占中心一小块,左上角远在域外
expect(layer.hitAt(30, 30)).toBe(-1);
layer.destroy();
});
it('没拿到 grid+mask 时退回圆:走 arc,不画格子路径', () => {
const layer = new HotspotLayer(fakeMap());
const { row } = squareDomain();
layer.setData([row]);
FakeCustomLayer.created[0]?.render?.();
expect(ctx.calls.get('arc') ?? 0).toBeGreaterThanOrEqual(1);
expect(ctx.calls.get('rect') ?? 0).toBe(0);
layer.destroy();
});
});
/**
* 这两条守的是「炫」的那部分:外光雾 + 三明治描边,以及选中那一下不加戏。
*
* 它们都属于「画得好看」而不是「算得对」,没有数值可以断言,只能钉住笔数:
* 少画一笔就退回一个平淡的描边圈,多画一笔则会在几十片同屏时拖慢拖动。
*/
describe('黑点图形 · 光效分档', () => {
/** 20×20 的域:z13 下等效半径远超满配门槛,走完整特效 */
function bigDomain(): { mask: Uint8Array; row: HotspotRow } {
const mask = new Uint8Array(GRID.w * GRID.h);
for (let y = 0; y < 20; y += 1) {
for (let x = 0; x < 20; x += 1) mask[y * GRID.w + x] = 1;
}
const row = {
id: 0,
domainId: 1,
lng: 114.0015,
lat: 22.5015,
count: 420,
cells: 400,
radiusM: 565,
cellMeters: 100,
} as unknown as HotspotRow;
return { mask, row };
}
it('大片走满配:三档光雾 + 暗底 + 主边 + 高光,且光雾只留在轮廓外', () => {
const layer = new HotspotLayer(fakeMap());
const { mask, row } = bigDomain();
layer.setData([row], GRID, mask);
FakeCustomLayer.created[0]?.render?.();
// 内侧发光已去掉:不再需要把轮廓 clip 一次再描宽线
expect(ctx.calls.get('clip') ?? 0).toBe(0);
// 光雾是描宽线,线宽一半在内 —— 必须用 destination-out 把里面那半擦掉。
// 这一笔是「内圈光晕」这个问题的根治处:只靠 0.14 的填充压不住 0.34 的光。
expect(ctx.calls.get('erase') ?? 0).toBeGreaterThanOrEqual(1);
// 三档光雾 + 暗底 + 主边 + 高光 = 6 笔
expect(ctx.calls.get('stroke') ?? 0).toBeGreaterThanOrEqual(6);
// 一次擦 + 一次填充
expect(ctx.calls.get('fill') ?? 0).toBeGreaterThanOrEqual(2);
layer.destroy();
});
it('选中只在本层加粗描边,不额外建画布、不起动画', () => {
const layer = new HotspotLayer(fakeMap());
const { mask, row } = bigDomain();
layer.setData([row], GRID, mask);
FakeCustomLayer.created[0]?.render?.();
const idle = ctx.calls.get('stroke') ?? 0;
ctx.calls.clear();
layer.setOptions({ active: 0 });
FakeCustomLayer.created[0]?.render?.();
// 同一批笔画,选中只是把线宽与浓度提一档 —— 不再有扩散光圈那几笔
expect(ctx.calls.get('stroke') ?? 0).toBe(idle);
// 全程只有一张画布:每片都配一张全屏 backing store 在 DPR 2 下是几十兆
expect(FakeCustomLayer.created).toHaveLength(1);
layer.destroy();
});
});