buckets.ts 23.3 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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
import { mercY, textWidth, type FrameScale, type OverlayMap } from '../frame';
import { hitByRadius } from '../hit';
import { defineOverlay, type DefineOverlay, type Overlay, type OverlayRuntime } from '../overlay';
import type { GestureState } from '../gesture';

/**
 * 内置层:格子计数(一屏几百个数字,压在当前缩放的分格重心上)。
 *
 * 它是内核第一个「传数据就能用」的图层:调用方只需要给几列并列数组
 * (桶心经纬度 + 数量),建层、挂图、出帧、跳过、裁剪、命中、收起交还全由内核做。
 * 数字的口径、深浅的算法、要不要让位都是宿主的事,内核只负责**把它画出来、点得中**。
 *
 * 帧预算的三件事都落在这层里,改它时请一起保住:
 *   1) 投影每帧两次乘加(`frame.x/y`),不逐点问底图;
 *   2) 与屏幕无关的东西(墨卡托纬度、数字串、底衬半径、字色)在喂数据那一次算完,
 *      每帧只剩「求位置 + 落笔」—— 留在帧里等于把同一道题在拖动途中重算几百遍;
 *   3) 视图与本层都没变时整帧跳过;快拖那一拍整层不落笔,但命中缓存照填。
 *
 * ⚠️ 底衬半径只由文字宽度决定,不随数量放大:这类数字层的角色是「报个数」,
 *   圈一按数量放大就会把底下的图(热力、点位)挡得只剩边角,遮挡面积能差出好几倍。
 *
 * ⚠️ 深浅逐格决定,且只由这一格自己的数量决定。整层一刀切(按屏内最大值在「写数字」和
 *   「只留点」之间二选一)会被读成「数字在自己变颜色」:拖动时一个格子进出视口,
 *   就能让满屏换一种长相,同一个 1 一会儿是点、一会儿写着「1」。
 */

/** 底衬与字的长相。色串是画布上真实可用的 CSS 色,内核不解析它、也不管配色含义 */
export interface BucketsStyle {
  /** 常规字号 */
  font: string;
  /** 数字长过 `wideAfter` 位时换这一档(长数字收紧,否则顶出底衬)。不给则全程用 font */
  fontWide?: string;
  /** 底衬(圆)色。不给就不画底衬。给了 plateAt 时它优先 */
  plate?: string;
  /**
   * 底衬那一圈的高光描边(透明玻璃的边界感全靠它,见本项目的 GLASS_PLATE)。
   * 不给就不描边 —— 实心盘(黑点盘心那一类)不需要,也不该被强加一圈。
   */
  plateRim?: string;
  /** 描边宽度。默认 1px:盘只有十几 px,再粗就成了第二个底衬 */
  plateRimWidth?: number;
  /**
   * 按桶案量给底衬上色(高德式彩色圆盘)。
   * 与 ink 一样按 count 去重缓存,不在帧里现算。不给就用 plate。
   */
  plateAt?: (count: number, ceiling: number) => string;
  /**
   * 按桶案量给底衬一个半径,与文字宽度取大、再受 plateMaxRadius 约束。
   * 不给就只按文字宽度(省遮挡的老行为)。
   */
  plateRadiusOf?: (count: number, ceiling: number) => number;
  /** 底衬半径上限。防止大盘叠在热力上把色块挡没 */
  plateMaxRadius?: number;
  /** 字色:固定一支,或按(本数量, 归一化上限)自己算深浅 */
  ink: string | ((count: number, ceiling: number) => string);
  /** 字的描边色。底色是暖色时,深色字靠它才站得住 */
  halo?: string;
  haloWidth?: number;
  /**
   * 非显著桶(dots=1)的独立长相 —— 数字**照样写**,只是换一套退到背景的轻量样式。
   *
   * 为什么需要它:只把彩色盘调淡一档,密集区仍是几百个盘连成"彩色地毯",热力透不出来。
   * 把 dots 桶换成「淡白小底 + 深字」,峰值盘就成了少数点睛的彩色大盘,热力露得出来,
   * 而每一个数字都还在、都读得出是多少起 —— 这是用户明确要求的"抽稀别把数字变没了"。
   *
   * 不给这套样式时,dots 桶沿用主样式、只降透明度(旧行为)。
   */
  faded?: {
    /** 底衬色。不给就不画底衬 */
    plate?: string;
    /** 字色 */
    ink: string;
    /** 底衬半径。默认只包住文字(不受 plateRadiusOf 放大) */
    radius?: number;
    halo?: string;
    haloWidth?: number;
    /** 这一档的不透明度。默认 0.82 —— 比旧的 0.5 可读,但仍退在峰值盘之后 */
    opacity?: number;
  };
  /** 底衬最小半径:只包住文字 */
  minRadius?: number;
  /** 文字宽度之外每侧再放的余量 */
  padX?: number;
  /** 关掉数字时那个定位点画多大 */
  dotRadius?: number;
  /**
   * 关掉数字时那个定位点的命中半径。默认比画出来的那一大 1.5px:
   * 「看着变小」不该连带「点着也变小」,但也不必按底衬那样宽 —— 底衬那一圈已经不在屏上了。
   */
  dotHitRadius?: number;
}

export interface BucketsData {
  /** 桶心经度(度) */
  lng: ArrayLike<number>;
  lat: ArrayLike<number>;
  /** 本桶的数量。显示与深浅都读它 */
  count: ArrayLike<number>;
  /** 格子编号。命中回传时带上,宿主才认得出点的是哪一格;没给回传 NaN */
  gx?: ArrayLike<number>;
  gy?: ArrayLike<number>;
  /**
   * 每桶不透明度覆盖(0~1)。宿主用它做「非热点区域的数字淡出」——
   * 内核不判断谁该淡,那只与业务口径有关。
   */
  alpha?: ArrayLike<number>;
  /**
   * 每桶是否整块让位(1 = 不画、也不命中)。
   * 用途是「一个位置只留一个数」:别的层在同一处报了更权威的数,这里就得收声,
   * 否则两个数并排出现,读的人只能猜哪个算数。
   */
  suppress?: ArrayLike<number>;
  /**
   * 每桶是否淡化数字(1 = 字写淡一档,但仍写、仍点得中)。
   *
   * 与 suppress 的区别:suppress 是整个消失(不画也不命中),dots 只是把那串字**写淡**,
   * 不是收起来。用途是数字太密时按局部峰值分主次:峰值格正常浓,
   * 其余淡一档 —— 既拉开主次,又不抹掉任何一个数字、更不变成一堆认不出值的黑点。
   *
   * 曾经把它做成"低案量格只留一个黑点",用户当场否决:"抽稀不要把数字变没了,
   * 这样还不如不抽"。所以这里只降透明度,绝不换掉数字本身。
   */
  dots?: ArrayLike<number>;
  /**
   * 每桶是否「只留定位点、不写数字」(1 = 退化成点)。高德 MarkerClusterer 的
   * minClusterSize 口径:单点不聚合,图上就是一个点,但点还在、仍点得中。
   * 与 showNumbers=false 的区别是那一条整层都不写字,这条只影响这几格。
   * 与 dots 的区别是 dots 仍写字(只是淡),这条一个字都不写。
   */
  plain?: ArrayLike<number>;
  /**
   * 深浅的归一化上限(这一批的)。不给就沿用上一次的值,一批都没给过时按本批最大数量。
   * 与别的层共用一个数时,这个口径必须一起喂进来,否则「字很淡、底下却还很亮」。
   */
  ceiling?: number;
}

export interface BucketsOptions {
  zIndex: number;
  /** 层身份与可读名字(遥测用)。默认 'buckets' */
  id?: string;
  name?: string;
  /**
   * 建层口。宿主要在建层那一刻接上自己的东西(遥测注册、全页统一的投影口径与出帧方式)
   * 就把 `defineAppOverlay` 那一类函数传进来;不传用内核的 `defineOverlay`。
   * 传了它,`scale / gesture / shouldTime / alwaysRender` 这几项由宿主那次装配说了算。
   */
  define?: DefineOverlay;
  /** 投影口径。宿主按底图传一份,各层共用 */
  scale?: FrameScale;
  /** 手势状态。给了才有快拖那一拍的减载 */
  gesture?: GestureState;
  shouldTime?: () => boolean;
  runtime?: OverlayRuntime;
  alwaysRender?: boolean;
  zooms?: [number, number];
  style: BucketsStyle;
  /** 数量 → 屏上那一串字。要与清单同口径就给同一个格式化函数 */
  labelOf?: (count: number) => string;
  /** 超过几位走 fontWide。默认 3 */
  wideAfter?: number;
  /** 半径之外的点击余量。默认 7 */
  hitPad?: number;
}

export interface BucketsHit {
  /** 桶在本层数组里的下标 */
  index: number;
  /** 格子编号。没喂 gx/gy 时为 NaN */
  gx: number;
  gy: number;
  count: number;
  lng: number;
  lat: number;
  /** 命中那一帧画出来的屏幕位置:浮层卡片按它摆,别再投影一遍 */
  screenX: number;
  screenY: number;
  /**
   * 命中那一帧画出来的盘半径(不含 hitPad 余量)。
   * 宿主要给「这正是图上那个圆盘」做悬浮高亮时,从这一圈往外扩才贴得住边沿;
   * 自己按文字宽度重算一遍,迟早会和内层的长相漂移。
   */
  radius: number;
}

export interface BucketsLayer {
  overlay: Overlay;
  /** 整批换掉。一屏几百个桶,逐桶更新没有意义 */
  setData(data: BucketsData): void;
  clear(): void;
  /** 只换长相与口径:底衬几何与字色会按新 style 重量一遍 */
  setStyle(style: Partial<BucketsStyle>): void;
  setShowNumbers(on: boolean): void;
  /**
   * 只换那两张逐桶的表(淡出与让位):不重量字、不换数据、不动深浅。
   *
   * 单独开这一句是因为这两张表变得比数据勤得多 —— 图层开关、别的层报上来的让位范围、
   * 分母换值都会碰到它们,而每次走一遍整批 setData 等于把几百串数字重新量一遍。
   * 没给的那一张保持原样;给空数组表示「全按默认」(alpha 全不透明、suppress 全不让位)。
   */
  setMasks(masks: {
    alpha?: ArrayLike<number>;
    suppress?: ArrayLike<number>;
    dots?: ArrayLike<number>;
    plain?: ArrayLike<number>;
  }): void;
  /**
   * 深浅的归一化上限。传 0 表示按本批桶自己的最大数量。
   * 与别的层共用一个数时,「字很淡、底下却还很亮」那种错乱才不会出来。
   */
  setCeiling(max: number): void;
  hitAt(x: number, y: number): BucketsHit | null;
  readonly bucketCount: number;
  show(): void;
  hide(): void;
  destroy(): void;
}

/** 量不到画布时(无 canvas 的环境)的一字兜底宽度。只影响底衬大小,不参与任何数量口径 */
const FALLBACK_CHAR_W = 6.2;

export function createBucketsLayer(map: OverlayMap, options: BucketsOptions): BucketsLayer {
  const opts: BucketsOptions = { wideAfter: 3, hitPad: 7, ...options };
  let style = opts.style;

  let n = 0;
  let lngDeg = new Float64Array(0);
  let latDeg = new Float64Array(0);
  /** 墨卡托纬度:与缩放无关,喂数据时算一次,每帧只剩减法与乘法 */
  let my = new Float64Array(0);
  let count = new Int32Array(0);
  let gx = new Float64Array(0);
  let gy = new Float64Array(0);
  let alpha = new Float32Array(0);
  let suppress = new Uint8Array(0);
  let dots = new Uint8Array(0);
  /** 单点桶(高德 minClusterSize 口径):只画点、不写字,但仍可命中 */
  let plain = new Uint8Array(0);
  /* 底衬那一串字、它的半径、该用哪支墨 —— 都只由桶自己的数量决定,与缩放和屏幕无关 */
  let label: string[] = [];
  let plateR = new Float32Array(0);
  /** 文字本身那一圈半径(不受彩色盘放大影响):非峰值桶的淡白小底用它 */
  let textR = new Float32Array(0);
  let plateColor: string[] = [];
  let ink: string[] = [];
  /** 0/1:用 font 还是 fontWide。与底衬半径同源,画时按档切换 */
  let wide = new Uint8Array(0);
  /** 最大的那块底衬:裁剪外扩由它推出来,不留魔法常数 */
  let maxPlateR = 0;
  let ownMax = 0;
  let ceiling = 0;
  let showNumbers = true;
  let visible = true;

  /* 屏幕坐标缓存:绘制那一帧顺手填,点击时只比距离、不再投影一遍 */
  let sx = new Float32Array(0);
  let sy = new Float32Array(0);
  let sr = new Float32Array(0);

  /** 命中半径:按这一帧画出来的形状算,别忘 suppress 已经归零 */
  function hitRadius(i: number): number {
    if (suppress[i] === 1) return 0;
    // 淡化桶仍然写着数字,按正常底衬半径命中;整层关数字 / 单点退化成点才按点算
    return showNumbers && plain[i] !== 1 ? plateR[i] : dotHitR();
  }

  const dotR = () => style.dotRadius ?? 3.5;
  const dotHitR = () => style.dotHitRadius ?? dotR() + 1.5;

  /**
   * 按当前 style 重量一遍底衬几何。
   *
   * 量字宽按标签去重:一屏几百格的数字翻来覆去也就几十种写法。
   * 半径只由文字宽度决定 —— 这是遮挡能降下来的关键。
   */
  function measureLabels(): void {
    const minR = style.minRadius ?? 9;
    const padX = style.padX ?? 3;
    label = new Array(n);
    plateR = new Float32Array(n);
    textR = new Float32Array(n);
    wide = new Uint8Array(n);
    const widths = new Map<string, number>();
    maxPlateR = minR;
    for (let i = 0; i < n; i += 1) {
      const str = (opts.labelOf ?? String)(count[i]);
      label[i] = str;
      const isWide = str.length > (opts.wideAfter ?? 3);
      wide[i] = isWide ? 1 : 0;
      const font = isWide && style.fontWide ? style.fontWide : style.font;
      let tw = widths.get(str);
      if (tw === undefined) {
        tw = textWidth(font, str) ?? str.length * FALLBACK_CHAR_W;
        widths.set(str, tw);
      }
      let r = Math.max(minR, tw / 2 + padX);
      textR[i] = r;
      // 高德式圆盘:案量越大盘越大(但受上限约束),读的是主次而不是一堆等大的小圆
      if (style.plateRadiusOf) {
        r = Math.max(r, style.plateRadiusOf(count[i], Math.max(1, ceiling || ownMax)));
      }
      if (style.plateMaxRadius !== undefined) r = Math.min(r, style.plateMaxRadius);
      plateR[i] = r;
      if (r > maxPlateR) maxPlateR = r;
    }
  }

  /** 底衬色查表:与 ink 同样按 count 去重(一屏案量种类远少于格子数) */
  function recomputePlate(): void {
    if (plateColor.length !== n) plateColor = new Array<string>(n);
    if (!style.plateAt) {
      const flat = style.plate ?? '';
      for (let i = 0; i < n; i += 1) plateColor[i] = flat;
      return;
    }
    const cap = Math.max(1, ceiling || ownMax);
    const byCount = new Map<number, string>();
    for (let i = 0; i < n; i += 1) {
      const c = count[i];
      let color = byCount.get(c);
      if (color === undefined) {
        color = style.plateAt(c, cap);
        byCount.set(c, color);
      }
      plateColor[i] = color;
    }
  }

  /**
   * 字色查表。给的是函数就按数量去重(同一个数在全屏必然同色,而一屏的数量种类远少于格子数),
   * 是常数就直接填。换上限只重填这张表,不在帧里现算几百次。
   */
  function recomputeInk(): void {
    if (ink.length !== n) ink = new Array<string>(n);
    if (typeof style.ink === 'string') {
      for (let i = 0; i < n; i += 1) ink[i] = style.ink;
      return;
    }
    const cap = Math.max(1, ceiling || ownMax);
    const byCount = new Map<number, string>();
    for (let i = 0; i < n; i += 1) {
      const c = count[i];
      let color = byCount.get(c);
      if (color === undefined) {
        color = style.ink(c, cap);
        byCount.set(c, color);
      }
      ink[i] = color;
    }
  }

  const overlay = (opts.define ?? defineOverlay)(map, {
    id: opts.id ?? 'buckets',
    name: opts.name ?? 'BucketsLayer · 格子计数层',
    zIndex: opts.zIndex,
    zooms: opts.zooms,
    alwaysRender: opts.alwaysRender,
    scale: opts.scale,
    gesture: opts.gesture,
    shouldTime: opts.shouldTime,
    runtime: opts.runtime,
    canPaint: () => visible && n > 0,
    paint(env) {
      const ctx = env.ctx;
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      // 底衬、描边宽度与描边色整层一样,出循环设一次
      ctx.lineWidth = style.haloWidth ?? 3;
      ctx.strokeStyle = style.halo ?? 'rgba(255,255,255,0.9)';
      // ctx.font 赋值要重新解析字体串:几百格各设一次就是几百次解析。
      // 只有两档,相邻格子大多同档,所以按「档变了才设」处理。
      let fontWide = -1;
      // 裁剪外扩:一格的底衬可能探出屏幕这么多,不留这一圈就会在边缘出现画到一半的数字
      const pad = maxPlateR + 8;
      let painted = 0;

      for (let i = 0; i < n; i += 1) {
        const x = env.frame.x(lngDeg[i]);
        const y = env.frame.y(my[i]);
        if (x < -pad || y < -pad || x > env.width + pad || y > env.height + pad) {
          sr[i] = 0;
          continue;
        }
        sx[i] = x;
        sy[i] = y;
        const r = hitRadius(i);
        sr[i] = r;
        // 减载帧只摘画笔,命中缓存照填:松手那一下的点击指的还是同一格,
        // 画面少画几笔不该让底下的坐标留着上一帧的过期值
        if (env.light || r === 0) continue;

        if (showNumbers && plain[i] !== 1) {
          // 所有桶都写数字。非局部峰值那一档只是换成"退到背景"的轻量样式:
          // 彩色盘只留给少数峰值格,其余用淡白小底 + 深字 —— 热力透得出来,
          // 数字一个不少。用户明确否决过"抽稀把数字变没了、只剩一堆点"。
          const faded = dots[i] === 1 ? style.faded : undefined;
          ctx.globalAlpha = faded ? alpha[i] * (faded.opacity ?? 0.82) : alpha[i];
          if (faded) {
            // 轻量档:底衬只包住文字(或按 faded.radius),不再按案量放大成大盘
            const fr = faded.radius ? Math.max(textR[i], faded.radius) : textR[i];
            if (faded.plate) {
              ctx.beginPath();
              ctx.arc(x, y, fr, 0, Math.PI * 2);
              ctx.fillStyle = faded.plate;
              ctx.fill();
            }
            if (faded.halo) {
              ctx.lineWidth = faded.haloWidth ?? style.haloWidth ?? 3;
              ctx.strokeStyle = faded.halo;
              ctx.strokeText(label[i], x, y + 0.5);
              // 下一格若回到峰值档,描边设置要还原(循环外只设了一次主档)
              ctx.lineWidth = style.haloWidth ?? 3;
              ctx.strokeStyle = style.halo ?? 'rgba(255,255,255,0.9)';
            }
            const fw = wide[i];
            if (fw !== fontWide) {
              ctx.font = fw && style.fontWide ? style.fontWide : style.font;
              fontWide = fw;
            }
            ctx.fillStyle = faded.ink;
            ctx.fillText(label[i], x, y + 0.5);
          } else {
            if (style.plate || style.plateAt) {
              ctx.beginPath();
              ctx.arc(x, y, plateR[i], 0, Math.PI * 2);
              ctx.fillStyle = plateColor[i];
              ctx.fill();
              // 透明玻璃靠的是这一圈亮边,不是盘本身的浓度。描完要还原字描边那两档:
              // 循环外只设过一次主档,这里改了不还原,下一格的白边会跟着盘的宽度走。
              // 淡档(faded)不描边:那一档的存在理由就是退到背景,加一圈亮边等于把它拎回来。
              if (style.plateRim) {
                ctx.lineWidth = style.plateRimWidth ?? 1;
                ctx.strokeStyle = style.plateRim;
                ctx.stroke();
                ctx.lineWidth = style.haloWidth ?? 3;
                ctx.strokeStyle = style.halo ?? 'rgba(255,255,255,0.9)';
              }
            }
            const w = wide[i];
            if (w !== fontWide) {
              ctx.font = w && style.fontWide ? style.fontWide : style.font;
              fontWide = w;
            }
            const text = label[i];
            // 先描白边再填字:底色是暖色时,深色字靠白描边才立得住
            if (style.halo) ctx.strokeText(text, x, y + 0.5);
            ctx.fillStyle = ink[i];
            ctx.fillText(text, x, y + 0.5);
          }
        } else {
          // 整层关掉数字时才退化成定位点;点色仍按本格的墨走,位置照样点得中
          ctx.globalAlpha = alpha[i];
          ctx.beginPath();
          ctx.arc(x, y, dotR(), 0, Math.PI * 2);
          ctx.fillStyle = ink[i];
          ctx.fill();
        }
        ctx.globalAlpha = 1;
        painted += 1;
      }
      return painted;
    },
  });

  const layer: BucketsLayer = {
    overlay,
    setData(data) {
      n = data.count.length;
      if (data.ceiling !== undefined) ceiling = data.ceiling;
      count = Int32Array.from(data.count);
      lngDeg = Float64Array.from(data.lng);
      latDeg = Float64Array.from(data.lat);
      my = new Float64Array(n);
      gx = data.gx ? Float64Array.from(data.gx) : new Float64Array(n).fill(Number.NaN);
      gy = data.gy ? Float64Array.from(data.gy) : new Float64Array(n).fill(Number.NaN);
      alpha = data.alpha ? Float32Array.from(data.alpha) : new Float32Array(n).fill(1);
      suppress = data.suppress ? Uint8Array.from(data.suppress) : new Uint8Array(n);
      dots = data.dots ? Uint8Array.from(data.dots) : new Uint8Array(n);
      plain = data.plain ? Uint8Array.from(data.plain) : new Uint8Array(n);
      sx = new Float32Array(n);
      sy = new Float32Array(n);
      sr = new Float32Array(n);
      ownMax = 0;
      for (let i = 0; i < n; i += 1) {
        my[i] = mercY(latDeg[i]);
        if (count[i] > ownMax) ownMax = count[i];
      }
      measureLabels();
      recomputeInk();
      recomputePlate();
      // 先点名(第一次有数据就在这一步把层建起来),再报数据量:setTotal 要写在建好的那块画布上
      overlay.invalidate();
      overlay.setTotal(n);
    },
    clear() {
      layer.setData({ lng: [], lat: [], count: [] });
    },
    setStyle(patch) {
      style = { ...style, ...patch };
      const restyle =
        patch.font || patch.fontWide || patch.minRadius !== undefined ||
        patch.padX !== undefined || patch.plateRadiusOf !== undefined ||
        patch.plateMaxRadius !== undefined;
      if (restyle) measureLabels();
      if (patch.ink !== undefined) recomputeInk();
      if (patch.plate !== undefined || patch.plateAt !== undefined) recomputePlate();
      overlay.invalidate();
    },
    setShowNumbers(on) {
      showNumbers = on;
      overlay.invalidate();
    },
    setMasks(masks) {
      if (masks.alpha) alpha = Float32Array.from(masks.alpha);
      if (masks.suppress) suppress = Uint8Array.from(masks.suppress);
      if (masks.dots) dots = Uint8Array.from(masks.dots);
      if (masks.plain) plain = Uint8Array.from(masks.plain);
      overlay.invalidate();
    },
    setCeiling(max) {
      ceiling = max > 0 ? max : 0;
      recomputeInk();
      // 彩色圆盘的色与半径都按 ceiling 归一,分母变了要跟着重算
      recomputePlate();
      if (style.plateRadiusOf) measureLabels();
      overlay.invalidate();
    },
    hitAt(x, y) {
      // 收起时屏幕坐标缓存还是上一帧的:不挡掉就会出现「图上什么都没有、点一下却弹出卡片」
      if (!visible) return null;
      const best = hitByRadius({ sx, sy, r: sr }, x, y, { pad: opts.hitPad, weight: count });
      if (best < 0) return null;
      return {
        index: best,
        gx: gx[best],
        gy: gy[best],
        count: count[best],
        lng: lngDeg[best],
        lat: latDeg[best],
        screenX: sx[best],
        screenY: sy[best],
        radius: sr[best],
      };
    },
    get bucketCount() {
      return n;
    },
    show() {
      visible = true;
      overlay.show();
    },
    hide() {
      visible = false;
      overlay.hide();
    },
    destroy() {
      overlay.destroy();
    },
  };
  return layer;
}