feat(image-annotate): v1.7.0 图层面板补齐(缩略图 / 重命名 / 不透明度)
按通读代码后定的顺序处理 P3 剩余项里最有价值的三条。
1) 不透明度真正可调
opacity 字段一直在(合成/合并/导出/快照都读它),但面板没有任何控件能写它,
roadmap 里标的「已有」与事实不符。现在选中层后在该项下方展开滑杆:
- 位图走 canvas 的 CSS opacity(像素不动),导出/合并时乘 globalAlpha,所见即所得
- 文字/形状改完重绘矢量层(不透明度已烘进像素,不能再叠 CSS)
- input 第一次到达才拍快照,一次拖动只产生 1 条历史;双击滑杆复位 100%
- 非 100% 且未选中的层在名字旁挂百分比角标
2) 缩略图 + 双击重命名
- 22px 缩略图(canvas 44 设备像素),等比缩放居中;空图层透出类型图标兜底
- 绘制合批 90ms,避免每次 renderLayerPanel 都重画几十张
- 双击名字改名(Enter 提交 / Esc 放弃),改名期间禁用整项拖动
3) 顺手清理
- 删除 .iwb-layer-color / .iwb-layer-width 两个永远看不见的死控件
(CSS display:none !important 写了三遍),连同绑定与死样式
- pasteImageFromUrl 的 window.alert 改 toast(阻塞弹窗在宿主里会卡整页)
- resetState() 补清 layerMulti / hoverPos / lastClick / lastNameClick / altPicker
关键修正:矢量层不透明度只能乘一次。
drawVectorToCanvas / drawShape 已把 opacity 画进像素,getCompositeCanvas 与
composeUnitsInto 再乘 globalAlpha 就是双倍透明 —— 两处都改为只对位图乘。
验证:harness 133 项全通(新增 4 项)、validate 9/9、产物 331811 B。
harness 的版本断言改为读 package.json,不再硬编码。
This commit is contained in:
1 parent
7c2e23e4e3
commit
3e76f2792e
11 files changed
+593
-105
No files matched your search
@@ -13,7 +13,6 @@ import { boxCorners, clamp, drawRotationHandles, hitRotationHandle, rotPt, unrot
|
||||
import { drawShape } from './tools.js'
|
||||
import { saveSnapshot, snapshotState } from './history.js'
|
||||
import { placeImageDirectly } from './placement.js'
|
||||
import { updateBrushParamUI, updateColorUI, refreshBrushRing } from './events.js'
|
||||
import { isDrawableLayer } from '../vendor/layer-logic.js'
|
||||
|
||||
|
||||
@@ -310,8 +309,12 @@ import { isDrawableLayer } from '../vendor/layer-logic.js'
|
||||
|
||||
/** 切换画笔/橡皮作用的活动图层 */
|
||||
export function setActiveLayer(id) {
|
||||
// 已经既活跃又选中时不必重建面板:renderLayerPanel 会换掉整块 innerHTML,
|
||||
// 每次点击都重建会让「点第二下改名」这类连续交互彻底失效(v1.7)
|
||||
const changed = S.activeLayerId !== id || S.selectedLayerId !== id
|
||||
S.activeLayerId = id
|
||||
selectUnit('bitmap', id)
|
||||
if (!changed) return
|
||||
renderLayerPanel()
|
||||
renderObjects()
|
||||
}
|
||||
@@ -485,6 +488,8 @@ export function setActiveLayer(id) {
|
||||
layer.canvas.style.height = h + 'px'
|
||||
layer.canvas.style.left = offset + 'px'
|
||||
layer.canvas.style.top = offset + 'px'
|
||||
// 位图图层的不透明度走 CSS(像素不动):画面上看到的效果才和导出/合并一致
|
||||
layer.canvas.style.opacity = String(layer.opacity == null ? 1 : layer.opacity)
|
||||
}
|
||||
styleVectorViewport(w, h, offset)
|
||||
}
|
||||
@@ -501,6 +506,39 @@ export function setActiveLayer(id) {
|
||||
|
||||
// ============ 图层面板 UI ============
|
||||
|
||||
/** HTML 转义:图层名可能来自用户输入的文字内容(含 < > & "),拼进模板会破坏结构 */
|
||||
function esc(v) {
|
||||
return String(v == null ? '' : v)
|
||||
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
}
|
||||
|
||||
/** 0~1 的不透明度 → 面板展示用的整数百分比 */
|
||||
function opacityPct(v) {
|
||||
const n = v == null ? 1 : v
|
||||
return Math.max(0, Math.min(100, Math.round(n * 100)))
|
||||
}
|
||||
|
||||
/** 按 id/type 找到图层单元对应的对象(位图 / 文字 / 形状共用一套面板控件) */
|
||||
function findPanelTarget(type, id) {
|
||||
if (type === 'bitmap') return S.layers.find(l => l.id === id) || null
|
||||
if (type === 'text') return S.textLayers.find(t => t.id === id) || null
|
||||
if (type === 'shape') return S.shapeLayers.find(s => s.id === id) || null
|
||||
return null
|
||||
}
|
||||
|
||||
/** 图层不透明度落到画面上:
|
||||
* 位图走 canvas 的 CSS opacity(像素不动,导出时由合成环节乘 globalAlpha);
|
||||
* 文字/形状的不透明度在 drawShape / drawVectorToCanvas 里已经烘进像素,
|
||||
* 改完只需重绘矢量层,不能再叠一层 CSS,否则就是双倍透明。 */
|
||||
function applyOpacityVisual(type, obj) {
|
||||
if (type === 'bitmap' && obj && obj.canvas) {
|
||||
obj.canvas.style.opacity = String(obj.opacity == null ? 1 : obj.opacity)
|
||||
return
|
||||
}
|
||||
if (type === 'text' || type === 'shape') renderObjects()
|
||||
}
|
||||
|
||||
export function renderLayerPanel() {
|
||||
const list = runtime.el.querySelector('.iwb-editor-layer-list')
|
||||
if (!list) return
|
||||
@@ -565,17 +603,28 @@ export function setActiveLayer(id) {
|
||||
? '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="5" y="10" width="14" height="10" rx="2"/><path d="M8 10V7a4 4 0 0 1 8 0v3"/></svg>'
|
||||
: '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="5" y="10" width="14" height="10" rx="2"/><path d="M8 10V7a4 4 0 0 1 7-2"/></svg>'
|
||||
const delSvg = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>'
|
||||
// 不透明度百分比:0~100 整数。选中项下面的滑杆已经显示数值,角标只给未选中的层做提醒,
|
||||
// 免得窄面板里「名字 + ✎ + 角标 + 锁 + 删除」把图层名挤成几个字
|
||||
const pct = opacityPct(item.opacity)
|
||||
const canRename = item.type === 'bitmap'
|
||||
html += `<div class="iwb-layer-item${activeClass}${multiClass}${visClass}" draggable="${item.id !== 'base'}" data-layer-id="${item.id}" data-layer-type="${item.type}">
|
||||
<button class="iwb-layer-vis" data-vis-id="${item.id}" data-vis-type="${item.type}" title="${item.visible ? '隐藏' : '显示'}">${visSvg}</button>
|
||||
<span class="iwb-layer-icon">${typeIconSvg}</span>
|
||||
<span class="iwb-layer-name">${item.name}</span>${item.paintTarget ? '<span class="iwb-layer-paint" title="当前画笔 / 橡皮作用图层">✎</span>' : ''}
|
||||
${(item.type === 'bitmap' || item.type === 'shape') ? `<input class="iwb-layer-color" type="color" value="${item.color}" data-color-id="${item.id}" data-color-type="${item.type}" title="调整颜色">` : ''}
|
||||
${(item.type === 'bitmap' || item.type === 'shape') ? `<input class="iwb-layer-width" type="number" min="1" max="300" value="${Math.round(item.lineWidth)}" data-width-id="${item.id}" data-width-type="${item.type}" title="调整边框粗细">` : ''}
|
||||
<button class="iwb-layer-lock${item.locked ? ' active' : ''}" data-lock-id="${item.id}" data-lock-type="${item.type}" title="${item.locked ? '解锁图层' : '锁定图层'}">${lockSvg}</button>
|
||||
${item.canDelete ? `<button class="iwb-layer-del" data-del-id="${item.id}" data-del-type="${item.type}" title="删除图层">${delSvg}</button>` : ''}
|
||||
<div class="iwb-layer-row">
|
||||
<button class="iwb-layer-vis" data-vis-id="${item.id}" data-vis-type="${item.type}" title="${item.visible ? '隐藏' : '显示'}">${visSvg}</button>
|
||||
<span class="iwb-layer-thumb"><canvas width="44" height="44" data-thumb-id="${item.id}" data-thumb-type="${item.type}"></canvas><span class="iwb-layer-icon">${typeIconSvg}</span></span>
|
||||
<span class="iwb-layer-name${canRename ? ' renamable' : ''}" data-name-id="${item.id}" data-name-type="${item.type}" title="${esc(item.name)}${canRename ? ' · 双击重命名' : ''}">${esc(item.name)}</span>
|
||||
${item.paintTarget ? '<span class="iwb-layer-paint" title="当前画笔 / 橡皮作用图层">✎</span>' : ''}
|
||||
${(pct < 100 && !item.active) ? `<span class="iwb-layer-op" title="不透明度 ${pct}%(选中该层可调)">${pct}%</span>` : ''}
|
||||
<button class="iwb-layer-lock${item.locked ? ' active' : ''}" data-lock-id="${item.id}" data-lock-type="${item.type}" title="${item.locked ? '解锁图层' : '锁定图层'}">${lockSvg}</button>
|
||||
${item.canDelete ? `<button class="iwb-layer-del" data-del-id="${item.id}" data-del-type="${item.type}" title="删除图层">${delSvg}</button>` : ''}
|
||||
</div>
|
||||
${item.active ? `<div class="iwb-layer-op-row">
|
||||
<input class="iwb-layer-opacity" type="range" min="0" max="100" step="1" value="${pct}" data-op-id="${item.id}" data-op-type="${item.type}" title="不透明度:拖动调整,双击复位 100%">
|
||||
<span class="iwb-layer-op-val">${pct}%</span>
|
||||
</div>` : ''}
|
||||
</div>`
|
||||
}
|
||||
list.innerHTML = html
|
||||
scheduleThumbs()
|
||||
|
||||
// 侧栏「快速效果」的目标层名:效果和画笔共用同一个目标层,这里一起刷,
|
||||
// 免得切层后标签还停在上一个图层名上。
|
||||
@@ -618,7 +667,8 @@ export function setActiveLayer(id) {
|
||||
if (!list) return
|
||||
|
||||
bindLayerItemEvents(list)
|
||||
bindLayerStyleInputs(list)
|
||||
bindLayerNameRename(list)
|
||||
bindLayerOpacityInputs(list)
|
||||
bindLayerVisButtons(list)
|
||||
bindLayerLockButtons(list)
|
||||
bindLayerDelButtons(list)
|
||||
@@ -635,6 +685,8 @@ export function setActiveLayer(id) {
|
||||
item.addEventListener('drop', (e) => { e.preventDefault(); reorderPanelItem(e.dataTransfer.getData('text/plain'), item.dataset.layerId) })
|
||||
item.addEventListener('click', (e) => {
|
||||
if (e.target.closest('.iwb-layer-vis') || e.target.closest('.iwb-layer-lock') || e.target.closest('.iwb-layer-del')) return
|
||||
// 拖动不透明度滑杆 / 正在改名:都不能触发整块重绘,否则控件节点会被换掉
|
||||
if (e.target.closest('.iwb-layer-opacity') || e.target.closest('.iwb-layer-name-input') || e.target.closest('.iwb-layer-op-row')) return
|
||||
const id = item.dataset.layerId
|
||||
const type = item.dataset.layerType
|
||||
// ⌘/Ctrl 点击:加入/移出合并集合(不改变画笔目标与单选高亮)
|
||||
@@ -657,32 +709,142 @@ export function setActiveLayer(id) {
|
||||
})
|
||||
}
|
||||
|
||||
/** 图层颜色与边框粗细输入:修改后立即刷新画布和面板 */
|
||||
function bindLayerStyleInputs(list) {
|
||||
// 图层颜色与边框粗细:修改后立即刷新画布和面板。
|
||||
list.querySelectorAll('.iwb-layer-color').forEach(input => {
|
||||
input.addEventListener('mousedown', (e) => e.stopPropagation())
|
||||
input.addEventListener('input', (e) => {
|
||||
/**
|
||||
* 双击图层名重命名(v1.7)。
|
||||
*
|
||||
* 为什么自己算双击而不是用 dblclick:点一下图层名就会 setActiveLayer → renderLayerPanel
|
||||
* 重建整块 innerHTML,第 2 次点击落在新节点上,浏览器不会再派发 dblclick。
|
||||
* 这里用「两次 mousedown 间隔 <400ms 且同一个图层」自行判定,重绘不影响识别。
|
||||
*/
|
||||
function bindLayerNameRename(list) {
|
||||
list.querySelectorAll('.iwb-layer-name.renamable').forEach(el => {
|
||||
el.addEventListener('mousedown', (e) => {
|
||||
e.stopPropagation()
|
||||
const id = input.dataset.colorId
|
||||
const type = input.dataset.colorType
|
||||
const obj = type === 'bitmap' ? S.layers.find(l => l.id === id) : S.shapeLayers.find(s => s.id === id)
|
||||
if (obj) { obj.color = input.value; S.color = input.value; updateColorUI(); renderObjects(); renderLayerPanel(); S.pickerComposite = null }
|
||||
const id = el.dataset.nameId
|
||||
const now = Date.now()
|
||||
const prev = S.lastNameClick
|
||||
if (prev && prev.id === id && now - prev.t < 400) {
|
||||
S.lastNameClick = null
|
||||
beginRename(id)
|
||||
return
|
||||
}
|
||||
S.lastNameClick = { id, t: now }
|
||||
})
|
||||
})
|
||||
list.querySelectorAll('.iwb-layer-width').forEach(input => {
|
||||
}
|
||||
|
||||
/** 把图层名的 span 换成输入框;Enter / 失焦提交,Esc 取消 */
|
||||
function beginRename(id) {
|
||||
const list = runtime.el.querySelector('.iwb-editor-layer-list')
|
||||
const layer = S.layers.find(l => l.id === id)
|
||||
if (!list || !layer) return
|
||||
const item = list.querySelector('.iwb-layer-item[data-layer-id="' + id + '"]')
|
||||
const span = item && item.querySelector('.iwb-layer-name')
|
||||
if (!span || !span.parentNode) return
|
||||
|
||||
const input = document.createElement('input')
|
||||
input.className = 'iwb-layer-name-input'
|
||||
input.type = 'text'
|
||||
input.value = layer.name
|
||||
input.maxLength = 40
|
||||
span.replaceWith(input)
|
||||
// 重命名期间禁用整项拖动,否则在输入框里拖选文字会变成拖动图层排序
|
||||
item.draggable = false
|
||||
|
||||
let done = false
|
||||
const finish = (commit) => {
|
||||
if (done) return
|
||||
done = true
|
||||
const next = input.value.trim()
|
||||
if (commit && next && next !== layer.name) {
|
||||
saveSnapshot('重命名图层')
|
||||
layer.name = next
|
||||
}
|
||||
renderLayerPanel()
|
||||
}
|
||||
input.addEventListener('mousedown', (e) => e.stopPropagation())
|
||||
input.addEventListener('click', (e) => e.stopPropagation())
|
||||
input.addEventListener('keydown', (e) => {
|
||||
e.stopPropagation()
|
||||
if (e.key === 'Enter') { e.preventDefault(); finish(true) }
|
||||
else if (e.key === 'Escape') { e.preventDefault(); finish(false) }
|
||||
})
|
||||
input.addEventListener('blur', () => finish(true))
|
||||
input.focus()
|
||||
input.select()
|
||||
}
|
||||
|
||||
/**
|
||||
* 不透明度滑杆(v1.7)。拖动期间绝不能调用 renderLayerPanel():
|
||||
* 它会把整块 innerHTML 换掉,滑杆节点被销毁 → 拖动直接中断(这也是老代码里
|
||||
* 「改完颜色取色器就断开、数字框打不进两位」的根因,那些控件现在已删)。
|
||||
* 所以这里只改对象 + 就地刷标值,一次拖动只拍一张快照。
|
||||
*/
|
||||
function bindLayerOpacityInputs(list) {
|
||||
list.querySelectorAll('.iwb-layer-opacity').forEach(input => {
|
||||
input.addEventListener('mousedown', (e) => e.stopPropagation())
|
||||
input.addEventListener('click', (e) => e.stopPropagation())
|
||||
// 双击滑块复位 100%(PS 里双击「不透明度」标题即可复位,这里沿用同一习惯)
|
||||
input.addEventListener('dblclick', (e) => {
|
||||
e.stopPropagation()
|
||||
input.value = '100'
|
||||
applyOpacityFromInput(input)
|
||||
})
|
||||
input.addEventListener('input', (e) => {
|
||||
e.stopPropagation()
|
||||
const id = input.dataset.widthId
|
||||
const type = input.dataset.widthType
|
||||
const value = clamp(parseInt(input.value, 10) || 1, 1, 300)
|
||||
const obj = type === 'bitmap' ? S.layers.find(l => l.id === id) : S.shapeLayers.find(s => s.id === id)
|
||||
if (obj) { obj.lineWidth = value; if (type === 'shape') obj.lw = value; else { S.brushSize = value; refreshBrushRing() } renderObjects(); updateBrushParamUI(); renderLayerPanel() }
|
||||
applyOpacityFromInput(input)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function applyOpacityFromInput(input) {
|
||||
const type = input.dataset.opType
|
||||
const obj = findPanelTarget(type, input.dataset.opId)
|
||||
if (!obj) return
|
||||
const pct = clamp(parseInt(input.value, 10) || 0, 0, 100)
|
||||
if (!input._opSnapTaken) {
|
||||
// 第一格拖动才拍快照:一次拖动 = 一次撤销,不会产生几十张全幅像素快照
|
||||
input._opSnapTaken = true
|
||||
saveSnapshot('调整不透明度')
|
||||
}
|
||||
obj.opacity = pct / 100
|
||||
applyOpacityVisual(type, obj)
|
||||
S.pickerComposite = null
|
||||
const val = input.parentNode && input.parentNode.querySelector('.iwb-layer-op-val')
|
||||
if (val) val.textContent = pct + '%'
|
||||
}
|
||||
|
||||
/** 缩略图:合并 90ms 内的多次请求,避免连续选中/拖动时反复整块重画 */
|
||||
let _thumbTimer = 0
|
||||
function scheduleThumbs() {
|
||||
if (_thumbTimer) return
|
||||
_thumbTimer = setTimeout(() => { _thumbTimer = 0; paintThumbs() }, 90)
|
||||
}
|
||||
|
||||
/** 把每个图层画布缩到 22px 缩略图(设备像素 44,避免高分屏发虚) */
|
||||
function paintThumbs() {
|
||||
if (!runtime.el) return
|
||||
const list = runtime.el.querySelector('.iwb-editor-layer-list')
|
||||
if (!list) return
|
||||
list.querySelectorAll('.iwb-layer-thumb canvas').forEach(cv => {
|
||||
const type = cv.dataset.thumbType
|
||||
const obj = findPanelTarget(type, cv.dataset.thumbId)
|
||||
const src = obj ? (type === 'bitmap' ? obj.canvas : obj._canvas) : null
|
||||
const g = cv.getContext('2d')
|
||||
if (!g) return
|
||||
g.clearRect(0, 0, cv.width, cv.height)
|
||||
if (!src || !src.width || !src.height) return
|
||||
// 等比缩放到缩略框内并居中:背景是透明格子时不会变形
|
||||
const k = Math.min(cv.width / src.width, cv.height / src.height)
|
||||
const dw = Math.max(1, Math.round(src.width * k))
|
||||
const dh = Math.max(1, Math.round(src.height * k))
|
||||
try {
|
||||
g.imageSmoothingQuality = 'high'
|
||||
g.drawImage(src, Math.round((cv.width - dw) / 2), Math.round((cv.height - dh) / 2), dw, dh)
|
||||
} catch (e) { /* 画布已释放等异常不阻断其余缩略图 */ }
|
||||
})
|
||||
}
|
||||
|
||||
/** 显隐切换按钮:位图直接切 visible,文字/形状切对象 visible 并重绘 */
|
||||
function bindLayerVisButtons(list) {
|
||||
// 显隐切换
|
||||
@@ -776,7 +938,9 @@ export function setActiveLayer(id) {
|
||||
export function clearLayerMulti() { S.layerMulti.length = 0 }
|
||||
|
||||
/** 把一组单元按 z 从低到高叠进目标位图层的像素。
|
||||
* alpha 规则与 getCompositeCanvas(导出/吸管)保持一致,保证「合并后看到的就是导出结果」。 */
|
||||
* alpha 规则与 getCompositeCanvas(导出/吸管)保持一致,保证「合并后看到的就是导出结果」。
|
||||
* 只有位图层的 opacity 需要在合成时乘 globalAlpha —— 文字/形状的不透明度在
|
||||
* drawVectorToCanvas / drawShape 里已经烘进像素,这里再乘一次就是双倍透明。 */
|
||||
function composeUnitsInto(layer, units) {
|
||||
const tmp = document.createElement('canvas')
|
||||
tmp.width = S.imgW
|
||||
@@ -785,7 +949,7 @@ export function setActiveLayer(id) {
|
||||
for (const u of units) {
|
||||
const el = u.kind === 'bitmap' ? u.obj.canvas : u.obj._canvas
|
||||
if (!el || el.nodeType !== 1) continue
|
||||
tctx.globalAlpha = u.obj.opacity == null ? 1 : u.obj.opacity
|
||||
tctx.globalAlpha = u.kind === 'bitmap' ? (u.obj.opacity == null ? 1 : u.obj.opacity) : 1
|
||||
tctx.drawImage(el, 0, 0)
|
||||
}
|
||||
tctx.globalAlpha = 1
|
||||
|
||||
Reference in new issue
Block a user