feat(image-annotate): 1.2.1 默认不预建画笔层(按需自动落标注层)+ 侧栏硬度/透明去重

This commit is contained in:
qiqi committed 2026-11-04 20:06:38 +08:00
1 parent d8e4e0cb26
commit 1bf8220c05
10 files changed
+124 -114

No files matched your search

+3 -12
View File
@@ -49,7 +49,7 @@ import { bindEvents } from './events.js'
<button class="iwb-editor-tool active" data-tool="brush" title="画笔"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z"/><path d="m15 5 4 4"/></svg></button>
<button class="iwb-editor-tool" data-tool="eraser" title="橡皮:擦除当前活跃图层的内容(画笔标注或原图),与画笔共用大小/硬度/透明度"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m7 21-4.3-4.3c-1-1-1-2.5 0-3.4l9.6-9.6c1-1 2.5-1 3.4 0l5.6 5.6c1 1 1 2.5 0 3.4L13 21"/><path d="M22 21H7"/><path d="m5 11 9 9"/></svg></button>
<button class="iwb-editor-tool" data-tool="eraser" title="橡皮:擦除当前活跃图层的内容,与画笔共用大小/硬度/透明度"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m7 21-4.3-4.3c-1-1-1-2.5 0-3.4l9.6-9.6c1-1 2.5-1 3.4 0l5.6 5.6c1 1 1 2.5 0 3.4L13 21"/><path d="M22 21H7"/><path d="m5 11 9 9"/></svg></button>
<button class="iwb-editor-tool" data-tool="pen" title="钢笔:点击添加节点,双击或 Enter 完成自定义形状"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m12 19 7-7 3 3-7 7-3-3z"/><path d="m18 13-1.5-7.5L2 2l3.5 14.5L13 18l5-5z"/><path d="m2 2 7.586 7.586"/><circle cx="11" cy="11" r="2"/></svg></button>
@@ -202,16 +202,7 @@ import { bindEvents } from './events.js'
<button type="button" class="iwb-editor-brush-preset" data-brush-size="24" title="描边 24"><i></i></button>
<button type="button" class="iwb-editor-brush-preset" data-brush-size="32" title="描边 32"><i></i></button>
</div>
<div class="iwb-editor-brush-group">
<label class="iwb-editor-brush-label">硬度
<input type="range" class="iwb-editor-brush-hardness" min="0" max="100" value="100">
<span class="iwb-editor-brush-hardness-val">100</span>
</label>
<label class="iwb-editor-brush-label iwb-editor-opacity-label">透明
<input type="range" class="iwb-editor-brush-opacity" min="5" max="100" step="5" value="100">
<span class="iwb-editor-brush-opacity-val">100%</span>
</label>
</div>
<!-- 硬度/透明滑杆已移除:与就地选项栏(画笔/橡皮工具态)完全重复,参数只留选项栏一处 -->
</div>
@@ -232,7 +223,7 @@ import { bindEvents } from './events.js'
<span class="iwb-editor-status-size">—</span>
<span class="iwb-editor-status-pos">x — y —</span>
<span class="iwb-editor-status-sel">无选区</span>
<span class="iwb-editor-status-layer">当前层:画笔标注</span>
<span class="iwb-editor-status-layer">当前层:原图</span>
<span class="iwb-editor-status-tool">画笔</span>
</div>
<div class="iwb-editor-toast" role="status" aria-live="polite"></div>
+5 -3
View File
@@ -8,7 +8,7 @@
import { S, ZOOM_STEP, runtime } from './state.js'
import { ROT_CURSOR, clamp, getPos, isOverRotationHandle, setView, setZoomFit, zoomStep } from './viewport.js'
import { addBitmapLayer, addBlankLayer, applyBitmapDrag, beginBitmapDrag, clearSelection, getActiveCtx, hitBitmapHandle, removeVecCanvas, renderLayerPanel } from './layers.js'
import { addBitmapLayer, addBlankLayer, applyBitmapDrag, beginBitmapDrag, clearSelection, ensureWritableLayer, getActiveCtx, hitBitmapHandle, removeVecCanvas, renderLayerPanel } from './layers.js'
import { applyColorChange, beginStroke, clearToolRing, drawToolRing, endStroke, eraseSegment, exitPicker, hidePickerTip, sampleColor, setTool, showPickerTip, strokeSegment } from './tools.js'
import { beginMarquee, updateMarquee, endMarquee, beginLasso, updateLasso, endLasso, clearMarquee, syncAnts, renderSelection } from './selection.js'
import { runCommand, toast } from './commands.js'
@@ -208,9 +208,11 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
return
}
/** 画笔工具的按下:无可写图层则忽略,否则存快照并落下第一点 */
/** 画笔工具的按下:无可写图层则自动落「标注」层(用户裁定:默认不预建画笔层),
* 否则存快照并落下第一点。注意 ensureWritableLayer 必须在 saveSnapshot 之前,
* 让首笔 Ctrl+Z 连自动建的层一起撤掉。 */
function onBrushMouseDown(pos) {
if (!getActiveCtx()) return
if (!ensureWritableLayer()) return
S.isDrawing = true
saveSnapshot()
beginStroke()
+4 -11
View File
@@ -279,7 +279,7 @@ import { toast } from './commands.js'
/**
* 还原原图(用户 2026-11-04 裁定):清空标注 = 全部还原到刚打开时的状态 ——
* 删掉所有非原图图层、清掉所有绘制/矢量、复位变换与选区,只留「原图 + 空画笔层」。
* 删掉所有非原图图层、清掉所有绘制/矢量、复位变换与选区,只留「原图」。
* 整步记一个快照,Ctrl+Z 可完整恢复(包括被删的粘贴层与图形)。
* 旧版 resetDraw() 只清像素、留下空白层且清空撤销栈,已废弃。
*/
@@ -321,16 +321,9 @@ import { toast } from './commands.js'
base.opacity = 1
base.z = 0
}
const drawCanvas = createLayerCanvas()
const drawCtx = drawCanvas.getContext('2d', { willReadFrequently: true })
const draw = {
id: 'draw', name: '画笔标注', canvas: drawCanvas, ctx: drawCtx,
opacity: 1, visible: true, locked: false, transformable: true,
x: 0, y: 0, w: S.imgW, h: S.imgH, rotation: 0,
color: S.color, lineWidth: S.brushSize, z: 1
}
S.layers = base ? [base, draw] : [draw]
S.activeLayerId = 'draw'
S.layers = base ? [base] : []
// 默认无画笔层(用户裁定),活动层回到原图;再画时 ensureWritableLayer 兜底建层
S.activeLayerId = base ? 'base' : null
S.zCounter = 1
clearAllEditState()
if (S.renderAll) S.renderAll()
+30
View File
@@ -319,6 +319,36 @@ export function setActiveLayer(id) {
placeImageDirectly(img)
}
/** 确保有可绘制的活动图层:没有(或被锁定)就自动落一个「标注 N」空层并激活。
* 默认不再预建画笔层(用户裁定:需要时自己加),所以画笔起笔前必须兜底建层。
* 注意:本函数不进历史 —— 调用方(onBrushMouseDown)随后 saveSnapshot(),
* 这样首笔 Ctrl+Z 会连「自动建的层 + 笔迹」一起撤掉,不留空层。 */
export function ensureWritableLayer() {
const current = getActiveLayer()
if (current && !current.locked) return current
const id = S.layers.some(l => l.id === 'draw') ? 'L' + Date.now() + Math.floor(Math.random() * 1000) : 'draw'
const canvas = createLayerCanvas()
const ctx = canvas.getContext('2d', { willReadFrequently: true })
ctx.clearRect(0, 0, S.imgW, S.imgH)
const layer = {
id,
name: '标注 ' + (S.layers.filter(l => l.id !== 'base').length + 1),
canvas,
ctx,
opacity: 1,
visible: true,
locked: false,
x: 0, y: 0, w: S.imgW, h: S.imgH,
rotation: 0,
z: ++S.zCounter
}
S.layers.push(layer)
reorderLayerCanvas()
setActiveLayer(id)
renderLayerPanel()
return layer
}
/** 新建空白图层 */
export function addBlankLayer() { // 创建可独立编辑的位图图层
+5 -3
View File
@@ -15,7 +15,7 @@ import { S } from './state.js'
import { clamp, getPos } from './viewport.js'
import { clearOverlay } from './crop.js'
import { defineCommand, runCommand, toast } from './commands.js'
import { createLayerCanvas, getActiveLayer, reorderLayerCanvas, setActiveLayer, updateLayerCanvasStyles } from './layers.js'
import { createLayerCanvas, ensureWritableLayer, getActiveLayer, reorderLayerCanvas, setActiveLayer, updateLayerCanvasStyles } from './layers.js'
// ============ 状态查询 ============
export function hasSelection() { return !!(S.selection && S.selection.active) }
@@ -367,9 +367,11 @@ import { createLayerCanvas, getActiveLayer, reorderLayerCanvas, setActiveLayer,
return true
}
/** 用当前前景色填充选区(只作用当前层) */
/** 用当前前景色填充选区(只作用当前层)。
* 填充是「加内容」的操作:当前层锁定(默认只剩原图)时自动落「标注」层,
* 与画笔的兜底一致;Delete/剪切是「减内容」,保持提示不自动建层。 */
function fillOnActiveLayer() {
const layer = writableLayer()
const layer = ensureWritableLayer()
if (!layer) return false
const sel = S.selection
layer.ctx.save()
+5 -21
View File
@@ -69,27 +69,11 @@ import { getSelectedShape, getSelectedText, renderObjects, shapeBounds, textBoun
z: 0
})
// 创建图层 2: 画笔标注(空透明层)
const drawCanvas = createLayerCanvas()
const drawCtx = drawCanvas.getContext('2d', { willReadFrequently: true })
drawCtx.clearRect(0, 0, S.imgW, S.imgH)
S.layers.push({
id: 'draw',
name: '画笔标注',
canvas: drawCanvas,
ctx: drawCtx,
opacity: 1,
visible: true,
locked: false,
transformable: true,
x: 0, y: 0, w: S.imgW, h: S.imgH,
color: S.color,
lineWidth: S.brushSize,
z: 1
})
S.activeLayerId = 'draw' // 默认活跃图层为画笔层
S.zCounter = Math.max(S.layers.length - 1, 0)
// 默认不再预建「画笔标注」空层(用户裁定 2026-11-04:需要时自己加)。
// 画笔起笔时 ensureWritableLayer() 会自动落一个「标注 N」层兜底,
// 所以这里活动层指向原图(锁定态),不会误画到原图上。
S.activeLayerId = 'base'
S.zCounter = 1
S.textCtx.clearRect(0, 0, S.imgW, S.imgH)
S.strokeCtx.clearRect(0, 0, S.imgW, S.imgH)