/* effects.js - 侧栏「快速效果」(v1.4) * * 作用对象只有一个:当前位图图层(侧栏标题后面实时显示层名)。 * 全部走 canvas 2D 的 ctx.filter / 缩放重采样,不引入依赖、不动矢量层、 * 不改图层几何,因此对「原图」这种带 locked 标记的层也安全(效果不是画笔)。 * * 撤销:应用前 saveSnapshot()。base 与画笔层走像素快照(putImageData), * 天然可回退;导入图片层走 image/sourceCanvas 重绘,原地改像素会被下一次 * 重绘冲掉,所以先按 PS「栅格化」的做法落一个普通位图副本(原层隐藏)。 */ 'use strict' import { S } from './state.js' import { saveSnapshot } from './history.js' import { createLayerCanvas, getActiveLayer, renderLayerPanel, reorderLayerCanvas, updateLayerCanvasStyles } from './layers.js' import { layerUnits } from './objects.js' import { needsPixelSnapshot } from '../vendor/layer-logic.js' import { toast } from './commands.js' /** 效果表:label 用于按钮与提示,css 可依赖画布尺寸(柔化半径随图缩放) */ export const QUICK_EFFECTS = { invert: { label: '反白', hint: '反相:明暗翻转,白底变黑底', css: () => 'invert(1)' }, gray: { label: '灰度', hint: '去色,保留明暗层次', css: () => 'grayscale(1)' }, boost: { label: '增强', hint: '提亮并略加对比', css: () => 'brightness(1.12) contrast(1.1) saturate(1.06)' }, contrast: { label: '高对比', hint: '压成黑白硬调', css: () => 'grayscale(1) contrast(2.4)' }, soft: { label: '柔化', hint: '轻微模糊,弱化背景', css: (c) => 'blur(' + softRadius(c) + 'px)' }, mosaic: { label: '马赛克', hint: '像素化,用于打码', pixel: true } } const SOFT_MIN = 0.8 const SOFT_MAX = 8 function softRadius(canvas) { const side = Math.min(canvas.width || 0, canvas.height || 0) return Math.max(SOFT_MIN, Math.min(SOFT_MAX, Math.round(side / 500 * 10) / 10)) } function mosaicBlock(canvas) { const side = Math.min(canvas.width || 0, canvas.height || 0) return Math.max(6, Math.round(side / 60)) } /** 效果目标:当前活跃位图图层;没有就退回「原图」 */ export function effectTarget() { const active = getActiveLayer() if (active) return active return S.layers.find(l => l.id === 'base') || null } /** 用 ctx.filter 把画布自身重绘一遍(临时画布中转,避免自绘自的采样问题) */ function filterCanvas(canvas, css) { const w = canvas.width, h = canvas.height if (!w || !h || !css) return const tmp = document.createElement('canvas') tmp.width = w tmp.height = h const tctx = tmp.getContext('2d') tctx.filter = css tctx.drawImage(canvas, 0, 0) const ctx = canvas.getContext('2d') ctx.save() // 图层 ctx 平时带着视图变换,重绘必须回到设备像素坐标系 ctx.setTransform(1, 0, 0, 1, 0, 0) ctx.globalAlpha = 1 ctx.globalCompositeOperation = 'copy' // copy:连透明像素一起替换,不留旧内容 ctx.filter = 'none' ctx.drawImage(tmp, 0, 0) ctx.restore() } /** 马赛克:缩到 1/block 再关掉平滑放大,比逐像素 getImageData 快一个量级 */ function pixelateCanvas(canvas, block) { const w = canvas.width, h = canvas.height if (!w || !h) return const sw = Math.max(1, Math.round(w / block)) const sh = Math.max(1, Math.round(h / block)) const tmp = document.createElement('canvas') tmp.width = sw tmp.height = sh const tctx = tmp.getContext('2d') tctx.imageSmoothingEnabled = true tctx.drawImage(canvas, 0, 0, sw, sh) const ctx = canvas.getContext('2d') ctx.save() ctx.setTransform(1, 0, 0, 1, 0, 0) ctx.globalAlpha = 1 ctx.globalCompositeOperation = 'copy' ctx.imageSmoothingEnabled = false ctx.filter = 'none' ctx.drawImage(tmp, 0, 0, w, h) ctx.restore() } /** * 把「导入图片层」栅格化成普通位图,插在它上面一层。 * 只拷当前 canvas 的可见结果(已含旋转/缩放),所以视觉不变。 * 原层隐藏而不删除:撤销时快照里有它的 visible,能原样恢复。 */ function rasterizeLayer(layer) { const canvas = createLayerCanvas() const ctx = canvas.getContext('2d', { willReadFrequently: true }) ctx.clearRect(0, 0, S.imgW, S.imgH) ctx.drawImage(layer.canvas, 0, 0) // z 取「原层 → 上一层」的中点,保证副本紧贴在原层上方,不会窜到别的标注之上 const units = layerUnits() const i = units.findIndex(u => u.kind === 'bitmap' && u.obj === layer) const baseZ = layer.z || 0 const nextZ = (i >= 0 && units[i + 1]) ? (units[i + 1].obj.z || 0) : baseZ + 1 const z = nextZ > baseZ ? (baseZ + nextZ) / 2 : baseZ + 0.5 const clone = { id: 'fx' + 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 } S.layers.push(clone) layer.visible = false if (S.selectedLayerId === layer.id) S.selectedLayerId = clone.id S.activeLayerId = clone.id reorderLayerCanvas() updateLayerCanvasStyles() renderLayerPanel() return clone } /** 唯一入口:应用一个快速效果 */ export function applyQuickEffect(key) { const fx = QUICK_EFFECTS[key] if (!fx) return false const layer = effectTarget() if (!layer) { toast('没有可处理的图层'); return false } if (layer.visible === false) { toast('「' + layer.name + '」已隐藏,先显示再应用效果') return false } saveSnapshot('效果:' + fx.label) let target = layer if (!needsPixelSnapshot(layer)) target = rasterizeLayer(layer) if (!target) { toast('栅格化失败,效果未应用'); return false } if (fx.pixel) pixelateCanvas(target.canvas, mosaicBlock(target.canvas)) else filterCanvas(target.canvas, fx.css(target.canvas)) target._bounds = undefined S.pickerComposite = null if (S.renderAll) S.renderAll() renderLayerPanel() toast('已应用「' + fx.label + '」到 ' + target.name + ' · Ctrl+Z 撤销', 'success') return true }