BucketHoverLayer.ts
9.22 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
import { mercY, type Overlay, type PaintEnv } from '@hm/map-overlay';
import { defineAppOverlay } from './appOverlay';
import type { BucketHit } from './ClusterLayer';
import { mapGesture } from './gesture';
import { PALETTE } from './palette';
import type { Basemap } from './provider/types';
/**
* 数字桶的悬浮反馈:鼠标压住哪个圆盘,那一圈就轻轻亮一下、向外吐两道扩散环。
*
* 为什么单独开一层,而不是把动画塞进 `ClusterLayer`:
* - 聚合层是「视图没变就整帧跳过」的省电层,一屏几百个桶。让它为了一颗被指着的桶
* 每 33ms 重画整屏,等于把那条帧预算拆了;独立层 `canPaint` 只在有 hover 时为真,
* 平时连画布都不建,一分钱不花。
* - 盘半径、位置都从命中结果拿(`BucketHit.radius` 就是那一帧画出来的那一圈),
* 不在这一层另算文字宽度 —— 另算就会和内层长相漂移,环画得比盘大一圈或小一圈。
* - 动画层自己会动,走内核的 `animated` 那一档;地图没动时只擦上一帧那一小块
* (`env.clearedAll === false`),绝不整屏 clearRect。
*
* 视觉克制:这是「这里能点」的回应,不是第二套数据编码。只用品牌靛那一支,
* 不碰热力的红橙端;盘面只提亮 10%,数字仍读得出。
*
* 出帧节律抄 `CasePinLayer` 的呼吸光圈(33ms 下限 + 自管 rAF + 局部擦除),
* 两层行为一致,调其中一处时记得另一处也要一起看。
*/
/** 一圈扩散的时长。两圈错相,看起来是连续的呼吸而不是一下一下的闪 */
const RING_PERIOD_MS = 1200;
/** 刷新下限。和 CasePinLayer 的 HALO_FRAME_MS 同值:满帧重画一个光圈没有意义 */
const RING_FRAME_MS = 33;
/** 同时在场几圈 */
const RING_COUNT = 2;
/** 从盘沿往外扩多少像素 */
const RING_EXPAND = 13;
/** 环色按相位查表,动画帧里不生成字符串对象 */
const RING_STEPS = 16;
const RING_ALPHA = 0.62;
/** 靛墨 rgb 分量(PALETTE.primary = #4653A2) */
const RING_RGB = '70,83,162';
const RING_STROKES: string[] = Array.from({ length: RING_STEPS }, (_, i) => {
const alpha = RING_ALPHA * (1 - i / (RING_STEPS - 1));
return `rgba(${RING_RGB},${alpha.toFixed(3)})`;
});
/** 盘沿那一圈实线:让「就是这个盘」当场定住,不必等扩散环转起来 */
const RIM_WIDTH = 1.8;
/** 盘面提亮。10% 只是让玻璃盘从背景里浮出来,不盖数字 */
const PLATE_GLOW = 'rgba(255,255,255,0.10)';
export class BucketHoverLayer {
private overlay: Overlay | null = null;
private hover: BucketHit | null = null;
/** 当前指着的桶(gx/gy 是桶的身份);同一个桶上鼠标挪几像素不该把动画重启 */
private key = '';
private startAt = 0;
private lastFrame = 0;
private raf = 0;
/** 上一帧真正落笔的屏幕矩形,下一帧只擦这一块 */
private dirty = new Float64Array(4);
private hasDirty = false;
constructor(private readonly map: Basemap) {}
get active(): boolean {
return this.hover !== null;
}
/**
* 换桶。传 null 即撤掉反馈(移到空白、移到别的图层、或拖动地图期间)。
*
* 身份用 gx/gy 而不是经纬度:缩放时同一个桶的屏幕位置会变,但动画不该从头开始;
* 反过来,两个挨得很近的桶虽然屏幕只差几像素,也该各自重新吐一圈。
*/
setHover(hit: BucketHit | null): void {
// 手势期间每个 mapmove / zoomchange 都会来清一次,没 hover 时直接走人,
// 别反复调 release(虽然它自己有宽度判断,热路径上也不该白跑一趟)
if (!hit && !this.hover) return;
const key = hit ? `${hit.gx},${hit.gy}` : '';
if (!hit) {
this.stop();
this.hover = null;
this.key = '';
this.hasDirty = false;
// release 一次做两件事:画布当场清空(残影不留),全屏 backing store 交还
this.overlay?.release();
return;
}
if (this.hover && this.key === key) {
// 同一个桶:半径随缩放更新,动画相位照旧
this.hover = hit;
return;
}
this.key = key;
this.hover = hit;
this.startAt = performance.now();
this.lastFrame = 0;
this.hasDirty = false;
this.ensure();
this.overlay?.setTotal(1);
// lastFrame 归零保证 rAF 第一拍一定落笔,这里不再同步画一遍:
// 重复落笔的那一帧马上会被下一拍擦掉,白付一次全屏取数
this.overlay?.invalidate();
this.start();
}
/** 地图在动时由外部点名。没有 hover 时 canPaint 会挡掉,不会欠下一帧脏 */
invalidate(): void {
this.overlay?.invalidate();
}
destroy(): void {
this.stop();
this.hover = null;
this.overlay?.destroy();
this.overlay = null;
}
private ensure(): void {
if (this.overlay) return;
this.overlay = defineAppOverlay(this.map, {
runtime: this.map.overlayRuntime,
id: 'bucketHover',
name: 'BucketHoverLayer · 数字桶悬浮反馈层',
// 压在聚合数字(130)之上、黑点层(140)之下:hover 到桶时不会同时命中黑点,
// 这一层只是给那一颗数字加一圈回应,不该越过黑点结论去抢注意力
zIndex: 135,
zooms: [3, 20],
animated: true,
extras: () => ({
built: true,
visible: this.hover !== null,
note: this.hover ? '指着哪个桶,哪一圈在呼吸' : '无 hover:不建画布',
}),
/**
* 地图在动时整层不接出帧。
*
* 这一层是 `animated`:视图每帧变化都会走 `beginAnimated` 的整屏 clearRect
* (投影一变,上一帧那一小圈的屏幕位置就作废,只能整屏擦)。缩放时底图逐帧回调,
* 于是「一个直径二十几像素的呼吸圈」能让一张 DPR2 全屏画布每帧重传整张纹理 ——
* 这正是缩放变卡那一下的由来。
*
* 而且滚轮缩放根本不发 mousemove,光靠 hover effect 那头清不掉;所以判据直接落在这里,
* 与 MapStage 手势事件那一头的 `setHover(null)` 互为双保险。
* 圈本来就是「停在这儿、指给我看」的东西,动图的时候没人读它,撤掉没有信息损失。
*/
canPaint: () => this.hover !== null && !mapGesture.active(),
paint: (env) => this.paint(env),
});
}
private start(): void {
if (this.raf) return;
const step = () => {
this.raf = 0;
if (!this.hover) return;
// 先在时间上判一次:没到点连地图都不问(取中心与尺寸比擦一小块还贵)
if (performance.now() - this.lastFrame >= RING_FRAME_MS) this.overlay?.draw();
this.raf = requestAnimationFrame(step);
};
this.raf = requestAnimationFrame(step);
}
private stop(): void {
if (this.raf) cancelAnimationFrame(this.raf);
this.raf = 0;
}
/**
* 返回 null 表示被 33ms 下限挡住(不改遥测读数),与 CasePinLayer 同一口径。
*/
private paint(env: PaintEnv): number | null {
const now = performance.now();
// 下限闸在落笔入口,不只闸自管 rAF:底图逐帧回调和那条 rAF 都汇到这里。
// 视图真变了的那一帧不受限,否则光圈会比底图慢半拍。
if (!env.clearedAll && now - this.lastFrame < RING_FRAME_MS) return null;
this.lastFrame = now;
const { ctx, frame, width: w, height: h } = env;
const hit = this.hover;
if (!hit) return 0;
// 地图没动 → 只擦上一帧那一小块
if (!env.clearedAll && this.hasDirty) {
ctx.clearRect(this.dirty[0], this.dirty[1], this.dirty[2], this.dirty[3]);
}
this.hasDirty = false;
const x = frame.x(hit.lng);
const y = frame.y(mercY(hit.lat));
const r = Math.max(4, hit.radius);
const reach = r + RING_EXPAND + 4 + RIM_WIDTH;
if (x < -reach || y < -reach || x > w + reach || y > h + reach) return 0;
// 盘面提一点亮:玻璃盘本来就是白的,这一层只让它从热力上浮出来半档
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fillStyle = PLATE_GLOW;
ctx.fill();
// 盘沿实线:定住"就是这一颗"
ctx.beginPath();
ctx.arc(x, y, r + 1, 0, Math.PI * 2);
ctx.strokeStyle = PALETTE.primary;
ctx.lineWidth = RIM_WIDTH;
ctx.stroke();
// 两道错相的扩散环:从盘沿外一点起,越往外越淡
const phase = ((now - this.startAt) % RING_PERIOD_MS) / RING_PERIOD_MS;
ctx.lineWidth = 1.6;
for (let k = 0; k < RING_COUNT; k += 1) {
const u = (phase + k / RING_COUNT) % 1;
const rr = r + 2 + RING_EXPAND * u;
const step = Math.min(RING_STEPS - 1, Math.floor(u * RING_STEPS));
ctx.beginPath();
ctx.arc(x, y, rr, 0, Math.PI * 2);
ctx.strokeStyle = RING_STROKES[step];
ctx.stroke();
}
// 记下这一帧占的地方。按最大扩散半径算,不按当前那一圈 —— 按当前半径擦会留残影
const x0 = Math.max(0, x - reach);
const y0 = Math.max(0, y - reach);
this.dirty[0] = x0;
this.dirty[1] = y0;
this.dirty[2] = Math.min(w, x + reach) - x0;
this.dirty[3] = Math.min(h, y + reach) - y0;
this.hasDirty = true;
return 1;
}
}