527 lines
21 KiB
JavaScript
527 lines
21 KiB
JavaScript
/* adjust.js - 图片调节(v1.15)
|
||
*
|
||
* 和 effects.js 的分工:
|
||
* effects.js = 一键效果(反白/灰度/马赛克……),语义是「换成另一种图」;
|
||
* adjust.js = 连续调色(曝光/对比度/白平衡……),语义是「把这张图调好看」。
|
||
* 两者都作用在当前位图图层,都保留 source 只重算不叠加。
|
||
*
|
||
* === 为什么不用 ctx.filter ===
|
||
* CSS filter 只有 brightness/contrast/saturate/hue-rotate/blur 这几种全局线性变换,做不到:
|
||
* · 阴影 / 高光 —— 需要按亮度做软蒙版分区提亮压暗;
|
||
* · 白平衡温度 / 色调 —— 需要 R/B、G/M 通道分别增益;
|
||
* · 锐度 / 清晰度 —— USM 反遮罩,需要卷积。
|
||
* 所以走 getImageData 逐像素。代价是耗时随像素数线性增长(实测):
|
||
* 800² 6.7ms · 1200² 12.8ms · 2000² 36.8ms · 4000² 144.6ms
|
||
* 因此拖动滑杆时用「降采样预览」(长边 ≤ PREVIEW_SIDE,一张小图反复算),
|
||
* 松手(change)才用原尺寸算一次。这是 PS 的常规做法,观感上只在松手瞬间有一帧停顿。
|
||
*
|
||
* === 非破坏性怎么保证 ===
|
||
* S.adjust.source 存「开始调节前」的原尺寸像素,preview 存它的降采样副本。
|
||
* 每次参数变化都从 source 重算,绝不在上一次结果上叠加 —— 否则来回拖滑杆会
|
||
* 层层累积、越拖越糊(effects.js 里同样的坑,注释里记过)。
|
||
*/
|
||
|
||
'use strict'
|
||
|
||
import { S, runtime } from './state.js'
|
||
import { saveSnapshot } from './history.js'
|
||
import { getActiveLayer, renderLayerPanel } from './layers.js'
|
||
import { needsPixelSnapshot } from '../vendor/layer-logic.js'
|
||
import { toast } from './commands.js'
|
||
|
||
/** 拖动预览用的最长边:0.6M 像素,实测 6.7ms,够 60fps */
|
||
const PREVIEW_SIDE = 900
|
||
/** 图本身小于这个边长就无需降采样,直接全量算 */
|
||
const DIRECT_SIDE = 1100
|
||
|
||
/**
|
||
* 参数表。范围按「拉满要有明显变化」定,不是摆设:
|
||
* 曝光 ±100 = ±2 档(×4 / ÷4),对比度 ±100 = 0~2 倍,饱和度 ±100 = 0~2 倍。
|
||
* 上一版快速效果「增强」拉满只有 brightness(1.12),用户反馈「效果弱」就是这么来的。
|
||
*/
|
||
export const ADJUST_DEFS = [
|
||
{ key: 'exposure', label: '曝光', min: -100, max: 100, step: 1, def: 0 },
|
||
{ key: 'brightness', label: '明度', min: -100, max: 100, step: 1, def: 0 },
|
||
{ key: 'contrast', label: '对比度', min: -100, max: 100, step: 1, def: 0 },
|
||
{ key: 'shadows', label: '阴影', min: -100, max: 100, step: 1, def: 0 },
|
||
{ key: 'highlights', label: '高光', min: -100, max: 100, step: 1, def: 0 },
|
||
{ key: 'hue', label: '色相', min: -180, max: 180, step: 1, def: 0, suffix: '°' },
|
||
{ key: 'saturation', label: '饱和度', min: -100, max: 100, step: 1, def: 0 },
|
||
{ key: 'temp', label: '温度', title: '白平衡温度:往右偏暖(黄),往左偏冷(蓝)', min: -100, max: 100, step: 1, def: 0 },
|
||
{ key: 'tint', label: '色调', title: '白平衡色调:往右偏品红,往左偏绿', min: -100, max: 100, step: 1, def: 0 },
|
||
{ key: 'sharpen', label: '锐度', min: 0, max: 100, step: 1, def: 0 },
|
||
{ key: 'clarity', label: '清晰度', min: -100, max: 100, step: 1, def: 0 }
|
||
]
|
||
|
||
export const ADJUST_KEYS = ADJUST_DEFS.map(d => d.key)
|
||
|
||
export function zeroAdjust() {
|
||
const o = {}
|
||
for (const k of ADJUST_KEYS) o[k] = 0
|
||
return o
|
||
}
|
||
|
||
/** 全部参数都是 0 = 没调过,直接跳过整趟像素运算 */
|
||
export function isNeutral(p) {
|
||
if (!p) return true
|
||
for (const k of ADJUST_KEYS) if (p[k]) return false
|
||
return true
|
||
}
|
||
|
||
/** 调节目标:当前活跃位图图层;没有就退回「原图」(与 effects.js 同规则) */
|
||
function adjustTarget() {
|
||
const active = getActiveLayer()
|
||
if (active) return active
|
||
return S.layers.find(l => l.id === 'base') || null
|
||
}
|
||
|
||
function copyCanvas(src, maxSide) {
|
||
const w0 = src.width, h0 = src.height
|
||
let w = w0, h = h0
|
||
if (maxSide && Math.max(w0, h0) > maxSide) {
|
||
const k = maxSide / Math.max(w0, h0)
|
||
w = Math.max(1, Math.round(w0 * k))
|
||
h = Math.max(1, Math.round(h0 * k))
|
||
}
|
||
const c = document.createElement('canvas')
|
||
c.width = w
|
||
c.height = h
|
||
const ctx = c.getContext('2d', { willReadFrequently: true })
|
||
ctx.imageSmoothingEnabled = true
|
||
ctx.imageSmoothingQuality = 'high'
|
||
ctx.drawImage(src, 0, 0, w, h)
|
||
return c
|
||
}
|
||
|
||
/* ---------------------------------------------------------------- 像素调色 */
|
||
|
||
/**
|
||
* 逐像素处理。顺序有讲究:
|
||
* 白平衡 → 曝光 → 阴影/高光 → 对比度 → 饱和度 → 色相
|
||
* 先修色偏再谈明暗,先把动态范围拉开再调饱和,否则「先加饱和后压对比」会把
|
||
* 颜色压成一片灰。色相放最后,因为它作用在已经很干净的色相上。
|
||
*/
|
||
function adjustPixels(ctx, w, h, p) {
|
||
const img = ctx.getImageData(0, 0, w, h)
|
||
const d = img.data
|
||
|
||
const ev = p.exposure ? Math.pow(2, p.exposure / 50) : 1
|
||
const add = (p.brightness / 100) * 0.45
|
||
const con = 1 + p.contrast / 100
|
||
const sh = p.shadows / 100
|
||
const hi = p.highlights / 100
|
||
const sat = 1 + p.saturation / 100
|
||
const wbR = 1 + p.temp / 250 + p.tint / 500
|
||
const wbG = 1 - p.tint / 400
|
||
const wbB = 1 - p.temp / 250 + p.tint / 500
|
||
const wb = Boolean(p.temp || p.tint)
|
||
const needSh = Boolean(sh)
|
||
const needHi = Boolean(hi)
|
||
const needCon = Boolean(p.contrast)
|
||
const needSat = Boolean(p.saturation)
|
||
const needHue = Boolean(p.hue)
|
||
|
||
// 色相旋转矩阵(与 CSS hue-rotate 同一套系数),预算好避免每像素算三角函数
|
||
let h0 = 0, h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0, h6 = 0, h7 = 0, h8 = 0
|
||
if (needHue) {
|
||
const a = (p.hue * Math.PI) / 180
|
||
const c = Math.cos(a), s = Math.sin(a)
|
||
h0 = 0.213 + c * 0.787 - s * 0.213
|
||
h1 = 0.715 - c * 0.715 - s * 0.715
|
||
h2 = 0.072 - c * 0.072 + s * 0.928
|
||
h3 = 0.213 - c * 0.213 + s * 0.143
|
||
h4 = 0.715 + c * 0.285 + s * 0.140
|
||
h5 = 0.072 - c * 0.072 - s * 0.283
|
||
h6 = 0.213 - c * 0.213 - s * 0.787
|
||
h7 = 0.715 - c * 0.715 + s * 0.715
|
||
h8 = 0.072 + c * 0.928 + s * 0.072
|
||
}
|
||
|
||
// 全部为 0 的短路:只做一次遍历判断,省掉逐像素分支
|
||
const trivial = !wb && !p.exposure && !add && !needSh && !needHi && !needCon && !needSat && !needHue
|
||
if (trivial) return
|
||
|
||
for (let i = 0; i < d.length; i += 4) {
|
||
let r = d[i] / 255, g = d[i + 1] / 255, b = d[i + 2] / 255
|
||
|
||
if (wb) { r *= wbR; g *= wbG; b *= wbB }
|
||
if (ev !== 1) { r *= ev; g *= ev; b *= ev }
|
||
if (add) { r += add; g += add; b += add }
|
||
|
||
// 阴影/高光:按当前亮度给一个软蒙版。阴影权重 (1-l)²,高光 l²,
|
||
// 这样中间调几乎不动,只有暗部/亮部两端被推 —— 硬阈值会在边界留下可见台阶。
|
||
if (needSh || needHi) {
|
||
const l = 0.2126 * r + 0.7152 * g + 0.0722 * b
|
||
if (needSh) {
|
||
const m = (1 - l) * (1 - l) * sh * 0.85
|
||
r += m; g += m; b += m
|
||
}
|
||
if (needHi) {
|
||
const m = l * l * hi * 0.85
|
||
r += m; g += m; b += m
|
||
}
|
||
}
|
||
|
||
if (needCon) {
|
||
r = (r - 0.5) * con + 0.5
|
||
g = (g - 0.5) * con + 0.5
|
||
b = (b - 0.5) * con + 0.5
|
||
}
|
||
|
||
if (needSat) {
|
||
const l = 0.2126 * r + 0.7152 * g + 0.0722 * b
|
||
r = l + (r - l) * sat
|
||
g = l + (g - l) * sat
|
||
b = l + (b - l) * sat
|
||
}
|
||
|
||
if (needHue) {
|
||
const nr = h0 * r + h1 * g + h2 * b
|
||
const ng = h3 * r + h4 * g + h5 * b
|
||
const nb = h6 * r + h7 * g + h8 * b
|
||
r = nr; g = ng; b = nb
|
||
}
|
||
|
||
d[i] = r <= 0 ? 0 : (r >= 1 ? 255 : r * 255)
|
||
d[i + 1] = g <= 0 ? 0 : (g >= 1 ? 255 : g * 255)
|
||
d[i + 2] = b <= 0 ? 0 : (b >= 1 ? 255 : b * 255)
|
||
}
|
||
ctx.putImageData(img, 0, 0)
|
||
}
|
||
|
||
/* ---------------------------------------------------------------- 锐化 */
|
||
|
||
/**
|
||
* USM 反遮罩:result = src + (src - blur) * amount。
|
||
* 锐度用 1px 半径(找边缘),清晰度用更大的半径 + 更低强度(找中间调的体积感)。
|
||
* 降采样预览时半径要跟着缩放,否则 900px 的预览用 1px 和原图 1px 视觉上不是一回事。
|
||
*/
|
||
function unsharp(canvas, radius, amount) {
|
||
if (!(amount > 0)) return
|
||
const w = canvas.width, h = canvas.height
|
||
const blur = document.createElement('canvas')
|
||
blur.width = w
|
||
blur.height = h
|
||
const bc = blur.getContext('2d', { willReadFrequently: true })
|
||
bc.filter = 'blur(' + radius.toFixed(2) + 'px)'
|
||
bc.drawImage(canvas, 0, 0)
|
||
|
||
const ctx = canvas.getContext('2d', { willReadFrequently: true })
|
||
const src = ctx.getImageData(0, 0, w, h)
|
||
const bl = bc.getImageData(0, 0, w, h)
|
||
const a = src.data, b = bl.data
|
||
for (let i = 0; i < a.length; i += 4) {
|
||
// 透明像素不参与(位图层的空白区域),否则边缘会被锐出一圈黑边
|
||
if (a[i + 3] === 0) continue
|
||
for (let c = 0; c < 3; c++) {
|
||
const v = a[i + c] + (a[i + c] - b[i + c]) * amount
|
||
a[i + c] = v <= 0 ? 0 : (v >= 255 ? 255 : v)
|
||
}
|
||
}
|
||
ctx.putImageData(src, 0, 0)
|
||
}
|
||
|
||
/* ---------------------------------------------------------------- 渲染 */
|
||
|
||
/** 把参数应用到一张画布上(画布尺寸决定锐化半径的缩放) */
|
||
function paint(canvas, p, refSide) {
|
||
const ctx = canvas.getContext('2d', { willReadFrequently: true })
|
||
adjustPixels(ctx, canvas.width, canvas.height, p)
|
||
|
||
const k = Math.max(canvas.width, canvas.height) / (refSide || Math.max(canvas.width, canvas.height))
|
||
if (p.sharpen > 0) unsharp(canvas, Math.max(0.6, 1 * k), (p.sharpen / 100) * 1.1)
|
||
if (p.clarity) {
|
||
// 负清晰度 = 柔化中间调,同样走 USM,只是 amount 为负
|
||
unsharp(canvas, Math.max(1.5, 6 * k), (p.clarity / 100) * 0.55)
|
||
}
|
||
}
|
||
|
||
function blit(target, src) {
|
||
const ctx = target.getContext('2d')
|
||
ctx.save()
|
||
ctx.setTransform(1, 0, 0, 1, 0, 0)
|
||
ctx.globalAlpha = 1
|
||
ctx.globalCompositeOperation = 'copy'
|
||
ctx.filter = 'none'
|
||
ctx.imageSmoothingEnabled = true
|
||
ctx.imageSmoothingQuality = 'high'
|
||
ctx.drawImage(src, 0, 0, target.width, target.height)
|
||
ctx.restore()
|
||
target._bounds = undefined
|
||
S.pickerComposite = null
|
||
}
|
||
|
||
/**
|
||
* 从 source 重算并写回目标层。
|
||
* preview=true 走降采样:拖动滑杆时只要「看得出往哪边变」,不需要像素级准确。
|
||
*/
|
||
function renderAdjust(preview) {
|
||
const st = S.adjust
|
||
if (!st || !st.source) return false
|
||
const target = S.layers.find(l => l.id === st.layerId)
|
||
if (!target) return false
|
||
|
||
const src = preview && st.previewCanvas ? st.previewCanvas : st.source
|
||
const work = copyCanvas(src)
|
||
paint(work, st.params, Math.max(st.source.width, st.source.height))
|
||
blit(target.canvas, work)
|
||
return true
|
||
}
|
||
|
||
/* ---------------------------------------------------------------- 对外 */
|
||
|
||
/**
|
||
* 每层的「调节前基准像素」缓存。
|
||
* 为什么需要它:A 层调过 → 切到 B 层调 → 切回 A 层再调。此时 A 的 canvas 里已经
|
||
* 是调节后的像素,若拿它当新基准,第二次调整就叠在第一次结果上(来回拖会越调越脏,
|
||
* effects.js 注释里记过同样的坑)。缓存不进图层对象,因此不会被历史快照深拷贝。
|
||
*/
|
||
const sourceCache = new Map()
|
||
|
||
function captureFor(layer) {
|
||
const hit = sourceCache.get(layer.id)
|
||
if (hit) return hit
|
||
const rec = {
|
||
source: copyCanvas(layer.canvas, 0),
|
||
previewCanvas: copyCanvas(layer.canvas, PREVIEW_SIDE)
|
||
}
|
||
sourceCache.set(layer.id, rec)
|
||
return rec
|
||
}
|
||
|
||
/**
|
||
* 退出编辑器 / 重新打开时清空基准缓存(v1.15.1 修)。
|
||
*
|
||
* 必须清的原因:缓存的 key 是 layer.id,而原图层的 id 是写死的 'base'。
|
||
* 退出编辑器时 releaseEditorMemory() 释放了图层画布,却没清这张 Map,
|
||
* 于是下一张图打开后 captureFor() 直接命中上一张图的像素,
|
||
* 表现为「调第二张图,画面变回第一张」。id 固定 ⇒ 100% 复现,不是偶发。
|
||
*
|
||
* 顺带把 S.adjust 也归零:残留的 params 会让新图的滑杆一进来就显示 +80,
|
||
* 而画面是未调状态,参数与画面对不上。
|
||
*/
|
||
export function resetAdjustCache() {
|
||
sourceCache.clear()
|
||
S.adjust = { layerId: null, source: null, previewCanvas: null, params: zeroAdjust() }
|
||
}
|
||
|
||
/** 观测口:回归用例用它断言「退出后缓存确实清空了」 */
|
||
export function adjustCacheSize() {
|
||
return sourceCache.size
|
||
}
|
||
|
||
/** 切换调节目标:旧层基准入缓存,新层取缓存或现拍,参数按层恢复 */
|
||
function switchTarget(layer) {
|
||
const st = S.adjust
|
||
if (st && st.layerId && st.source) {
|
||
sourceCache.set(st.layerId, { source: st.source, previewCanvas: st.previewCanvas })
|
||
}
|
||
const rec = captureFor(layer)
|
||
S.adjust = {
|
||
layerId: layer.id,
|
||
source: rec.source,
|
||
previewCanvas: rec.previewCanvas,
|
||
params: layer.adjustParams ? { ...layer.adjustParams } : zeroAdjust()
|
||
}
|
||
}
|
||
|
||
/** 参数写回图层对象:切层回来能显示「这层调成了什么样」 */
|
||
function persistParams() {
|
||
const st = S.adjust
|
||
if (!st || !st.layerId || !st.params) return
|
||
const layer = S.layers.find(l => l.id === st.layerId)
|
||
if (layer) layer.adjustParams = { ...st.params }
|
||
}
|
||
|
||
/** 确保当前目标层有一份可重算的基准像素 */
|
||
export function ensureAdjustTarget() {
|
||
let layer = adjustTarget()
|
||
if (!layer) { toast('没有可调节的图层'); return null }
|
||
if (layer.visible === false) {
|
||
toast('「' + layer.name + '」已隐藏,先显示再调节')
|
||
return null
|
||
}
|
||
|
||
if (!needsPixelSnapshot(layer)) layer = rasterizeForAdjust(layer)
|
||
if (!layer) { toast('栅格化失败,调节未应用'); return null }
|
||
|
||
if (!S.adjust || S.adjust.layerId !== layer.id || !S.adjust.source) switchTarget(layer)
|
||
return layer
|
||
}
|
||
|
||
/** 导入图片层原地改像素会被下次重绘冲掉 —— 先复制一层普通位图(与 effects.js 同策略) */
|
||
function rasterizeForAdjust(layer) {
|
||
const canvas = document.createElement('canvas')
|
||
canvas.width = S.imgW
|
||
canvas.height = S.imgH
|
||
const ctx = canvas.getContext('2d', { willReadFrequently: true })
|
||
ctx.drawImage(layer.canvas, 0, 0)
|
||
|
||
const copy = {
|
||
id: 'adj' + Date.now() + Math.floor(Math.random() * 1000),
|
||
name: layer.name + ' · 调节',
|
||
canvas, ctx,
|
||
opacity: layer.opacity == null ? 1 : layer.opacity,
|
||
visible: true, locked: false,
|
||
x: 0, y: 0, w: S.imgW, h: S.imgH,
|
||
rotation: 0,
|
||
z: (layer.z || 0) + 0.5
|
||
}
|
||
S.layers.push(copy)
|
||
layer.visible = false
|
||
if (S.selectedLayerId === layer.id) S.selectedLayerId = copy.id
|
||
S.activeLayerId = copy.id
|
||
renderLayerPanel()
|
||
return copy
|
||
}
|
||
|
||
/** 参数变了:重算并画到画布上。preview=true 用降采样 */
|
||
export function applyAdjust(preview) {
|
||
if (!S.adjust || !S.adjust.source) return false
|
||
const ok = renderAdjust(Boolean(preview))
|
||
if (ok) {
|
||
persistParams()
|
||
if (S.renderAll) S.renderAll()
|
||
}
|
||
renderAdjustPanel()
|
||
return ok
|
||
}
|
||
|
||
/** 单个参数变化 */
|
||
export function setAdjustValue(key, value, preview) {
|
||
const st = S.adjust
|
||
if (!st || !st.source) return false
|
||
const def = ADJUST_DEFS.find(d => d.key === key)
|
||
if (!def) return false
|
||
const v = Math.min(def.max, Math.max(def.min, Number(value) || 0))
|
||
st.params[key] = v
|
||
applyAdjust(preview)
|
||
return true
|
||
}
|
||
|
||
/** 复位:单条 or 全部 */
|
||
export function resetAdjust(key) {
|
||
const st = S.adjust
|
||
if (!st || !st.source) return false
|
||
saveSnapshot('调节复位')
|
||
if (key) st.params[key] = 0
|
||
else st.params = zeroAdjust()
|
||
applyAdjust(false)
|
||
syncAdjustControls()
|
||
return true
|
||
}
|
||
|
||
/** 有没有调过(用于「复位」按钮的可用态与标题提示) */
|
||
export function adjustDirty() {
|
||
return Boolean(S.adjust && S.adjust.source && !isNeutral(S.adjust.params))
|
||
}
|
||
|
||
/* ---------------------------------------------------------------- UI */
|
||
|
||
/**
|
||
* 按 ADJUST_DEFS 生成 11 行控件,只做一次(面板是静态的,之后只改 value / 类名)。
|
||
* 单行紧凑布局:名称 46px + 滑杆 flex + 数值 36px —— 240px 侧栏里 11 项只占约 240px 高,
|
||
* 不需要把侧栏撑成一整屏滚动。
|
||
*/
|
||
export function mountAdjustPanel() {
|
||
if (!runtime.el) return
|
||
const list = runtime.el.querySelector('.iwb-editor-adj-list')
|
||
if (!list || list.childElementCount) return
|
||
list.innerHTML = ADJUST_DEFS.map(d => `
|
||
<div class="iwb-editor-adj-row" data-adj="${d.key}">
|
||
<span class="iwb-editor-adj-name" title="${d.title || d.label}${d.suffix ? '' : ''}(双击归零)">${d.label}</span>
|
||
<input type="range" class="iwb-editor-adj-range" min="${d.min}" max="${d.max}" step="${d.step}" value="${d.def}" aria-label="${d.title || d.label}">
|
||
<input type="number" class="iwb-editor-adj-val" min="${d.min}" max="${d.max}" step="${d.step}" value="${d.def}" aria-label="${d.title || d.label}数值">
|
||
</div>`).join('')
|
||
renderAdjustPanel()
|
||
}
|
||
|
||
/** 面板里的 target 层名 + 复位按钮可用态 */
|
||
export function renderAdjustPanel() {
|
||
if (!runtime.el) return
|
||
const sec = runtime.el.querySelector('.iwb-editor-adj')
|
||
if (!sec) return
|
||
const layer = adjustTarget()
|
||
|
||
// 目标层被切走:面板跟着换到新层(参数按层恢复、新的基准),旧层的像素不动。
|
||
// 必须在这里兜住 —— 图层切换走的是 layers.js,那边不能 import 本模块(会形成循环)。
|
||
if (layer && S.adjust && S.adjust.layerId && S.adjust.layerId !== layer.id) {
|
||
switchTarget(layer)
|
||
syncAdjustControls()
|
||
return
|
||
}
|
||
|
||
const nameEl = sec.querySelector('.iwb-editor-adj-target')
|
||
if (nameEl) {
|
||
const name = layer ? layer.name : '无图层'
|
||
nameEl.textContent = name
|
||
nameEl.title = layer ? '调节作用于「' + name + '」' : '没有可调节的位图图层'
|
||
}
|
||
const dirty = adjustDirty()
|
||
sec.classList.toggle('is-dirty', dirty)
|
||
const reset = sec.querySelector('[data-adj-reset="all"]')
|
||
if (reset) reset.disabled = !dirty
|
||
sec.querySelectorAll('.iwb-editor-adj-row').forEach(row => {
|
||
const k = row.dataset.adj
|
||
const v = S.adjust && S.adjust.params ? S.adjust.params[k] || 0 : 0
|
||
row.classList.toggle('is-set', Boolean(v))
|
||
})
|
||
}
|
||
|
||
/** 把 S.adjust.params 写回所有滑杆与数值框(复位后调用) */
|
||
export function syncAdjustControls() {
|
||
if (!runtime.el) return
|
||
const sec = runtime.el.querySelector('.iwb-editor-adj')
|
||
if (!sec || !S.adjust || !S.adjust.params) return
|
||
sec.querySelectorAll('.iwb-editor-adj-row').forEach(row => {
|
||
const k = row.dataset.adj
|
||
const v = S.adjust.params[k] || 0
|
||
const range = row.querySelector('.iwb-editor-adj-range')
|
||
const val = row.querySelector('.iwb-editor-adj-val')
|
||
if (range) range.value = String(v)
|
||
if (val) val.value = String(v)
|
||
})
|
||
renderAdjustPanel()
|
||
}
|
||
|
||
/** 进入编辑器 / 切换目标层时重建基准 */
|
||
export function refreshAdjustTarget() {
|
||
// 只在目标层真的变了才重置基准,否则每次重绘都丢参数
|
||
const layer = adjustTarget()
|
||
if (!layer) return
|
||
const st = S.adjust
|
||
if (st.source && st.layerId === layer.id) return
|
||
S.adjust = { layerId: null, source: null, previewCanvas: null, params: zeroAdjust() }
|
||
renderAdjustPanel()
|
||
syncAdjustControls()
|
||
}
|
||
|
||
/**
|
||
* 撤销 / 重做之后。
|
||
*
|
||
* 为什么必须归零:调整是「写进像素」的破坏性操作,历史里只存了像素快照,没存
|
||
* 「调节前的基准」。撤销把像素还原了,但 source 对应的已经不是这个状态 —— 留着
|
||
* 旧参数只会出现「滑块显示 +80、画面其实没调」。更糟的是下一次拖动会从错误的
|
||
* 基准重算,等于把调整叠加两遍。
|
||
*
|
||
* 所以规则很简单也很可预测:**撤销一步 = 调整作废,滑块归零,基准重拍当前像素**。
|
||
* 想再调就从现在这张画面上重新调。
|
||
*/
|
||
export function onHistoryRestore() {
|
||
sourceCache.clear()
|
||
const layer = adjustTarget()
|
||
if (!layer) {
|
||
S.adjust = { layerId: null, source: null, previewCanvas: null, params: zeroAdjust() }
|
||
syncAdjustControls()
|
||
return
|
||
}
|
||
if (layer.adjustParams) delete layer.adjustParams
|
||
S.adjust = {
|
||
layerId: layer.id,
|
||
source: copyCanvas(layer.canvas, 0),
|
||
previewCanvas: copyCanvas(layer.canvas, PREVIEW_SIDE),
|
||
params: zeroAdjust()
|
||
}
|
||
syncAdjustControls()
|
||
}
|