focusView.test.ts 14.4 KB
import { describe, expect, it } from 'vitest';
import { pxPerDegLat, pxPerDegLng } from '../aggregate/grid';
import {
  CASE_FOCUS_MS,
  CASE_FOCUS_ZOOM,
  centerForPointAt,
  FRAME_PAD_PX,
  frameBounds,
  hotspotCameraIntent,
  RECENTER_MS,
  SPOTLIGHT_MAX_ZOOM,
  viewShowsBounds,
  visibleSpot,
  type GeoBounds,
} from './focusView';

const CANVAS = { width: 1600, height: 900 };
/** 深圳尺度下的一条记录 */
const POINT = { lng: 114.0579, lat: 22.5431 };

describe('visibleSpot · 定位目标点', () => {
  it('两侧没有遮挡时就是画布正中', () => {
    expect(visibleSpot(CANVAS, { left: 0, right: 0 })).toEqual({ x: 800, y: 450 });
  });

  it('右侧被抽屉盖住时往左挪,挪的量是遮挡宽度的一半', () => {
    const spot = visibleSpot(CANVAS, { left: 0, right: 736 });
    expect(spot.x).toBeCloseTo(800 - 736 / 2, 6);
    // 垂直方向不受抽屉影响
    expect(spot.y).toBe(450);
  });

  it('左右同时被盖(清单 + 抽屉)时取两段之间那一段的中点', () => {
    const spot = visibleSpot(CANVAS, { left: 380, right: 736 });
    expect(spot.x).toBeCloseTo((380 + (1600 - 736)) / 2, 6);
  });

  it('遮挡宽到没有可见带时不做负数:退回到左边缘,定位仍然执行', () => {
    const spot = visibleSpot({ width: 600, height: 800 }, { left: 300, right: 500 });
    expect(spot.x).toBe(300);
  });
});

describe('centerForPointAt · 把点放到指定屏幕位置', () => {
  it('目标位置就是画布正中时,中心等于该点本身', () => {
    const [lng, lat] = centerForPointAt(POINT, CANVAS, CASE_FOCUS_ZOOM, {
      x: CANVAS.width / 2,
      y: CANVAS.height / 2,
    });
    expect(lng).toBeCloseTo(POINT.lng, 12);
    expect(lat).toBeCloseTo(POINT.lat, 12);
  });

  it('点要落在中心左边时,中心落在点的东边', () => {
    const spot = visibleSpot(CANVAS, { left: 0, right: 736 });
    const [lng] = centerForPointAt(POINT, CANVAS, CASE_FOCUS_ZOOM, spot);
    // 屏幕 x 随经度增大:点被推到左边,说明中心比它更靠东
    expect(lng).toBeGreaterThan(POINT.lng);
    const shiftPx = CANVAS.width / 2 - spot.x;
    expect(lng - POINT.lng).toBeCloseTo(shiftPx / pxPerDegLng(CASE_FOCUS_ZOOM), 10);
  });

  it('点要落在中心上边时,中心落在点的南边', () => {
    const [_, lat] = centerForPointAt(POINT, CANVAS, CASE_FOCUS_ZOOM, {
      x: CANVAS.width / 2,
      y: 200,
    });
    // 屏幕 y 随纬度减小:点在上方,中心的纬度必须更低
    expect(lat).toBeLessThan(POINT.lat);
    expect(POINT.lat - lat).toBeCloseTo(250 / pxPerDegLat(CASE_FOCUS_ZOOM, POINT.lat), 10);
  });

  it('像素偏移与度数偏移成正比(同一级别下线性)', () => {
    const a = centerForPointAt(POINT, CANVAS, CASE_FOCUS_ZOOM, { x: 700, y: 450 });
    const b = centerForPointAt(POINT, CANVAS, CASE_FOCUS_ZOOM, { x: 600, y: 450 });
    expect(b[0] - a[0]).toBeCloseTo(a[0] - POINT.lng, 10);
  });

  it('放大一级,同样的像素偏移只挪一半的度数', () => {
    const spot = { x: 600, y: 450 };
    const z16 = centerForPointAt(POINT, CANVAS, 16, spot);
    const z17 = centerForPointAt(POINT, CANVAS, 17, spot);
    expect(Math.abs(z17[0] - POINT.lng)).toBeCloseTo(Math.abs(z16[0] - POINT.lng) / 2, 10);
  });
});

/**
 * 聚焦展示的取景:把「一屏那批形状」的并集范围搬到屏幕上。
 *
 * 钉住的是两条读数规则,不是画面 —— 形状画多大由图层管,但取景错了就什么都看不见:
 *   1) 这批形状必须完整落在左右浮层之间那一段里,并且落在得下的最深一档;
 *   2) 中心落在可见段的中点,而不是画布正中 —— 否则聚焦出来的东西正好躲在抽屉底下,
 *      表现为「点了没反应」。
 */

const MID = { lng: 114.05, lat: 22.54 };
const NO_COVER = { left: 0, right: 0 };
/** 可见带扣掉四周呼吸位之后还能塞东西的宽高 */
const USABLE_W = CANVAS.width - FRAME_PAD_PX * 2;
const USABLE_H = CANVAS.height - FRAME_PAD_PX * 2;

/** 以某点为中心、按度数跨度围一块范围(纬度方向的换算跟墨卡托一致,深一级的每度像素翻倍) */
function boxAround(spanLng: number, spanLat: number, mid = MID): GeoBounds {
  return {
    minLng: mid.lng - spanLng / 2,
    maxLng: mid.lng + spanLng / 2,
    minLat: mid.lat - spanLat / 2,
    maxLat: mid.lat + spanLat / 2,
  };
}

/** 这一屏上这块范围占多少像素(线性近似:聚焦范围最多几公里,误差远小于一像素) */
function spanPx(bounds: GeoBounds, zoom: number): { w: number; h: number } {
  return {
    w: (bounds.maxLng - bounds.minLng) * pxPerDegLng(zoom),
    h: (bounds.maxLat - bounds.minLat) * pxPerDegLat(zoom, (bounds.minLat + bounds.maxLat) / 2),
  };
}

describe('frameBounds · 把一屏聚焦的形状取景到可见带', () => {
  it('东西长的一屏按宽度定深浅,南北长的那一屏按高度定深浅', () => {
    const wide = boxAround(0.06, 0.001);
    const tall = boxAround(0.0005, 0.03);
    const w = spanPx(wide, frameBounds(wide, CANVAS, NO_COVER).zoom);
    const t = spanPx(tall, frameBounds(tall, CANVAS, NO_COVER).zoom);
    // 限制它的那一头正好顶到可见带边缘,另一头还有大量余量 —— 反过来就是「按错边取景」,
    // 一条南北向的路会被拉到屏幕外
    expect(w.w).toBeGreaterThan(USABLE_W - 2);
    expect(w.h).toBeLessThan(USABLE_H / 4);
    expect(t.h).toBeGreaterThan(USABLE_H - 2);
    expect(t.w).toBeLessThan(USABLE_W / 4);
  });

  it('塞得下,而且已经是最深的一档(再深一级就露到屏幕外)', () => {
    for (const bounds of [
      boxAround(0.02, 0.004),
      boxAround(0.003, 0.025),
      boxAround(0.008, 0.008),
    ]) {
      const { zoom } = frameBounds(bounds, CANVAS, NO_COVER);
      const fit = spanPx(bounds, zoom);
      expect(fit.w).toBeLessThanOrEqual(USABLE_W + 1);
      expect(fit.h).toBeLessThanOrEqual(USABLE_H + 1);
      // 未触顶时不能再深:深 0.01 级必有一头顶出可见带
      if (zoom < SPOTLIGHT_MAX_ZOOM - 1e-6) {
        const deeper = spanPx(bounds, zoom + 0.01);
        expect(deeper.w > USABLE_W || deeper.h > USABLE_H).toBe(true);
      }
    }
  });

  it('范围小到一个路口时封顶在聚焦上限,不停在 z18 以上(那只剩两栋楼,看不出是哪儿)', () => {
    const { zoom } = frameBounds(boxAround(0.0002, 0.0002), CANVAS, NO_COVER);
    expect(zoom).toBe(SPOTLIGHT_MAX_ZOOM);
  });

  it('左右浮层都盖住时可见带变窄,取景自动退一档,屏上还是完整一块', () => {
    const bounds = boxAround(0.02, 0.006);
    const open = frameBounds(bounds, CANVAS, NO_COVER);
    const docked = frameBounds(bounds, CANVAS, { left: 380, right: 736 });
    expect(docked.zoom).toBeLessThan(open.zoom);
    expect(spanPx(bounds, docked.zoom).w).toBeLessThanOrEqual(
      CANVAS.width - 380 - 736 - FRAME_PAD_PX * 2 + 1,
    );
  });

  it('中心落在左右浮层之间那一段的中点,不在画布正中(不然形状正好躲在抽屉底下)', () => {
    const bounds = boxAround(0.02, 0.006);
    const midLng = (bounds.minLng + bounds.maxLng) / 2;
    const open = frameBounds(bounds, CANVAS, NO_COVER);
    // 无遮挡时可见段就是整屏,中心与该范围的经度中心重合
    expect(open.center[0]).toBeCloseTo(midLng, 10);
    expect(open.center[1]).toBeCloseTo((bounds.minLat + bounds.maxLat) / 2, 10);

    const rightCover = frameBounds(bounds, CANVAS, { left: 0, right: 736 });
    // 目标点被推到左边 → 中心必须落在范围东边,才能把形状让到抽屉左侧那一段
    expect(rightCover.center[0]).toBeGreaterThan(midLng);
    const shiftPx = CANVAS.width / 2 - visibleSpot(CANVAS, { left: 0, right: 736 }).x;
    expect(rightCover.center[0] - midLng).toBeCloseTo(shiftPx / pxPerDegLng(rightCover.zoom), 9);
  });

  it('缩放上下限由调用方给(地图本身不许越界,聚焦上限只在其内再收)', () => {
    const tiny = boxAround(0.0002, 0.0002);
    // 上限压到 12:即使形状只有一格也不许越过地图允许的深度
    expect(frameBounds(tiny, CANVAS, NO_COVER, [9, 12]).zoom).toBe(12);
    // 下限抬到 13:宽范围也不许退到全市视角
    expect(frameBounds(boxAround(0.6, 0.4), CANVAS, NO_COVER, [13, 18]).zoom).toBe(13);
    // 不给上下限时用地图配置那两级,聚焦上限仍在其内起效
    expect(frameBounds(tiny, CANVAS, NO_COVER, [9, 18]).zoom).toBe(SPOTLIGHT_MAX_ZOOM);
  });

  it('同样的地面范围,纬度越高越往浅退(墨卡托把南北拉长,按同一档放会把路顶出屏幕)', () => {
    // 南北 3 公里:度数跨度与纬度无关(1° 纬线定长),但它在屏幕上占的像素随纬度增大
    const spanLat = 3000 / 110700;
    const shenzhen = frameBounds(
      boxAround(0.0004, spanLat, { lng: 114.05, lat: 22.54 }),
      CANVAS,
      NO_COVER,
    );
    const harbin = frameBounds(
      boxAround(0.0004, spanLat, { lng: 126.62, lat: 45.75 }),
      CANVAS,
      NO_COVER,
    );
    expect(harbin.zoom).toBeLessThan(shenzhen.zoom);
    expect(shenzhen.zoom - harbin.zoom).toBeCloseTo(
      Math.log2(Math.cos((22.54 * Math.PI) / 180) / Math.cos((45.75 * Math.PI) / 180)),
      3,
    );
    expect(
      spanPx(boxAround(0.0004, spanLat, { lng: 126.62, lat: 45.75 }), harbin.zoom).h,
    ).toBeLessThanOrEqual(USABLE_H + 1);
  });
});

/**
 * 两类相机移动的时长关系。
 *
 * 钉的是「不许退回默认那一拍」:从某个路口拉回全市是全站跨度最大的一次移动,
 * 高德默认时长要上千毫秒,人早就去点别的控件了(界面反馈这是被提出来的问题)。
 * 也不许压成 0 —— 瞬移时底图瓦片来不及换,会闪一帧糊图,理由见两个常量上面的注释。
 */
describe('取景时长 · 定位与复位的快慢关系', () => {
  it('定位比复位快:连着点清单行要的是即时,复位要的是一眼接得住', () => {
    expect(CASE_FOCUS_MS).toBeLessThan(RECENTER_MS);
  });

  it('两档都远短于默认那一拍,又都长到眼睛来得及跟', () => {
    expect(CASE_FOCUS_MS).toBeGreaterThanOrEqual(160);
    expect(RECENTER_MS).toBeGreaterThanOrEqual(240);
    expect(RECENTER_MS).toBeLessThanOrEqual(400);
  });
});

/**
 * 点黑点之后镜头要不要动、怎么动。
 *
 * 钉的是四件事,每一件都对应一次真实的坏体验:
 *   1. 切换黑点必须重新取景 —— 用户原话「切换黑点,我想让该黑点出现自动调整后合适的视口」;
 *   2. 切换时允许缩小 —— 不允的话,从 z16 的路口切到两公里外的大片,屏上只剩几个格子;
 *   3. 同一片、已经够大 → 绝不动镜头 —— 早先「点一下地图画面就跳」的投诉来源;
 *   4. 同一片、比合适视口更小 → 放大,但绝不往回缩(往回缩只发生在切换那一条)。
 */
describe('hotspotCameraIntent · 黑点取景的四种动因', () => {
  const base = {
    switched: false,
    onScreen: true,
    currentZoom: 13,
    fitZoom: 15,
  };

  it('切到另一片:一律重新取景,哪怕当前已经比合适视口更近', () => {
    expect(hotspotCameraIntent({ ...base, switched: true, currentZoom: 17 })).toBe('fit-switch');
    expect(hotspotCameraIntent({ ...base, switched: true, onScreen: false })).toBe('fit-switch');
  });

  it('同一片但质心在屏幕外:拉回视野', () => {
    expect(hotspotCameraIntent({ ...base, onScreen: false })).toBe('fit-recover');
  });

  it('同一片、比合适视口更小:放大到位(城市级别点一个小黑点,不放大等于没反应)', () => {
    expect(hotspotCameraIntent({ ...base, currentZoom: 12, fitZoom: 16 })).toBe('fit-zoom-in');
  });

  it('同一片、已经与合适视口持平或更近:不动,绝不往回缩', () => {
    expect(hotspotCameraIntent({ ...base, currentZoom: 15, fitZoom: 15 })).toBe('hold');
    expect(hotspotCameraIntent({ ...base, currentZoom: 16.5, fitZoom: 15 })).toBe('hold');
    // 浮点误差不触发第二次取景
    expect(hotspotCameraIntent({ ...base, currentZoom: 14.995, fitZoom: 15 })).toBe('hold');
  });
});

/**
 * 收窄取景的判据:这一片在视野里吗。
 *
 * 钉的是「不看你缩放多少,只看你装不装得下」这一条 —— 旧的判据是
 * 「当前缩放比目标更近就跳过」,于是从市域(z11)点某一类(目标 z10.4,因为右侧
 * 面板占掉一截可用宽度),镜头一动不动,而这一类的点在屏外。
 */
describe('viewShowsBounds · 这一片在不在视野里', () => {
  /**
   * 一个假投影:以 (114.06, 22.54) 为屏幕中心的等距投影。
   * 每度经度 4000px、每度纬度 4000px —— 只关心几何关系,不必还原墨卡托。
   */
  const CENTER = { lng: 114.06, lat: 22.54 };
  const PX_PER_DEG = 4000;
  const project = (lng: number, lat: number) => ({
    x: CANVAS.width / 2 + (lng - CENTER.lng) * PX_PER_DEG,
    y: CANVAS.height / 2 - (lat - CENTER.lat) * PX_PER_DEG,
  });
  const NO_COVER = { left: 0, right: 0 };
  /** 一块以中心对称、给定半宽半高的范围(度) */
  const around = (halfDeg: number): GeoBounds => ({
    minLng: CENTER.lng - halfDeg,
    maxLng: CENTER.lng + halfDeg,
    minLat: CENTER.lat - halfDeg,
    maxLat: CENTER.lat + halfDeg,
  });

  it('整块都在可见区里:不再动镜头', () => {
    // 0.05° ≈ 200px,远小于半屏
    expect(viewShowsBounds(project, around(0.05), NO_COVER, CANVAS, 12)).toBe(true);
  });

  it('有一角出屏:必须动 —— 哪怕只是宽出一点点', () => {
    // 0.3° = 1200px > 800-12,左右都出去了
    expect(viewShowsBounds(project, around(0.3), NO_COVER, CANVAS, 12)).toBe(false);
  });

  it('点在屏内、但被右侧抽屉盖住:也算没装下', () => {
    // 0.2° = 800px:右侧那个角正好落在 800 + 800 = 1600 的边线上
    const cover = { left: 0, right: 400 };
    expect(viewShowsBounds(project, around(0.2), cover, CANVAS, 12)).toBe(false);
  });

  it('单点(零面积范围)落在可见区里就算装得下', () => {
    const dot: GeoBounds = { minLng: CENTER.lng, maxLng: CENTER.lng, minLat: CENTER.lat, maxLat: CENTER.lat };
    expect(viewShowsBounds(project, dot, NO_COVER, CANVAS, 12)).toBe(true);
  });

  it('浮层把可用区压没了:一律不算装下(交给 frameBounds 退到边界值)', () => {
    const cover = { left: 800, right: 800 };
    expect(viewShowsBounds(project, around(0.0001), cover, CANVAS, 12)).toBe(false);
  });
});