feat(image-annotate): 1.2.0 非破坏性裁剪(可撤销/不 flatten)+ 工具参数解耦与就地选项栏

This commit is contained in:
qiqi committed 2026-11-04 18:30:54 +08:00
1 parent a4e89f5f8b
commit d8e4e0cb26
13 files changed
+1768 -1243

No files matched your search

+83 -63
View File
@@ -1,14 +1,15 @@
/* crop.js - 裁剪工具
* 节:裁剪、裁剪栏。
* 遮罩绘制写 S.overlayCanvas,确认裁剪后重绘位图图层。
* 遮罩绘制写 S.overlayCanvas;确认裁剪后逐图层搬运像素(v1.2 起不再 flatten)。
*/
'use strict'
import { S, runtime } from './state.js'
import { getCompositeCanvas } from './tools.js'
import { setupCanvas } from './viewport.js'
import { resizeWorkspace } from './viewport.js'
import { disposeSelection } from './selection.js'
import { saveSnapshot } from './history.js'
import { toast } from './commands.js'
// ============ 裁剪 ============
@@ -235,59 +236,80 @@ import { disposeSelection } from './selection.js'
S.cropRect = { x: nx, y: ny, w: nw, h: nh }
}
/**
* 确认裁剪(v1.2 起非破坏性:可撤销、不 flatten)。
*
* 旧实现把可见图层合成成一张图再 setupCanvas,等于:① 所有图层被合并成一张;
* ② S.history 直接清零 —— 裁错了没有任何退路。现在改为:
* ① 先 saveSnapshot('裁剪'),快照记的是「裁剪前」的尺寸 + 每层像素;
* ② 逐图层把可见区域搬进新尺寸的 canvas(图层结构、命名、透明度、锁定全保留);
* ③ resizeWorkspace 只换尺寸不重建图层,像素随后写回。
* 撤销时 restoreState 会先把画布尺寸复位到快照里的 imgW/imgH,再回填像素。
*/
export function confirmCrop() {
if (!S.cropRect) return
const { x, y, w, h } = S.cropRect
if (w < 2 || h < 2) { cancelCrop(); return }
// 画布尺寸即将变化:旧坐标系下的选区/拉框/蚂蚁线/浮动条全部失效,
// 必须按 disposeSelection 级别清理,否则之后 Delete 会按旧 bbox
// 在新画布上开超大掩膜,把当前层整层擦掉。
disposeSelection()
// 合成所有可见图层
const tmp = getCompositeCanvas()
const sx = Math.round(x), sy = Math.round(y)
const sw = Math.round(w), sh = Math.round(h)
const cropC = document.createElement('canvas')
const c = S.cropConstraint
// 计算实际在原图范围内的源区域(裁剪框可超出原图边界,超出部分为透明)
const srcX = Math.max(0, sx)
const srcY = Math.max(0, sy)
const srcRight = Math.min(S.imgW, sx + sw)
const srcBottom = Math.min(S.imgH, sy + sh)
const srcW = Math.max(0, srcRight - srcX)
const srcH = Math.max(0, srcBottom - srcY)
// 在裁剪画布中的偏移
const dstX = srcX - sx
const dstY = srcY - sy
if (c.type === 'size' && c.sizeW > 0 && c.sizeH > 0) {
// 尺寸约束:先画到 sw×sh 画布再缩放到目标尺寸
cropC.width = c.sizeW
cropC.height = c.sizeH
const cctx = cropC.getContext('2d')
const scaleX = c.sizeW / sw
const scaleY = c.sizeH / sh
if (srcW > 0 && srcH > 0) {
cctx.drawImage(tmp, srcX, srcY, srcW, srcH,
dstX * scaleX, dstY * scaleY, srcW * scaleX, srcH * scaleY)
}
S.imgW = c.sizeW
S.imgH = c.sizeH
} else {
cropC.width = sw
cropC.height = sh
const cctx = cropC.getContext('2d')
if (srcW > 0 && srcH > 0) {
cctx.drawImage(tmp, srcX, srcY, srcW, srcH, dstX, dstY, srcW, srcH)
}
S.imgW = sw
S.imgH = sh
}
// 文字/形状图层坐标随裁剪区域变换
const sx = Math.round(x), sy = Math.round(y)
const sw = Math.max(1, Math.round(w)), sh = Math.max(1, Math.round(h))
const isSize = c.type === 'size' && c.sizeW > 0 && c.sizeH > 0
const scaleX = isSize ? c.sizeW / sw : 1
const scaleY = isSize ? c.sizeH / sh : 1
const nw = isSize ? Math.max(1, Math.round(c.sizeW)) : sw
const nh = isSize ? Math.max(1, Math.round(c.sizeH)) : sh
// 完全没变就别记一步空历史
if (nw === S.imgW && nh === S.imgH && sx === 0 && sy === 0) { cancelCrop(); return }
const oldW = S.imgW, oldH = S.imgH
const scaleX = nw / sw, scaleY = nh / sh
// 裁剪框可以超出画面:源矩形先夹到画面内,目标矩形按同一比例落位(超出部分为透明)
const srcX = Math.max(0, sx), srcY = Math.max(0, sy)
const srcW = Math.max(0, Math.min(oldW, sx + sw) - srcX)
const srcH = Math.max(0, Math.min(oldH, sy + sh) - srcY)
const dstX = (srcX - sx) * scaleX, dstY = (srcY - sy) * scaleY
const dstW = srcW * scaleX, dstH = srcH * scaleY
// ① 进历史:快照记「裁剪前」的尺寸与每层像素
saveSnapshot('裁剪')
// ② 旧坐标系下的选区/拉框/蚂蚁线/浮动条全部失效
disposeSelection()
// ③ 逐图层把可见区域搬出来(保留图层结构,不再合成成一张)
const staged = S.layers.map(layer => {
const tmp = document.createElement('canvas')
tmp.width = nw
tmp.height = nh
if (srcW > 0 && srcH > 0) {
try {
tmp.getContext('2d').drawImage(layer.canvas, srcX, srcY, srcW, srcH, dstX, dstY, dstW, dstH)
} catch (e) { /* 单层搬运失败不阻断其余 */ }
}
return tmp
})
// ④ 换尺寸(会清空图层像素)后把搬出来的内容写回
resizeWorkspace(nw, nh)
S.layers.forEach((layer, i) => {
const tmp = staged[i]
try { layer.ctx.drawImage(tmp, 0, 0) } catch (e) { /* ignore */ }
tmp.width = 0
tmp.height = 0
// 像素已落在新坐标系的正确位置,几何元数据不能再参与二次定位;
// 图片源(image / sourceCanvas)尺寸是旧的,留着会让重绘错位,必须摘掉。
// 摘掉后这些层按「像素层」参与快照与恢复(见 vendor/layer-logic.js)。
layer.x = 0
layer.y = 0
layer.w = nw
layer.h = nh
layer.rotation = 0
layer.transformable = false
layer.image = null
layer.sourceCanvas = null
layer._srcBox = null
layer._bounds = undefined
})
// ⑤ 文字/形状按裁剪区域位移与缩放
S.textLayers = S.textLayers.map(t => ({
...t,
x: (t.x - sx) * scaleX,
@@ -295,31 +317,29 @@ import { disposeSelection } from './selection.js'
fontSize: t.fontSize * scaleX
}))
S.shapeLayers = S.shapeLayers.map(s => ({
...s,
...(s.type === 'pen' ? { pts: (s.pts || []).map(p => ({ x: (p.x - sx) * scaleX, y: (p.y - sy) * scaleY })) } : {
x1: (s.x1 - sx) * scaleX, y1: (s.y1 - sy) * scaleY,
x2: (s.x2 - sx) * scaleX, y2: (s.y2 - sy) * scaleY
}),
lw: Math.max(1, s.lw * (scaleX + scaleY) / 2)
}))
...s,
...(s.type === 'pen' ? { pts: (s.pts || []).map(p => ({ x: (p.x - sx) * scaleX, y: (p.y - sy) * scaleY })) } : {
x1: (s.x1 - sx) * scaleX, y1: (s.y1 - sy) * scaleY,
x2: (s.x2 - sx) * scaleX, y2: (s.y2 - sy) * scaleY
}),
lw: Math.max(1, s.lw * (scaleX + scaleY) / 2)
}))
S.selectedTextId = null
S.textDrag = null
S.selectedShapeId = null
S.shapeDrag = null
// 裁剪后 setupCanvas 会重建全部位图图层,旧 selectedLayerId 会指向已删除的 id
// 裁剪后图层 id 不变但几何全变,旧的选中 id 与拖拽中间态都要清
S.selectedLayerId = null
S.bitmapDrag = null
S.zoom = 1
setupCanvas(cropC)
S.history = []
S.redoStack = []
S.cropRect = null
S.cropHandle = null
S.cropMoving = false
S.moveStart = null
clearOverlay()
updateCropInfo()
// 尺寸/坐标系已变,统一重绘出口要在新画布上重建选区层与浮动条位置
if (S.renderAll) S.renderAll()
toast('已裁剪为 ' + nw + '×' + nh + '(Ctrl+Z 可撤销)', 'success')
}
export function cancelCrop() {