feat(image-annotate): 1.2.0 非破坏性裁剪(可撤销/不 flatten)+ 工具参数解耦与就地选项栏
This commit is contained in:
1 parent
a4e89f5f8b
commit
d8e4e0cb26
13 files changed
+1768
-1243
No files matched your search
@@ -231,14 +231,14 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
if (!S.penPath) S.penPath = { pts: [], preview: null, closed: false }
|
||||
const point = { x: clamp(pos.x, 0, S.imgW), y: clamp(pos.y, 0, S.imgH) }
|
||||
const first = S.penPath.pts[0]
|
||||
if (first && S.penPath.pts.length >= 3 && Math.hypot(point.x - first.x, point.y - first.y) <= Math.max(12 / S.zoom, S.brushSize * 1.5)) {
|
||||
if (first && S.penPath.pts.length >= 3 && Math.hypot(point.x - first.x, point.y - first.y) <= Math.max(12 / S.zoom, S.shapeLineWidth * 1.5)) {
|
||||
S.penPath.closed = true
|
||||
S.penPath.nearStart = true
|
||||
S.penPath.preview = null
|
||||
commitPenPath()
|
||||
} else {
|
||||
S.penPath.pts.push(point)
|
||||
renderObjects({ id: 'pen-preview', type: 'pen', pts: S.penPath.pts.concat(S.penPath.preview ? [S.penPath.preview] : []), closed: false, color: S.color, lw: Math.max(2, S.brushSize), fill: false, opacity: 1, visible: true })
|
||||
renderObjects({ id: 'pen-preview', type: 'pen', pts: S.penPath.pts.concat(S.penPath.preview ? [S.penPath.preview] : []), closed: false, color: S.color, lw: Math.max(1, S.shapeLineWidth), fill: false, opacity: 1, visible: true })
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -423,7 +423,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
/** 钢笔路径预览:更新悬停点与起点闭合提示 */
|
||||
function onPenDrawMove(pos) {
|
||||
if (S.penPath) { S.penPath.preview = pos; const first = S.penPath.pts[0]
|
||||
const nearStart = first && S.penPath.pts.length >= 3 && Math.hypot(pos.x - first.x, pos.y - first.y) <= Math.max(12 / S.zoom, S.brushSize * 1.5)
|
||||
const nearStart = first && S.penPath.pts.length >= 3 && Math.hypot(pos.x - first.x, pos.y - first.y) <= Math.max(12 / S.zoom, S.shapeLineWidth * 1.5)
|
||||
S.penPath.nearStart = !!nearStart
|
||||
renderObjects() }
|
||||
}
|
||||
@@ -436,8 +436,8 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
x1: S.startX, y1: S.startY,
|
||||
x2: pos.x, y2: pos.y,
|
||||
color: S.color,
|
||||
lw: Math.max(2, S.brushSize),
|
||||
lineWidth: Math.max(2, S.brushSize),
|
||||
lw: Math.max(1, S.shapeLineWidth),
|
||||
lineWidth: Math.max(1, S.shapeLineWidth),
|
||||
fill: S.tool === 'rect' || S.tool === 'circle' ? !!S.rectFill : false,
|
||||
opacity: 1,
|
||||
visible: true
|
||||
@@ -635,7 +635,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
x1: S.startX, y1: S.startY,
|
||||
x2: pos.x, y2: pos.y,
|
||||
color: S.color,
|
||||
lw: Math.max(2, S.brushSize),
|
||||
lw: Math.max(1, S.shapeLineWidth),
|
||||
fill: S.tool === 'rect' || S.tool === 'circle' ? !!S.rectFill : false,
|
||||
opacity: 1,
|
||||
visible: true,
|
||||
@@ -799,32 +799,101 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
picker: '吸管', hand: '抓手', crop: '裁剪'
|
||||
}
|
||||
|
||||
/** 选项栏只露当前工具 2–3 个参数;用独立控件,避免与右侧旧参数区抢 querySelector */
|
||||
export function renderOptionBar() {
|
||||
const body = runtime.el && runtime.el.querySelector('.iwb-editor-optionbar-body')
|
||||
if (!body) return
|
||||
let html = ''
|
||||
if (S.tool === 'marquee') {
|
||||
html = `<span class="iwb-opt-label">形状</span>
|
||||
const OPT_HINTS = {
|
||||
crop: '裁剪栏在画布下方;裁剪与选区互斥,确认后可 Ctrl+Z 撤销',
|
||||
select: '点击选中元素,拖拽移动,手柄缩放',
|
||||
picker: '点击画布取色,取完自动切回上一个工具',
|
||||
hand: '拖拽平移画布;任意工具下按住空格也可以',
|
||||
marquee: '拖出选区 → 操作条就在选区旁边',
|
||||
lasso: '按住拖动画圈,松手闭合'
|
||||
}
|
||||
|
||||
/** 滑杆 + 数字框共用同一 data-opt,两条输入路径都写回 S */
|
||||
function rangeOpt(key, label, min, max, value, unit) {
|
||||
return `<span class="iwb-opt-label">${label}</span>
|
||||
<input type="range" class="iwb-opt-range" data-opt="${key}" min="${min}" max="${max}" step="1" value="${value}">
|
||||
<input type="number" class="iwb-opt-num" data-opt="${key}" min="${min}" max="${max}" step="1" value="${value}">${unit ? `<span class="iwb-opt-unit">${unit}</span>` : ''}`
|
||||
}
|
||||
|
||||
/** 每个工具只露最常用的 2–4 个参数,全部直接写回 S(右侧参数面板同步) */
|
||||
function optionBarHTML(tool) {
|
||||
if (tool === 'brush' || tool === 'eraser') {
|
||||
return rangeOpt('brushSize', '大小', 1, 300, S.brushSize, '')
|
||||
+ `<span class="iwb-opt-label">硬度</span>
|
||||
<input type="range" class="iwb-opt-range" data-opt="brushHardness" min="0" max="100" step="1" value="${S.brushHardness}">
|
||||
<span class="iwb-opt-val" data-opt-val="brushHardness">${S.brushHardness}</span>
|
||||
<span class="iwb-opt-label">透明</span>
|
||||
<input type="range" class="iwb-opt-range" data-opt="brushOpacity" min="5" max="100" step="1" value="${Math.round(S.brushOpacity * 100)}">
|
||||
<span class="iwb-opt-val" data-opt-val="brushOpacity">${Math.round(S.brushOpacity * 100)}%</span>`
|
||||
}
|
||||
if (tool === 'rect' || tool === 'circle') {
|
||||
return rangeOpt('shapeLineWidth', '线宽', 1, 60, S.shapeLineWidth, '')
|
||||
+ `<span class="iwb-opt-label">样式</span>
|
||||
<button class="iwb-opt-btn${S.rectFill ? '' : ' active'}" data-opt-toggle="stroke" data-value="stroke">描边</button>
|
||||
<button class="iwb-opt-btn${S.rectFill ? ' active' : ''}" data-opt-toggle="fill" data-value="fill">实心</button>`
|
||||
}
|
||||
if (tool === 'arrow' || tool === 'pen') {
|
||||
return rangeOpt('shapeLineWidth', '线宽', 1, 60, S.shapeLineWidth, '')
|
||||
+ `<span class="iwb-opt-hint">${tool === 'pen' ? '逐点点击,点回起点闭合' : '拖出箭头,松手完成'}</span>`
|
||||
}
|
||||
if (tool === 'text') {
|
||||
return rangeOpt('textSize', '字号', 8, 400, S.textSize, 'px')
|
||||
+ '<span class="iwb-opt-hint">点画布输入文字,Enter 确认</span>'
|
||||
}
|
||||
if (tool === 'marquee') {
|
||||
return `<span class="iwb-opt-label">形状</span>
|
||||
<button class="iwb-opt-btn${S.selectionKind === 'rect' ? ' active' : ''}" data-opt="shape" data-value="rect">矩形</button>
|
||||
<button class="iwb-opt-btn${S.selectionKind === 'ellipse' ? ' active' : ''}" data-opt="shape" data-value="ellipse">椭圆</button>
|
||||
<label class="iwb-opt-label">羽化 <input type="number" class="iwb-opt-num" data-opt="feather" min="0" max="20" step="1" value="${S.selectionFeather}"> px</label>
|
||||
<span class="iwb-opt-hint">拖出选区 → 操作条就在选区旁边</span>`
|
||||
} else if (S.tool === 'lasso') {
|
||||
html = `<span class="iwb-opt-label">套索</span>
|
||||
<label class="iwb-opt-label">羽化 <input type="number" class="iwb-opt-num" data-opt="feather" min="0" max="20" step="1" value="${S.selectionFeather}"> px</label>
|
||||
<span class="iwb-opt-hint">按住拖动画圈,松手闭合</span>`
|
||||
} else {
|
||||
const hints = {
|
||||
crop: '裁剪栏在画布下方;裁剪与选区互斥',
|
||||
brush: '选区激活时笔刷只画在选区内',
|
||||
eraser: '橡皮只擦当前画笔目标图层(✎)',
|
||||
select: '点击选中元素,拖拽移动,手柄缩放'
|
||||
}
|
||||
html = `<span class="iwb-opt-current">当前工具:${TOOL_NAMES[S.tool] || S.tool}</span>
|
||||
<span class="iwb-opt-hint">${hints[S.tool] || '选区工具按 M / L;Ctrl+J 复制选区,Delete 清除当前层'}</span>`
|
||||
<span class="iwb-opt-hint">${OPT_HINTS.marquee}</span>`
|
||||
}
|
||||
body.innerHTML = html
|
||||
if (tool === 'lasso') {
|
||||
return `<span class="iwb-opt-label">羽化 <input type="number" class="iwb-opt-num" data-opt="feather" min="0" max="20" step="1" value="${S.selectionFeather}"> px</span>
|
||||
<span class="iwb-opt-hint">${OPT_HINTS.lasso}</span>`
|
||||
}
|
||||
return `<span class="iwb-opt-current">当前工具:${TOOL_NAMES[tool] || tool}</span>
|
||||
<span class="iwb-opt-hint">${OPT_HINTS[tool] || '选区工具按 M / L;Ctrl+J 复制选区,Delete 清除当前层'}</span>`
|
||||
}
|
||||
|
||||
export function renderOptionBar(force) {
|
||||
const body = runtime.el && runtime.el.querySelector('.iwb-editor-optionbar-body')
|
||||
if (!body) return
|
||||
// 工具没变就只同步数值:renderAll 每次操作都会走到这里,全量重建 innerHTML
|
||||
// 会打断正在拖的滑杆和正在输入的数字框(静态审查 P2-11)。
|
||||
if (!force && body.dataset.tool === S.tool) { syncOptionBar(); return }
|
||||
body.dataset.tool = S.tool
|
||||
body.innerHTML = optionBarHTML(S.tool)
|
||||
syncOptionBar()
|
||||
}
|
||||
|
||||
/** 只更新值与选中态,不重建 DOM —— 保证拖拽中的控件与输入焦点不被抢 */
|
||||
export function syncOptionBar() {
|
||||
const body = runtime.el && runtime.el.querySelector('.iwb-editor-optionbar-body')
|
||||
if (!body) return
|
||||
const val = (key) => {
|
||||
if (key === 'brushSize') return S.brushSize
|
||||
if (key === 'brushHardness') return S.brushHardness
|
||||
if (key === 'brushOpacity') return Math.round(S.brushOpacity * 100)
|
||||
if (key === 'shapeLineWidth') return S.shapeLineWidth
|
||||
if (key === 'textSize') return S.textSize
|
||||
if (key === 'feather') return S.selectionFeather
|
||||
return ''
|
||||
}
|
||||
body.querySelectorAll('input[data-opt]').forEach(el => {
|
||||
if (document.activeElement === el) return // 正在拖/正在输入的不要被覆盖
|
||||
const v = val(el.dataset.opt)
|
||||
if (String(el.value) !== String(v)) el.value = v
|
||||
})
|
||||
body.querySelectorAll('[data-opt-val]').forEach(el => {
|
||||
const v = val(el.dataset.optVal)
|
||||
el.textContent = el.dataset.optVal === 'brushOpacity' ? v + '%' : v
|
||||
})
|
||||
body.querySelectorAll('[data-opt="shape"]').forEach(btn => {
|
||||
btn.classList.toggle('active', S.selectionKind === btn.dataset.value)
|
||||
})
|
||||
body.querySelectorAll('[data-opt-toggle]').forEach(btn => {
|
||||
btn.classList.toggle('active', !!S.rectFill === (btn.dataset.value === 'fill'))
|
||||
})
|
||||
}
|
||||
|
||||
/** 状态栏:缩放 / 尺寸 / 光标 / 选区 / 当前层 / 工具 */
|
||||
@@ -847,6 +916,74 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
put('.iwb-editor-status-tool', TOOL_NAMES[S.tool] || S.tool)
|
||||
}
|
||||
|
||||
// ---- 选项栏参数写回:单一真源是 S,右侧参数面板与画布随后同步 ----
|
||||
|
||||
let lwSnapDone = false
|
||||
let textSnapDone = false
|
||||
|
||||
/** 线宽也作用到当前选中的形状:拖动中只重绘,松手才记一步历史 */
|
||||
function applyShapeLineWidth(committed) {
|
||||
const sh = getSelectedShape()
|
||||
if (!sh || sh.locked) { lwSnapDone = false; return }
|
||||
if (committed || !lwSnapDone) { saveSnapshot('线宽'); lwSnapDone = true }
|
||||
sh.lw = S.shapeLineWidth
|
||||
renderObjects()
|
||||
if (committed) lwSnapDone = false
|
||||
}
|
||||
|
||||
/** 字号也作用到当前选中的文字 */
|
||||
function applyTextSize(committed) {
|
||||
const t = getSelectedText()
|
||||
if (!t || t.locked) { textSnapDone = false; return }
|
||||
if (committed || !textSnapDone) { saveSnapshot('字号'); textSnapDone = true }
|
||||
t.fontSize = S.textSize
|
||||
renderObjects()
|
||||
if (committed) textSnapDone = false
|
||||
}
|
||||
|
||||
/** 形状描边/实心:既改下次新建的默认值,也改当前选中的那个 */
|
||||
function setShapeFill(fill) {
|
||||
S.rectFill = !!fill
|
||||
updateFillUI()
|
||||
syncOptionBar()
|
||||
const sh = getSelectedShape()
|
||||
if (sh && (sh.type === 'rect' || sh.type === 'circle') && !sh.locked) {
|
||||
saveSnapshot('填充')
|
||||
sh.fill = S.rectFill
|
||||
renderObjects()
|
||||
}
|
||||
}
|
||||
|
||||
function setOptionFromControl(el, committed) {
|
||||
const key = el && el.dataset ? el.dataset.opt : ''
|
||||
if (!key) return
|
||||
const n = Math.round(Number(el.value))
|
||||
const safe = Number.isFinite(n) ? n : 0
|
||||
if (key === 'brushSize') {
|
||||
S.brushSize = clamp(safe || 1, 1, 300)
|
||||
updateBrushUI()
|
||||
updateBrushParamUI()
|
||||
} else if (key === 'brushHardness') {
|
||||
S.brushHardness = clamp(safe, 0, 100)
|
||||
updateBrushParamUI()
|
||||
} else if (key === 'brushOpacity') {
|
||||
S.brushOpacity = clamp(safe || 100, 5, 100) / 100
|
||||
updateBrushParamUI()
|
||||
if (S.strokeCanvas) S.strokeCanvas.style.opacity = S.brushOpacity
|
||||
} else if (key === 'shapeLineWidth') {
|
||||
S.shapeLineWidth = clamp(safe || 1, 1, 60)
|
||||
applyShapeLineWidth(committed)
|
||||
} else if (key === 'textSize') {
|
||||
S.textSize = clamp(safe || 32, 8, 400)
|
||||
applyTextSize(committed)
|
||||
} else if (key === 'feather') {
|
||||
S.selectionFeather = clamp(safe, 0, 20)
|
||||
if (S.selection && S.selection.active) S.selection.feather = S.selectionFeather
|
||||
updateStatusbar()
|
||||
}
|
||||
syncOptionBar()
|
||||
}
|
||||
|
||||
/** 更多菜单 / 面板收起 / 选项栏控件:只绑定一次,DOM 重绘不丢事件(委托) */
|
||||
export function bindLiteUI() {
|
||||
const root = runtime.el
|
||||
@@ -907,23 +1044,25 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
const body = root.querySelector('.iwb-editor-optionbar-body')
|
||||
if (body) {
|
||||
body.addEventListener('mousedown', (e) => e.stopPropagation())
|
||||
// 滑杆拖动中(input)与松手/失焦(change)走同一条写回路径,
|
||||
// committed 只决定「要不要顺手记一步历史」,不影响参数生效。
|
||||
body.addEventListener('input', (e) => setOptionFromControl(e.target, false))
|
||||
body.addEventListener('change', (e) => setOptionFromControl(e.target, true))
|
||||
body.addEventListener('click', (e) => {
|
||||
const btn = e.target.closest('[data-opt="shape"]')
|
||||
if (!btn) return
|
||||
e.stopPropagation()
|
||||
S.selectionKind = btn.dataset.value === 'ellipse' ? 'ellipse' : 'rect'
|
||||
renderOptionBar()
|
||||
toast('选区形状:' + (S.selectionKind === 'ellipse' ? '椭圆' : '矩形'), 'info')
|
||||
const shapeBtn = e.target.closest('[data-opt="shape"]')
|
||||
if (shapeBtn) {
|
||||
e.stopPropagation()
|
||||
S.selectionKind = shapeBtn.dataset.value === 'ellipse' ? 'ellipse' : 'rect'
|
||||
syncOptionBar()
|
||||
toast('选区形状:' + (S.selectionKind === 'ellipse' ? '椭圆' : '矩形'), 'info')
|
||||
return
|
||||
}
|
||||
const toggle = e.target.closest('[data-opt-toggle]')
|
||||
if (toggle) {
|
||||
e.stopPropagation()
|
||||
setShapeFill(toggle.dataset.value === 'fill')
|
||||
}
|
||||
})
|
||||
const onFeather = (e) => {
|
||||
const input = e.target.closest('[data-opt="feather"]')
|
||||
if (!input) return
|
||||
S.selectionFeather = clamp(parseInt(input.value, 10) || 0, 0, 20)
|
||||
if (S.selection && S.selection.active) S.selection.feather = S.selectionFeather
|
||||
updateStatusbar()
|
||||
}
|
||||
body.addEventListener('input', onFeather)
|
||||
body.addEventListener('change', onFeather)
|
||||
}
|
||||
|
||||
// 点画布 / 面板其它地方收起下拉。必须用捕获阶段:modal 自身有个
|
||||
@@ -1277,7 +1416,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
function commitPenPath() {
|
||||
if (!S.penPath || S.penPath.pts.length < 2) { S.penPath = null; renderObjects(); return }
|
||||
saveSnapshot()
|
||||
const shape = { id: 's' + Date.now() + Math.floor(Math.random() * 1000), type: 'pen', pts: S.penPath.pts.slice(), closed: !!S.penPath.closed, color: S.color, lw: Math.max(2, S.brushSize), fill: !!S.penPath.closed, opacity: 1, visible: true, locked: false, z: ++S.zCounter }
|
||||
const shape = { id: 's' + Date.now() + Math.floor(Math.random() * 1000), type: 'pen', pts: S.penPath.pts.slice(), closed: !!S.penPath.closed, color: S.color, lw: Math.max(1, S.shapeLineWidth), fill: !!S.penPath.closed, opacity: 1, visible: true, locked: false, z: ++S.zCounter }
|
||||
S.shapeLayers.push(shape); selectUnit('shape', shape.id); S.penPath = null; renderObjects(); renderLayerPanel()
|
||||
}
|
||||
|
||||
@@ -1452,9 +1591,24 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
S.drawCanvasCursor('crosshair')
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否处于「正在输入文字」态 —— 只对**文字内容**输入框为真。
|
||||
*
|
||||
* v1.2 修正:原来任何 INPUT 都算打字,于是在选项栏的数字框(羽化/线宽/字号)
|
||||
* 里输完值后,Ctrl+A、Ctrl+J、Delete、Esc 全被吞掉,表现成「快捷键突然没反应」。
|
||||
* 参数控件里带修饰键的组合、Delete 与 Esc 一律放行给画布;单个字符仍然正常输入。
|
||||
*/
|
||||
function isTyping(e) {
|
||||
const t = e.target
|
||||
return t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA')
|
||||
if (!t) return false
|
||||
if (t.tagName === 'TEXTAREA') return true
|
||||
if (t.tagName !== 'INPUT') return false
|
||||
const cls = t.classList
|
||||
if (cls && (cls.contains('iwb-editor-text-input') || cls.contains('iwb-editor-color-hex-input'))) return true
|
||||
// 以下是参数控件(数字框 / 滑杆)
|
||||
if (e.ctrlKey || e.metaKey || e.altKey) return false
|
||||
if (e.key === 'Delete' || e.key === 'Escape') return false
|
||||
return true
|
||||
}
|
||||
|
||||
function onKeydown(e) {
|
||||
|
||||
Reference in new issue
Block a user