fix(image-annotate): v1.8.2 选择框控制点尺寸固定为屏幕像素

现象:选中图形时,控制点巨大(约 100px 的发光方块),大小随图片尺寸变化、与缩放无关。

根因:控制点、旋转手柄、虚线间距、命中半径全部写成 5.5 / S.zoom。
但 S.zoom 是「相对 fitW 的倍数」,不是「图片像素→屏幕像素」的比例。叠加 overlay
画布按 viewScale 拉伸后,真实屏幕尺寸 = 11 * fitW / imgW —— 只跟图片尺寸走:
小图被适应窗口放大 5~10 倍时膨胀到 100px,大图缩到 4px 看不见。
zoomStep 的 anchor 换算是另一个语义(相对 fitW 单位),保持 / S.zoom 不动。

顺带发现第二个问题:overlay 画布的像素缓冲等于图片尺寸,小图放大后 UI 叠层糊成光斑。

修法:
- 新增 plugin/editor/scale.js(零依赖叶子模块)
  viewScale() = fitW*zoom/imgW;overlayScale() = clamp(fitW/imgW, 1, 4);
  resetOverlayTransform(cx) = 恢复「图片坐标 + pad + 倍率」
- 7 个文件共 30 余处 UI 常量改为 X / viewScale();去掉 Math.max(X/zoom, N)
  的托底(N 是图片单位,放大时会变成巨大命中区,导致误触)
- overlay / selection / cursor 三张画布按 overlayScale 建缓冲,绘制坐标仍是图片单位,
  所以全部绘制代码无需改动;四处置空函数(crop/selection/tools/index)改用设备像素
  clearRect + resetOverlayTransform 恢复

必须做成叶子模块:viewport 与 crop/objects/layers 存在循环 import,把换算放在循环里
esbuild 打成 IIFE 后另一侧拿到 undefined —— 实测 clearOverlay 报
"resetOverlayTransform is not defined",编辑器直接打不开(已踩过)。

实测(Playwright 按屏幕分辨率重采样后量像素包围盒):
  96x64    控制点 7~8px   旧实现 101.5px   overlay 288x256 -> 1152x1024
  800x600  控制点 8~9px   旧实现 12.2px
  2400x1800 控制点 9~10px 旧实现 4.1px
纯白内区比视觉尺寸小 1.5px(描边压边),视觉上稳定 11px。

验证:harness 140 项全通(新增 1 项控制点尺寸断言)、validate 9/9。
This commit is contained in:
qiqi committed 2026-11-08 17:49:16 +08:00
1 parent fccb9ae69e
commit 6e1685998f
14 files changed
+320 -154

No files matched your search

+50
View File
@@ -1461,6 +1461,56 @@ check('切到别的层:效果滑杆置灰;切回原层恢复可调', await (
return off && on ? true : '切走禁用=' + off + ' 切回恢复=' + on
})())
check('形状控制点屏幕尺寸恒定(不随图片放大而膨胀)', await (async () => {
// 当前这张 96×64 的图被「适应窗口」放大 ~9 倍。旧实现按 5.5 / S.zoom 画,
// 屏幕尺寸实际是 11 * fitW/imgW ≈ 100px(截图里那种巨大发光点);
// 现在按 viewScale() 换算,应稳定在 11px 左右、与图片尺寸无关。
await page.locator('[data-tool="rect"]').click()
const b = await page.locator('.iwb-editor-layer-canvas').first().boundingBox()
await page.mouse.move(b.x + b.width * 0.25, b.y + b.height * 0.25)
await page.mouse.down()
await page.mouse.move(b.x + b.width * 0.75, b.y + b.height * 0.75, { steps: 6 })
await page.mouse.up()
await page.waitForTimeout(320)
const sizes = await page.evaluate(() => {
const oc = document.querySelector('.iwb-editor-crop-overlay')
const rect = oc.getBoundingClientRect()
const sw = Math.max(1, Math.round(rect.width)), sh = Math.max(1, Math.round(rect.height))
// 重采样到屏幕尺寸:overlay 内部是图片坐标,小图上控制点在画布里只有 1~2px
const tmp = document.createElement('canvas')
tmp.width = sw; tmp.height = sh
const t = tmp.getContext('2d')
t.imageSmoothingEnabled = false
t.drawImage(oc, 0, 0, sw, sh)
const d = t.getImageData(0, 0, sw, sh).data
const seen = new Uint8Array(sw * sh)
const white = (i) => d[i * 4] > 235 && d[i * 4 + 1] > 235 && d[i * 4 + 2] > 235 && d[i * 4 + 3] > 200
const out = []
for (let i = 0; i < sw * sh; i++) {
if (seen[i] || !white(i)) continue
const st = [i]; seen[i] = 1
let mnx = 1e9, mxx = -1, mny = 1e9, mxy = -1, n = 0
while (st.length) {
const p = st.pop(); const px = p % sw, py = (p - px) / sw; n++
if (px < mnx) mnx = px
if (px > mxx) mxx = px
if (py < mny) mny = py
if (py > mxy) mxy = py
for (const q of [p - 1, p + 1, p - sw, p + sw]) {
if (q < 0 || q >= sw * sh || seen[q]) continue
const qx = q % sw
if (Math.abs(qx - px) > 1) continue
if (white(q)) { seen[q] = 1; st.push(q) } else seen[q] = 1
}
}
if (n >= 25) out.push(Math.round(((mxx - mnx) + (mxy - mny)) / 2))
}
return out
})
const max = sizes.length ? Math.max(...sizes) : 0
return sizes.length >= 4 && max <= 16 ? true : '控制点屏幕尺寸=' + JSON.stringify(sizes)
})())
check('无选中时 Ctrl+] 有 toast 提示(不再无声失败)', await (async () => {
await page.keyboard.press('Escape') // 清掉可能残留的选中态
await page.keyboard.press('Control+]')