canvasHeat.test.ts
13.4 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* 自绘热力层的颜色/密度契约(2026-11-25 第三十一轮:浓场回调,淡场 + 可见小核)。
*
* 权重/着色仍走 heatmap.js@2.0.5 口径:
* - 密度场 `globalAlpha = count/max`,无 gamma、无额外系数;
* - 头部补峰加 2.4 次幂主次压缩(场不变,头部出红心、中段退橙绿);
* - `palette[灰度字节]` 直取,输出 alpha = clamp(a, minOpacity, maxOpacity)。
* 笔刷拆成两笔(都走 source-over):
* - 密度场:1.8×格距、峰值 0.30、线性软边 → 邻桶叠加铺出连续橙红场但不饱和;
* - 头部补峰:0.58×格距、内 8% 软平台、强度 = 权重^2.4 → 头部桶圆心出可见红心。
*
* 这几条钉住:
* 1) 查表低/中/高三段颜色分开;
* 2) 权重是恒等(count/max),满案量=1;
* 3) 单桶的场停在冷/中段、多桶叠加才进暖端(面状场的来源),且叠加不封顶;
* 4) 补峰有软平台,满案量单点进红端,邻格中心仍落在补峰外;
* 5) 密度→色带恒等,无 gamma;
* 6) 两笔半径都随格距同比缩放、style.radius 退化为档位倍率;
* 7) 笔刷扰动确定性、零均值、核不偏移(同圆),场半径/相位在范围内抖动;
* 8) 第三十二轮:核做各向异性椭圆化 + 旋转 + 偏瓣(打破正圆),主圆心仍不动。
*/
import { describe, expect, it } from 'vitest';
import {
BLUR_FACTOR,
brushAlphaAt,
buildLut,
CORE_ASPECT_JITTER,
CORE_GAMMA,
CORE_LOBE_ALPHA,
CORE_LOBE_MIN_ALPHA,
CORE_RADIUS_SCALE,
coreWeightToAlpha,
densityToStop,
FIELD_PEAK,
FIELD_RADIUS_SCALE,
FIELD_ASPECT_JITTER,
FIELD_JITTER,
FIELD_SHIFT,
CORE_JITTER,
fieldAlphaAt,
brushVariation,
heatBrushRadii,
smoothMax,
weightToAlpha,
} from './CanvasHeatLayer';
describe('自绘热力渐变查表', () => {
const lut = buildLut();
it('256 级查表长度正确', () => {
expect(lut.length).toBe(256 * 3);
});
it('低密度冷色、高密度暖色,三段明显分开', () => {
const at = (a: number): [number, number, number] => [
lut[a * 3],
lut[a * 3 + 1],
lut[a * 3 + 2],
];
const low = at(48); // a≈0.19
const mid = at(140); // a≈0.55
const high = at(240); // a≈0.94
expect(low[2]).toBeGreaterThan(low[0]); // 冷端蓝占优
expect(mid[1]).toBeGreaterThan(mid[2]); // 中端绿/黄
expect(high[0]).toBeGreaterThan(high[2]); // 热端红占优
expect(low).not.toEqual(mid);
expect(mid).not.toEqual(high);
});
});
describe('案量 → 笔刷强度:heatmap.js 的 count/max 恒等', () => {
it('单调、夹在 [0,1],满案量=1(红心来源)', () => {
expect(weightToAlpha(0)).toBe(0);
expect(weightToAlpha(0.1)).toBeCloseTo(0.1, 6);
expect(weightToAlpha(0.5)).toBeCloseTo(0.5, 6);
expect(weightToAlpha(1)).toBe(1);
expect(weightToAlpha(2)).toBe(1); // 超上限夹紧(官方 Math.min(value,max))
expect(weightToAlpha(-1)).toBe(0);
});
it('满案量单点的核进红端(红心回归测试)', () => {
const peak = weightToAlpha(1);
expect(densityToStop(peak * 255)).toBeGreaterThan(0.95);
});
it('中低案量单点停在中段,不误烧红', () => {
expect(densityToStop(weightToAlpha(0.3) * 255)).toBeCloseTo(0.3, 6);
expect(densityToStop(weightToAlpha(0.5) * 255)).toBeCloseTo(0.5, 6);
});
});
describe('头部补峰主次压缩:场不变,只有头部桶圆心垫红', () => {
it('恒等权重做 2.4 次幂,单调、端点不越界', () => {
expect(CORE_GAMMA).toBeCloseTo(2.4, 6);
expect(coreWeightToAlpha(0)).toBe(0);
expect(coreWeightToAlpha(1)).toBe(1);
expect(coreWeightToAlpha(2)).toBe(1);
expect(coreWeightToAlpha(0.5)).toBeGreaterThan(coreWeightToAlpha(0.4));
});
it('头部红心可见、中段退到橙绿,但不至全灭', () => {
// 0.6^2.4≈0.29、0.8^2.4≈0.59、0.9^2.4≈0.77、1^2.4=1
expect(coreWeightToAlpha(0.6)).toBeGreaterThan(0.25);
expect(coreWeightToAlpha(0.6)).toBeLessThan(0.35);
expect(coreWeightToAlpha(0.8)).toBeGreaterThan(0.55);
expect(coreWeightToAlpha(0.8)).toBeLessThan(0.65);
expect(coreWeightToAlpha(0.95)).toBeGreaterThan(0.85);
expect(densityToStop(coreWeightToAlpha(1) * 255)).toBeGreaterThan(0.95);
});
it('密度场仍走恒等权重,连续场没有被主次压缩削弱', () => {
expect(weightToAlpha(0.6)).toBeCloseTo(0.6, 6);
expect(weightToAlpha(0.6)).toBeGreaterThan(coreWeightToAlpha(0.6));
});
});
describe('双尺度笔刷:密度场铺 blob + 头部补峰点睛', () => {
const sourceOver = (dst: number, src: number): number => src + dst * (1 - src);
it('单桶密度场停在冷/中段,不会自己烧出红圈', () => {
const perBrush = weightToAlpha(1) * FIELD_PEAK;
expect(fieldAlphaAt(0)).toBe(1);
expect(perBrush).toBeCloseTo(FIELD_PEAK, 6);
expect(densityToStop(perBrush * 255)).toBeLessThan(0.5);
});
it('邻桶的场明显重叠,密集区叠加进橙红但不封顶成糊红片', () => {
const neighborT = 1 / FIELD_RADIUS_SCALE; // 邻格中心 1 格 / 场半径 1.8 格
expect(neighborT).toBeGreaterThan(0);
expect(neighborT).toBeLessThan(1);
expect(fieldAlphaAt(neighborT)).toBeCloseTo(1 - neighborT, 6); // 线性软边
// 自身场 0.30 + 6 个邻桶贡献 ≈0.70,进橙红但没到饱和红端
let a = weightToAlpha(1) * FIELD_PEAK;
for (let i = 0; i < 6; i += 1) {
a = sourceOver(a, weightToAlpha(1) * FIELD_PEAK * fieldAlphaAt(neighborT));
}
expect(a).toBeGreaterThan(0.6);
expect(a).toBeLessThan(0.85);
});
it('补峰有软平台,满案量圆心饱和;邻格中心仍落在补峰外', () => {
expect(brushAlphaAt(0)).toBe(1);
expect(brushAlphaAt(BLUR_FACTOR * 0.5)).toBe(1); // 平台内饱和
expect(brushAlphaAt(BLUR_FACTOR)).toBe(1); // 平台边缘仍饱和
expect(brushAlphaAt(0.5)).toBeCloseTo((1 - 0.5) / (1 - BLUR_FACTOR), 6);
expect(brushAlphaAt(1)).toBe(0);
expect(brushAlphaAt(2)).toBe(0); // 夹紧
const coreNeighborT = FIELD_RADIUS_SCALE / CORE_RADIUS_SCALE; // 邻格中心相对补峰半径
expect(coreNeighborT).toBeGreaterThan(1);
expect(brushAlphaAt(coreNeighborT)).toBe(0);
});
it('场比格距大、补峰远小于格距,分工明确', () => {
expect(CORE_RADIUS_SCALE).toBeLessThan(0.7);
expect(FIELD_RADIUS_SCALE).toBeGreaterThan(1);
expect(FIELD_RADIUS_SCALE / CORE_RADIUS_SCALE).toBeCloseTo(1.8 / 0.58, 6);
});
});
describe('笔刷半径随实际格距缩放', () => {
it('格距翻倍时两笔半径同比翻倍', () => {
const small = heatBrushRadii(30);
const large = heatBrushRadii(60);
expect(small.core).toBeCloseTo(30 * CORE_RADIUS_SCALE, 6);
expect(small.field).toBeCloseTo(30 * FIELD_RADIUS_SCALE, 6);
expect(large.core / small.core).toBeCloseTo(2, 6);
expect(large.field / small.field).toBeCloseTo(2, 6);
});
it('style.radius 退化为档位倍率:40 是默认 80 的一半', () => {
const half = heatBrushRadii(50, 40);
expect(half.core).toBeCloseTo(50 * CORE_RADIUS_SCALE * 0.5, 6);
expect(half.field).toBeCloseTo(50 * FIELD_RADIUS_SCALE * 0.5, 6);
});
});
describe('笔刷个性扰动:打散蜂窝/棋盘感', () => {
const sample = [
brushVariation(114060000, 22620000),
brushVariation(114062000, 22622000),
brushVariation(114064000, 22624000),
brushVariation(114066000, 22626000),
];
it('同一桶永远同值,不同桶各不相同(重绘不闪烁)', () => {
expect(brushVariation(114060000, 22620000)).toEqual(sample[0]);
expect(new Set(sample.map((v) => v.core)).size).toBe(sample.length);
expect(new Set(sample.map((v) => v.field)).size).toBe(sample.length);
});
it('核/场半径扰动夹在标称范围,场心偏移不超 10%', () => {
for (let gx = 0; gx < 12; gx += 1) {
for (let gy = 0; gy < 12; gy += 1) {
const v = brushVariation(114060000 + gx * 2000, 22620000 + gy * 2000);
expect(v.core).toBeGreaterThanOrEqual(1 - CORE_JITTER);
expect(v.core).toBeLessThanOrEqual(1 + CORE_JITTER);
expect(v.field).toBeGreaterThanOrEqual(1 - FIELD_JITTER);
expect(v.field).toBeLessThanOrEqual(1 + FIELD_JITTER);
expect(Math.abs(v.fx)).toBeLessThanOrEqual(FIELD_SHIFT);
expect(Math.abs(v.fy)).toBeLessThanOrEqual(FIELD_SHIFT);
}
}
});
it('扰动是零均值:大量桶的平均半径仍等于标称倍率,不会整体放大/缩小', () => {
let core = 0;
let field = 0;
let sx = 0;
let sy = 0;
let fsx = 0;
let fsy = 0;
const n = 2000;
for (let i = 0; i < n; i += 1) {
const v = brushVariation(114000000 + i * 37, 22600000 + i * 53);
core += v.core;
field += v.field;
sx += v.sx;
sy += v.sy;
fsx += v.fsx;
fsy += v.fsy;
}
expect(core / n).toBeCloseTo(1, 1);
expect(field / n).toBeCloseTo(1, 1);
expect(sx / n).toBeCloseTo(1, 1);
expect(sy / n).toBeCloseTo(1, 1);
expect(fsx / n).toBeCloseTo(1, 1);
expect(fsy / n).toBeCloseTo(1, 1);
});
});
describe('有机核形状:椭圆化 + 旋转 + 偏瓣,打破完美正圆', () => {
it('椭圆长短轴独立扰动、场更轻,角度/偏瓣都在范围内', () => {
expect(CORE_ASPECT_JITTER).toBeGreaterThan(FIELD_ASPECT_JITTER);
for (let gx = 0; gx < 16; gx += 1) {
for (let gy = 0; gy < 16; gy += 1) {
const v = brushVariation(114060000 + gx * 1800, 22620000 + gy * 1800);
expect(v.sx).toBeGreaterThanOrEqual(1 - CORE_ASPECT_JITTER);
expect(v.sx).toBeLessThanOrEqual(1 + CORE_ASPECT_JITTER);
expect(v.sy).toBeGreaterThanOrEqual(1 - CORE_ASPECT_JITTER);
expect(v.sy).toBeLessThanOrEqual(1 + CORE_ASPECT_JITTER);
expect(v.fsx).toBeGreaterThanOrEqual(1 - FIELD_ASPECT_JITTER);
expect(v.fsx).toBeLessThanOrEqual(1 + FIELD_ASPECT_JITTER);
expect(v.fsy).toBeGreaterThanOrEqual(1 - FIELD_ASPECT_JITTER);
expect(v.fsy).toBeLessThanOrEqual(1 + FIELD_ASPECT_JITTER);
expect(v.rot).toBeGreaterThanOrEqual(0);
expect(v.rot).toBeLessThanOrEqual(Math.PI);
expect(v.lobeDist).toBeGreaterThanOrEqual(0.55);
expect(v.lobeDist).toBeLessThanOrEqual(1);
expect(v.lobeScale).toBeGreaterThanOrEqual(0.5);
expect(v.lobeScale).toBeLessThanOrEqual(0.8);
expect(v.lobeAngle).toBeGreaterThanOrEqual(0);
expect(v.lobeAngle).toBeLessThanOrEqual(Math.PI * 2);
}
}
});
it('同一桶形状永远同值,不同桶各不相同(重绘不闪烁、不整齐划一)', () => {
const a = brushVariation(114060000, 22620000);
expect(brushVariation(114060000, 22620000)).toEqual(a);
const others = [
brushVariation(114062000, 22622000),
brushVariation(114064000, 22624000),
brushVariation(114066000, 22626000),
];
expect(new Set(others.map((v) => v.rot)).size).toBe(others.length);
expect(new Set(others.map((v) => v.lobeAngle)).size).toBe(others.length);
expect(new Set(others.map((v) => `${v.sx.toFixed(4)}/${v.sy.toFixed(4)}`)).size).toBe(
others.length,
);
});
it('偏瓣只给够强的核画、强度减半:小案量不长尾巴、主峰不被盖过', () => {
expect(CORE_LOBE_MIN_ALPHA).toBeGreaterThan(0);
expect(CORE_LOBE_MIN_ALPHA).toBeLessThan(0.3);
expect(CORE_LOBE_ALPHA).toBeGreaterThan(0);
expect(CORE_LOBE_ALPHA).toBeLessThan(1);
// 满案量核 + 偏瓣仍不越出自己的格子:偏瓣最远 1×核半径,再乘 0.8×核半径,
// 合计 1.8×0.58=1.04 格;但偏瓣强度只有 0.45,不构成独立第二个红心。
expect(1.8 * CORE_RADIUS_SCALE).toBeLessThan(1.1);
});
});
describe('密度 → 色带位置:palette[a] 直取,无 gamma', () => {
it('恒等映射', () => {
expect(densityToStop(0)).toBeCloseTo(0, 6);
expect(densityToStop(255)).toBeCloseTo(1, 6);
expect(densityToStop(64)).toBeCloseTo(64 / 255, 6);
expect(densityToStop(128)).toBeCloseTo(128 / 255, 6);
expect(densityToStop(256)).toBeCloseTo(1, 6); // 夹紧
});
});
describe('归一化上限逐帧逼近', () => {
it('每帧朝目标拉近固定比例,升与降对称,最终收敛', () => {
expect(smoothMax(100, 1000)).toEqual({ value: 415, settled: false });
expect(smoothMax(1000, 100)).toEqual({ value: 685, settled: false });
expect(smoothMax(100, 101)).toEqual({ value: 101, settled: true });
let m = 100;
let settled = false;
let frames = 0;
while (!settled && frames < 60) {
const r = smoothMax(m, 300);
m = r.value;
settled = r.settled;
frames += 1;
}
expect(settled).toBe(true);
expect(m).toBe(300);
expect(frames).toBeLessThan(40);
});
});
describe('叠加合成语义', () => {
const sourceOver = (dst: number, src: number): number => src + dst * (1 - src);
it('source-over 渐近逼近 1;中案量叠多笔仍留层次', () => {
const mid = weightToAlpha(0.5);
let alpha = 0;
for (let i = 0; i < 8; i += 1) alpha = sourceOver(alpha, mid);
expect(alpha).toBeLessThan(1);
expect(alpha).toBeGreaterThan(0.99);
expect(sourceOver(alpha, mid)).toBeGreaterThan(alpha); // 单调不减
});
it('满案量核一笔即饱和,叠加仍红(不是靠叠加才出红)', () => {
const one = weightToAlpha(1);
expect(densityToStop(one * 255)).toBeGreaterThan(0.95);
const two = sourceOver(one, one);
expect(densityToStop(two * 255)).toBeGreaterThan(0.95);
});
});