feat(image-tools): 消除实时预览 + 智能消除笔(橡皮升级)
- 实时预览:点 OCR 框/选框松手瞬间即在画布显示消除效果(preview 层 直接盖绘合成图,蓝标「实时预览已生成」+「预览(未应用)」角标), 「✓ 应用消除」落图层 /「✕ 取消预览」还原;Escape、重框选自动清预览 - 智能消除笔:橡皮在底图(非画笔层)上涂抹→红色笔迹实时跟随, 松手以笔迹为蒙版 computeInpaint 并立即应用,无需框选/OCR/按钮 - computeInpaint 拆分为纯计算(支持外部笔迹蒙版),showPreview/ cancelPreview/applyPreview 三态;消除层置顶(盖住文字/色块层) - 导出前自动应用未确认预览(所见即所得);大区域迭代预算防卡顿 - 实测:OCR 点框预览→应用→导出蓝字 0;选框松手预览(2432→801 为 蚂蚁线 UI)→取消恢复→应用导出红字 0;消除笔涂抹→松手即应用导出 0
This commit is contained in:
1 parent
72e06b24fe
commit
d9d306956d
5 files changed
+293
-81
No files matched your search
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. See [standa
|
||||
|
||||
## Version 2026.10.16-it-tools-pro
|
||||
|
||||
### ✨ 消除实时预览 + 智能消除笔(橡皮升级)
|
||||
- **实时预览**:点 OCR 文字框或选框松手的**瞬间**,画布立即显示消除后的效果(蓝标「👁 实时预览已生成」+「预览(未应用)」角标),满意点「✓ 应用消除」落图层,不满意「✕ 取消预览」还原——先看效果再确认,不再是盲操作
|
||||
- **智能消除笔(橡皮 E 升级)**:直接在底图的文字/水印上涂抹(红色半透明笔迹实时跟随),**松手立即无痕消除**,无需框选、无需 OCR、无需点按钮;选中画笔层时仍是传统擦除
|
||||
- 消除层改为置顶图层(盖住底图及其上的文字/色块);导出前自动应用未确认预览(所见即所得);Escape/重框选自动清预览
|
||||
- 大区域消除加迭代预算(防卡顿)
|
||||
|
||||
### ✨ 图片编辑:智能消除(无痕删除图中文字/水印)
|
||||
- **OCR 路径**:识别文字后点框 →「🧹 消除文字」自动识别文字笔画像素,从周边背景扩散重建(inpainting),渐变/纹理背景自然补全,不再是纯色覆盖
|
||||
- **选框路径**:框选任意区域(文字/水印/杂物)→「🧹 消除选区内容」一键无痕消除,无需 OCR
|
||||
|
||||
+1
-1
@@ -310,7 +310,7 @@ tools:
|
||||
|
||||
image-tools:
|
||||
title: Image tools
|
||||
description: "In-browser image toolbox: compress, convert, resize, crop, rotate, filters, mosaic/redaction, Photoshop-style editor (layers, brush, text, selection, OCR text replacement, content-aware erase), EXIF viewer, QR generate/scan, watermark and stitching. Images never leave your device."
|
||||
description: "In-browser image toolbox: compress, convert, resize, crop, rotate, filters, mosaic/redaction, Photoshop-style editor (layers, brush, text, selection, OCR text replacement, content-aware erase with live preview), EXIF viewer, QR generate/scan, watermark and stitching. Images never leave your device."
|
||||
|
||||
pdf-tools:
|
||||
title: PDF tools
|
||||
|
||||
+1
-1
@@ -307,7 +307,7 @@ tools:
|
||||
|
||||
image-tools:
|
||||
title: 图片工具
|
||||
description: 浏览器本地处理的图片工具箱:图片压缩、格式转换、调整尺寸、裁剪、旋转、滤镜、打码、PS 风格图片编辑(图层/画笔/文字/选区/OCR 改字/智能消除无痕去文字水印)、EXIF 查看、二维码生成/识别、加水印、多图拼接,图片全程不上传。
|
||||
description: 浏览器本地处理的图片工具箱:图片压缩、格式转换、调整尺寸、裁剪、旋转、滤镜、打码、PS 风格图片编辑(图层/画笔/文字/选区/OCR 改字/智能消除笔实时预览无痕去文字水印)、EXIF 查看、二维码生成/识别、加水印、多图拼接,图片全程不上传。
|
||||
|
||||
pdf-tools:
|
||||
title: PDF工具
|
||||
|
||||
+1
-1
@@ -53,7 +53,7 @@ const groups: { title: string; items: Item[] }[] = [
|
||||
{ name: '多图拼接', icon: '拼', desc: '横向/纵向长图/九宫格/瀑布流/交错双列/拍立得 6 种方式,网格类 2-4 列可调,间距可调', tag: '改造' },
|
||||
{ name: 'JSON 美化和格式化', icon: 'J', desc: '双栏专业工作台:左侧行号编辑,右侧树视图/语法高亮切换,含 Dict/Get参数/XML/CSV 等 7 种转换', tag: '改造' },
|
||||
{ name: 'PDF工具', icon: 'P', desc: '合并/拆分/旋转/重排/压缩/加密/解密/水印/提取文本/页码/转图片,11 功能全程浏览器本地处理,需授权使用', tag: '授权' },
|
||||
{ name: '图片工具', icon: '图', desc: '压缩/转换/尺寸/裁剪/旋转/滤镜/打码/PS 风格编辑器(图层+画笔+文字+选区+历史记录+OCR 改字/智能消除无痕去文字水印)/EXIF/二维码生成识别/水印/拼接,13 功能本地处理,需授权使用', tag: '授权' },
|
||||
{ name: '图片工具', icon: '图', desc: '压缩/转换/尺寸/裁剪/旋转/滤镜/打码/PS 风格编辑器(图层+画笔+文字+选区+历史记录+OCR 改字/智能消除/消除笔实时预览无痕去文字水印)/EXIF/二维码生成识别/水印/拼接,13 功能本地处理,需授权使用', tag: '授权' },
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
@@ -40,7 +40,7 @@ const TOOLS: { key: Tool; icon: string; title: string }[] = [
|
||||
{ key: 'move', icon: '✥', title: '移动 (V):拖动文字/色块图层,或拖动当前画笔层' },
|
||||
{ key: 'marquee', icon: '▦', title: '选框 (M):框选区域后可填充/清除' },
|
||||
{ key: 'brush', icon: '🖌', title: '画笔 (B)' },
|
||||
{ key: 'eraser', icon: '⬜', title: '橡皮 (E):在画笔层上擦除' },
|
||||
{ key: 'eraser', icon: '🩹', title: '智能消除笔 (E):直接涂文字/水印,松手即无痕消除;选中画笔层时擦除笔迹' },
|
||||
{ key: 'text', icon: 'T', title: '文字 (T):点击画布放置文字层' },
|
||||
{ key: 'fill', icon: '🪣', title: '油漆桶 (G):点击相近颜色区域填充' },
|
||||
{ key: 'eyedropper', icon: '💧', title: '吸管 (I):取色' },
|
||||
@@ -66,7 +66,12 @@ const hover = ref<{ x: number; y: number } | null>(null);
|
||||
let resizeObs: ResizeObserver | null = null;
|
||||
|
||||
const statusHint = computed(() => {
|
||||
if (tool.value === 'brush' || tool.value === 'eraser') {
|
||||
if (tool.value === 'eraser') {
|
||||
return activeLayer.value?.type === 'paint'
|
||||
? `画笔层擦除:大小 ${brushSize.value}px,按 [ ] 调整`
|
||||
: `智能消除笔:在文字/水印上涂抹,松手立即无痕消除(${brushSize.value}px,[ ] 调整)`;
|
||||
}
|
||||
if (tool.value === 'brush') {
|
||||
return `大小 ${brushSize.value}px,按 [ ] 调整`;
|
||||
}
|
||||
if (tool.value === 'move') {
|
||||
@@ -121,6 +126,7 @@ function serializeState(): string {
|
||||
}
|
||||
|
||||
async function restoreState(s: string): Promise<void> {
|
||||
preview.value = null;
|
||||
const st = JSON.parse(s);
|
||||
docW.value = st.w;
|
||||
docH.value = st.h;
|
||||
@@ -153,6 +159,7 @@ async function restoreState(s: string): Promise<void> {
|
||||
}
|
||||
|
||||
function commit(label: string): void {
|
||||
preview.value = null; // 图层已变化,旧预览作废
|
||||
hist.value = hist.value.slice(0, hIdx.value + 1);
|
||||
hist.value.push({ label, state: serializeState() });
|
||||
if (hist.value.length > HIST_MAX) {
|
||||
@@ -302,6 +309,10 @@ function render(): void {
|
||||
if (compCanvas) {
|
||||
ctx.drawImage(compCanvas, 0, 0);
|
||||
}
|
||||
// 实时消除预览(未应用前直接盖在合成图上)
|
||||
if (preview.value) {
|
||||
ctx.drawImage(preview.value.canvas, preview.value.x, preview.value.y);
|
||||
}
|
||||
ctx.strokeStyle = 'rgba(0,0,0,0.45)';
|
||||
ctx.lineWidth = 1 / zoom.value;
|
||||
ctx.strokeRect(0, 0, docW.value, docH.value);
|
||||
@@ -325,6 +336,31 @@ function render(): void {
|
||||
}
|
||||
ctx.restore();
|
||||
// 屏幕空间叠加层
|
||||
// 预览区边框(提示“这里正在实时预览消除结果”)
|
||||
if (preview.value) {
|
||||
const pv = preview.value;
|
||||
ctx.strokeStyle = '#2563eb';
|
||||
ctx.lineWidth = 1.5;
|
||||
ctx.setLineDash([5, 4]);
|
||||
ctx.strokeRect(panX.value + pv.x * zoom.value, panY.value + pv.y * zoom.value, pv.w * zoom.value, pv.h * zoom.value);
|
||||
ctx.setLineDash([]);
|
||||
ctx.font = '11px sans-serif';
|
||||
ctx.fillStyle = '#2563eb';
|
||||
const pt = '预览(未应用)';
|
||||
const ptw = ctx.measureText(pt).width + 10;
|
||||
const pty = panY.value + pv.y * zoom.value - 17;
|
||||
ctx.fillRect(panX.value + pv.x * zoom.value, pty < 0 ? panY.value + pv.y * zoom.value : pty, ptw, 15);
|
||||
ctx.fillStyle = '#fff';
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.fillText(pt, panX.value + pv.x * zoom.value + 5, (pty < 0 ? panY.value + pv.y * zoom.value : pty) + 8);
|
||||
}
|
||||
// 智能消除笔拖动中的笔迹(红色半透明)
|
||||
if (healOn && healCanvas) {
|
||||
ctx.save();
|
||||
ctx.globalAlpha = 0.55;
|
||||
ctx.drawImage(healCanvas, panX.value, panY.value, docW.value * zoom.value, docH.value * zoom.value);
|
||||
ctx.restore();
|
||||
}
|
||||
ctx.strokeStyle = '#d03050';
|
||||
ctx.lineWidth = 1.5;
|
||||
if (selection.value) {
|
||||
@@ -431,6 +467,13 @@ function toDoc(e: PointerEvent | MouseEvent): { x: number; y: number } {
|
||||
|
||||
// ---------- 选区 / 拖拽框 ----------
|
||||
const selection = ref<{ x: number; y: number; w: number; h: number } | null>(null);
|
||||
// 实时预览:消除结果先渲染到画布(不落图层),点「✓ 应用」才写入
|
||||
const preview = ref<{ x: number; y: number; w: number; h: number; canvas: HTMLCanvasElement; label: string; hitId?: string } | null>(null);
|
||||
// 智能消除笔(橡皮底图模式):拖动中的笔迹蒙版
|
||||
let healCanvas: HTMLCanvasElement | null = null;
|
||||
let healOn = false;
|
||||
let healBBox = { x0: 0, y0: 0, x1: 0, y1: 0 };
|
||||
let healLast: { x: number; y: number } | null = null;
|
||||
const dragRect = ref<{ x: number; y: number; w: number; h: number } | null>(null);
|
||||
let dragStart: { x: number; y: number } | null = null;
|
||||
|
||||
@@ -439,16 +482,21 @@ function normRect(a: { x: number; y: number }, b: { x: number; y: number }): { x
|
||||
}
|
||||
|
||||
// ---------- 选区操作 ----------
|
||||
function createPaintLayer(name: string): PaintLayer {
|
||||
function createPaintLayer(name: string, toTop = false): PaintLayer {
|
||||
const c = document.createElement('canvas');
|
||||
c.width = docW.value;
|
||||
c.height = docH.value;
|
||||
const pl: PaintLayer = { id: uid(), type: 'paint', name, visible: true, opacity: 100, ox: 0, oy: 0, canvas: c };
|
||||
let idx = layers.value.findIndex(v => v.type === 'image');
|
||||
if (idx < 0) {
|
||||
idx = layers.value.length - 1;
|
||||
if (toTop) {
|
||||
layers.value.push(pl); // 消除结果必须盖住底图和其上的文字/色块
|
||||
}
|
||||
else {
|
||||
let idx = layers.value.findIndex(v => v.type === 'image');
|
||||
if (idx < 0) {
|
||||
idx = layers.value.length - 1;
|
||||
}
|
||||
layers.value.splice(idx + 1, 0, pl);
|
||||
}
|
||||
layers.value.splice(idx + 1, 0, pl);
|
||||
activeLayerId.value = pl.id;
|
||||
return pl;
|
||||
}
|
||||
@@ -552,15 +600,12 @@ function inpaintSelection(): void {
|
||||
emit('error', '请先框选要消除的区域(不小于 4×4)');
|
||||
return;
|
||||
}
|
||||
const ok = inpaintRect(s);
|
||||
if (ok) {
|
||||
selection.value = null;
|
||||
commit('智能消除选区');
|
||||
composite();
|
||||
scheduleRender();
|
||||
const res = computeInpaint(s);
|
||||
if (res) {
|
||||
showPreview(res, '智能消除选区');
|
||||
}
|
||||
else {
|
||||
emit('error', '未检出可消除的内容(区域与背景一致),可改用填充/画笔');
|
||||
emit('error', '选区内未检出可消除的内容(与背景一致),可改用填充/画笔');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -830,6 +875,7 @@ function applyHitReplace(): void {
|
||||
if (!hit || hit.used) {
|
||||
return;
|
||||
}
|
||||
cancelPreview(); // 替换是另一条路径,先撤掉消除预览
|
||||
const color = sampleBoxColor(hit);
|
||||
const padX = Math.max(1, Math.round(hit.w * 0.02));
|
||||
const padY = Math.max(1, Math.round(hit.h * 0.08));
|
||||
@@ -852,7 +898,23 @@ function applyHitErase(): void {
|
||||
if (!hit || hit.used) {
|
||||
return;
|
||||
}
|
||||
// 智能消除:识别笔画像素,从周边背景扩散重建(inpainting)
|
||||
// 已有该框的实时预览 → 直接应用;否则计算并立即应用(兼容按钮直点)
|
||||
if (preview.value && preview.value.hitId === hit.id) {
|
||||
applyPreview();
|
||||
return;
|
||||
}
|
||||
const rect = hitInpaintRect(hit);
|
||||
const res = computeInpaint(rect);
|
||||
if (!res) {
|
||||
emit('error', '该框内未检出可消除的文字');
|
||||
return;
|
||||
}
|
||||
showPreview(res, 'OCR 消除文字', hit.id);
|
||||
applyPreview();
|
||||
}
|
||||
|
||||
// OCR 文字框 → 消除用矩形(四周少量外扩)
|
||||
function hitInpaintRect(hit: OcrHit): { x: number; y: number; w: number; h: number } {
|
||||
const padX = Math.max(2, Math.round(hit.w * 0.04));
|
||||
const padY = Math.max(2, Math.round(hit.h * 0.1));
|
||||
const rect = {
|
||||
@@ -863,24 +925,16 @@ function applyHitErase(): void {
|
||||
};
|
||||
rect.w = Math.min(rect.w, docW.value - rect.x);
|
||||
rect.h = Math.min(rect.h, docH.value - rect.y);
|
||||
const ok = inpaintRect(rect);
|
||||
if (!ok) {
|
||||
return;
|
||||
}
|
||||
hit.used = true;
|
||||
selHitId.value = null;
|
||||
commit('OCR 消除文字');
|
||||
composite();
|
||||
scheduleRender();
|
||||
return rect;
|
||||
}
|
||||
|
||||
// ---------- 智能消除(inpainting) ----------
|
||||
// 1) 取矩形区域(含外扩边距)像素;2) 与周边背景色差异大的判为文字笔画;
|
||||
// 3) 笔画像素由四周已知像素迭代扩散重建(Gauss-Seidel),背景连续纹理/渐变都能自然补上;
|
||||
// 4) 只把重建像素写入一个新的「智能消除」画笔层,非破坏性、可撤销、可再编辑。
|
||||
function inpaintRect(rect: { x: number; y: number; w: number; h: number }): boolean {
|
||||
// 纯计算:识别文字笔画(或用外部笔迹蒙版),从周边已知像素扩散重建该区域,
|
||||
// 返回只含重建像素的透明画布(不写图层),供「实时预览」和「应用」共用。
|
||||
function computeInpaint(rect: { x: number; y: number; w: number; h: number }, strokeMask?: Uint8Array | null):
|
||||
{ x: number; y: number; w: number; h: number; canvas: HTMLCanvasElement } | null {
|
||||
if (!compCanvas || rect.w < 2 || rect.h < 2) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
const rw = Math.round(rect.w);
|
||||
const rh = Math.round(rect.h);
|
||||
@@ -888,51 +942,61 @@ function inpaintRect(rect: { x: number; y: number; w: number; h: number }): bool
|
||||
const ry = Math.round(rect.y);
|
||||
const src = compCanvas.getContext('2d', { willReadFrequently: true })!.getImageData(rx, ry, rw, rh);
|
||||
const d = src.data;
|
||||
// 1) 背景色:区域外圈 2px 环带的中位色
|
||||
const rs: number[] = [];
|
||||
const gs: number[] = [];
|
||||
const bs: number[] = [];
|
||||
for (let y = 0; y < rh; y++) {
|
||||
for (let x = 0; x < rw; x++) {
|
||||
const onRing = x < 2 || y < 2 || x >= rw - 2 || y >= rh - 2;
|
||||
if (!onRing) {
|
||||
continue;
|
||||
}
|
||||
const i = (y * rw + x) * 4;
|
||||
rs.push(d[i]);
|
||||
gs.push(d[i + 1]);
|
||||
bs.push(d[i + 2]);
|
||||
}
|
||||
}
|
||||
if (!rs.length) {
|
||||
return false;
|
||||
}
|
||||
const med = (a: number[]) => {
|
||||
a.sort((p, q) => p - q);
|
||||
return a[a.length >> 1];
|
||||
};
|
||||
const bgR = med(rs);
|
||||
const bgG = med(gs);
|
||||
const bgB = med(bs);
|
||||
// 2) 笔画掩码:与背景色差异大的像素(全区域检测,含边缘字符)
|
||||
const mask = new Uint8Array(rw * rh);
|
||||
let unknown = 0;
|
||||
const thr = 42;
|
||||
for (let y = 0; y < rh; y++) {
|
||||
for (let x = 0; x < rw; x++) {
|
||||
const p = y * rw + x;
|
||||
const i = p * 4;
|
||||
const dist = Math.abs(d[i] - bgR) + Math.abs(d[i + 1] - bgG) + Math.abs(d[i + 2] - bgB);
|
||||
if (dist > thr) {
|
||||
if (strokeMask && strokeMask.length === rw * rh) {
|
||||
// 外部笔迹蒙版(智能消除笔):涂哪消哪
|
||||
for (let p = 0; p < rw * rh; p++) {
|
||||
if (strokeMask[p]) {
|
||||
mask[p] = 1;
|
||||
unknown++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!unknown) {
|
||||
return false; // 没检出文字,无需处理
|
||||
else {
|
||||
// 自动检测:取区域外圈 2px 环带中位色作背景参考,差异大的判为笔画
|
||||
const rs: number[] = [];
|
||||
const gs: number[] = [];
|
||||
const bs: number[] = [];
|
||||
for (let y = 0; y < rh; y++) {
|
||||
for (let x = 0; x < rw; x++) {
|
||||
const onRing = x < 2 || y < 2 || x >= rw - 2 || y >= rh - 2;
|
||||
if (!onRing) {
|
||||
continue;
|
||||
}
|
||||
const i = (y * rw + x) * 4;
|
||||
rs.push(d[i]);
|
||||
gs.push(d[i + 1]);
|
||||
bs.push(d[i + 2]);
|
||||
}
|
||||
}
|
||||
if (!rs.length) {
|
||||
return null;
|
||||
}
|
||||
const med = (a: number[]) => {
|
||||
a.sort((p, q) => p - q);
|
||||
return a[a.length >> 1];
|
||||
};
|
||||
const bgR = med(rs);
|
||||
const bgG = med(gs);
|
||||
const bgB = med(bs);
|
||||
const thr = 42;
|
||||
for (let y = 0; y < rh; y++) {
|
||||
for (let x = 0; x < rw; x++) {
|
||||
const p = y * rw + x;
|
||||
const i = p * 4;
|
||||
const dist = Math.abs(d[i] - bgR) + Math.abs(d[i + 1] - bgG) + Math.abs(d[i + 2] - bgB);
|
||||
if (dist > thr) {
|
||||
mask[p] = 1;
|
||||
unknown++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!unknown) {
|
||||
return null; // 没检出文字
|
||||
}
|
||||
}
|
||||
// 掩码向外扩 1px,把抗锯齿的半透明边缘也盖住(带边界检查)
|
||||
// 掩码向外扩 1px,覆盖抗锯齿边缘(带边界检查)
|
||||
const grown = mask.slice();
|
||||
for (let y = 0; y < rh; y++) {
|
||||
for (let x = 0; x < rw; x++) {
|
||||
@@ -946,7 +1010,7 @@ function inpaintRect(rect: { x: number; y: number; w: number; h: number }): bool
|
||||
}
|
||||
}
|
||||
}
|
||||
// 3) 扩散重建:未知像素反复取四邻均值(迭代次数随区域尺寸自适应)
|
||||
// 扩散重建:未知像素反复取四邻均值(交替扫描消偏置;大区域降迭代防卡顿)
|
||||
const buf = new Float32Array(rw * rh * 3);
|
||||
for (let p = 0; p < rw * rh; p++) {
|
||||
const i = p * 4;
|
||||
@@ -954,9 +1018,9 @@ function inpaintRect(rect: { x: number; y: number; w: number; h: number }): bool
|
||||
buf[p * 3 + 1] = d[i + 1];
|
||||
buf[p * 3 + 2] = d[i + 2];
|
||||
}
|
||||
const iters = Math.min(500, Math.max(rw, rh) * 2);
|
||||
const budget = Math.min(500, Math.max(rw, rh) * 2);
|
||||
const iters = Math.min(budget, Math.max(40, Math.floor(12_000_000 / Math.max(1, unknown))));
|
||||
for (let it = 0; it < iters; it++) {
|
||||
// 交替扫描方向,消除顺序偏置
|
||||
const rev = it % 2 === 1;
|
||||
for (let yy = 0; yy < rh; yy++) {
|
||||
const y = rev ? rh - 1 - yy : yy;
|
||||
@@ -1006,7 +1070,7 @@ function inpaintRect(rect: { x: number; y: number; w: number; h: number }): bool
|
||||
}
|
||||
}
|
||||
}
|
||||
// 4) 写入独立画笔层(只含重建区域,其余透明)
|
||||
// 只保留重建像素,其余透明
|
||||
const out = new ImageData(rw, rh);
|
||||
const od = out.data;
|
||||
for (let p = 0; p < rw * rh; p++) {
|
||||
@@ -1023,9 +1087,46 @@ function inpaintRect(rect: { x: number; y: number; w: number; h: number }): bool
|
||||
tmp.width = rw;
|
||||
tmp.height = rh;
|
||||
tmp.getContext('2d')!.putImageData(out, 0, 0);
|
||||
const pl = createPaintLayer('智能消除');
|
||||
pl.canvas.getContext('2d')!.drawImage(tmp, rx, ry);
|
||||
return true;
|
||||
return { x: rx, y: ry, w: rw, h: rh, canvas: tmp };
|
||||
}
|
||||
|
||||
// 实时预览:把计算结果显示在画布上(不写图层),等用户确认
|
||||
function showPreview(res: { x: number; y: number; w: number; h: number; canvas: HTMLCanvasElement }, label: string, hitId?: string): void {
|
||||
preview.value = hitId ? { ...res, label, hitId } : { ...res, label };
|
||||
scheduleRender();
|
||||
}
|
||||
|
||||
function cancelPreview(): void {
|
||||
if (!preview.value) {
|
||||
return;
|
||||
}
|
||||
preview.value = null;
|
||||
scheduleRender();
|
||||
}
|
||||
|
||||
// 应用预览:写入「智能消除」画笔层并进历史
|
||||
function applyPreview(): void {
|
||||
const pv = preview.value;
|
||||
if (!pv) {
|
||||
return;
|
||||
}
|
||||
const pl = createPaintLayer('智能消除', true);
|
||||
pl.canvas.getContext('2d')!.drawImage(pv.canvas, pv.x, pv.y);
|
||||
const hitId = pv.hitId;
|
||||
if (hitId) {
|
||||
const hit = ocrHits.value.find(h => h.id === hitId);
|
||||
if (hit) {
|
||||
hit.used = true;
|
||||
}
|
||||
if (selHitId.value === hitId) {
|
||||
selHitId.value = null;
|
||||
hitText.value = '';
|
||||
}
|
||||
}
|
||||
preview.value = null;
|
||||
commit(pv.label);
|
||||
composite();
|
||||
scheduleRender();
|
||||
}
|
||||
|
||||
function ocrHitTest(dx: number, dy: number): OcrHit | null {
|
||||
@@ -1076,6 +1177,12 @@ function onDown(e: PointerEvent): void {
|
||||
if (oh) {
|
||||
selHitId.value = oh.id;
|
||||
hitText.value = '';
|
||||
// 点框即预览:文字立刻“消失”,满意点✓应用,不满意可换字或取消
|
||||
cancelPreview();
|
||||
const res = computeInpaint(hitInpaintRect(oh));
|
||||
if (res) {
|
||||
showPreview(res, 'OCR 消除文字', oh.id);
|
||||
}
|
||||
scheduleRender();
|
||||
return;
|
||||
}
|
||||
@@ -1104,10 +1211,29 @@ function onDown(e: PointerEvent): void {
|
||||
dragRect.value = { x: p.x, y: p.y, w: 0, h: 0 };
|
||||
if (tool.value === 'marquee') {
|
||||
selection.value = null;
|
||||
cancelPreview();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (tool.value === 'brush' || tool.value === 'eraser') {
|
||||
const al = activeLayer.value;
|
||||
if (tool.value === 'eraser' && (!al || al.type !== 'paint')) {
|
||||
// 智能消除笔:直接在底图/文字/色块上涂,松手即从周边背景重建
|
||||
cancelPreview();
|
||||
healOn = true;
|
||||
healCanvas = document.createElement('canvas');
|
||||
healCanvas.width = docW.value;
|
||||
healCanvas.height = docH.value;
|
||||
healLast = { x: p.x, y: p.y };
|
||||
healBBox = { x0: p.x, y0: p.y, x1: p.x, y1: p.y };
|
||||
const hctx = healCanvas.getContext('2d')!;
|
||||
hctx.fillStyle = '#fff';
|
||||
hctx.beginPath();
|
||||
hctx.arc(p.x, p.y, Math.max(1, brushSize.value / 2), 0, Math.PI * 2);
|
||||
hctx.fill();
|
||||
scheduleRender();
|
||||
return;
|
||||
}
|
||||
const pl = ensurePaintLayer();
|
||||
const ctx = pl.canvas.getContext('2d')!;
|
||||
strokeLast = { x: p.x - pl.ox, y: p.y - pl.oy };
|
||||
@@ -1156,6 +1282,24 @@ function onMove(e: PointerEvent): void {
|
||||
scheduleRender();
|
||||
return;
|
||||
}
|
||||
if (healOn && healCanvas && healLast) {
|
||||
const hctx = healCanvas.getContext('2d')!;
|
||||
hctx.strokeStyle = '#fff';
|
||||
hctx.lineWidth = Math.max(1, brushSize.value);
|
||||
hctx.lineCap = 'round';
|
||||
hctx.lineJoin = 'round';
|
||||
hctx.beginPath();
|
||||
hctx.moveTo(healLast.x, healLast.y);
|
||||
hctx.lineTo(p.x, p.y);
|
||||
hctx.stroke();
|
||||
healLast = { x: p.x, y: p.y };
|
||||
healBBox.x0 = Math.min(healBBox.x0, p.x);
|
||||
healBBox.y0 = Math.min(healBBox.y0, p.y);
|
||||
healBBox.x1 = Math.max(healBBox.x1, p.x);
|
||||
healBBox.y1 = Math.max(healBBox.y1, p.y);
|
||||
scheduleRender();
|
||||
return;
|
||||
}
|
||||
if (dragStart) {
|
||||
dragRect.value = normRect(dragStart, p);
|
||||
scheduleRender();
|
||||
@@ -1186,6 +1330,34 @@ function onUp(): void {
|
||||
panning = false;
|
||||
return;
|
||||
}
|
||||
if (healOn && healCanvas) {
|
||||
// 松手即消除:笔迹作蒙版,笔迹包围盒内从周边背景重建并立即应用
|
||||
healOn = false;
|
||||
healLast = null;
|
||||
const pad = brushSize.value / 2 + 4;
|
||||
const x0 = Math.max(0, Math.floor(healBBox.x0 - pad));
|
||||
const y0 = Math.max(0, Math.floor(healBBox.y0 - pad));
|
||||
const x1 = Math.min(docW.value, Math.ceil(healBBox.x1 + pad));
|
||||
const y1 = Math.min(docH.value, Math.ceil(healBBox.y1 + pad));
|
||||
const w = x1 - x0;
|
||||
const h = y1 - y0;
|
||||
const hc = healCanvas;
|
||||
healCanvas = null;
|
||||
if (w >= 4 && h >= 4) {
|
||||
const img = hc.getContext('2d')!.getImageData(x0, y0, w, h);
|
||||
const mask = new Uint8Array(w * h);
|
||||
for (let i = 0; i < w * h; i++) {
|
||||
mask[i] = img.data[i * 4 + 3] > 0 ? 1 : 0;
|
||||
}
|
||||
const res = computeInpaint({ x: x0, y: y0, w, h }, mask);
|
||||
if (res) {
|
||||
showPreview(res, '智能消除笔');
|
||||
applyPreview(); // 松手立即生效 = 实时效果
|
||||
}
|
||||
}
|
||||
scheduleRender();
|
||||
return;
|
||||
}
|
||||
if (moving) {
|
||||
moving = null;
|
||||
commit('移动图层');
|
||||
@@ -1197,6 +1369,14 @@ function onUp(): void {
|
||||
if (r.w >= 4 && r.h >= 4) {
|
||||
if (tool.value === 'marquee') {
|
||||
selection.value = r;
|
||||
// 松手即算:实时显示消除后的效果,用户确认后才落图层
|
||||
const res = computeInpaint(r);
|
||||
if (res) {
|
||||
showPreview(res, '智能消除选区');
|
||||
}
|
||||
else {
|
||||
preview.value = null;
|
||||
}
|
||||
}
|
||||
// crop 保留 dragRect 供「应用裁剪」
|
||||
}
|
||||
@@ -1292,6 +1472,7 @@ function onKeyDown(e: KeyboardEvent): void {
|
||||
selection.value = null;
|
||||
dragRect.value = null;
|
||||
selHitId.value = null;
|
||||
cancelPreview();
|
||||
scheduleRender();
|
||||
}
|
||||
}
|
||||
@@ -1310,6 +1491,9 @@ function exportDoc(): Promise<{ blob: Blob; detail: string; w: number; h: number
|
||||
reject(new Error('还没有加载图片'));
|
||||
return;
|
||||
}
|
||||
if (preview.value) {
|
||||
applyPreview(); // 画布上看到的预览必须进成品(所见即所得)
|
||||
}
|
||||
composite();
|
||||
const keepPng = props.file.type === 'image/png';
|
||||
const out = document.createElement('canvas');
|
||||
@@ -1409,14 +1593,23 @@ onBeforeUnmount(() => {
|
||||
<button class="psx-tool" title="重做 (Ctrl+Y)" :disabled="hIdx >= hist.length - 1" @click="redo()">↷</button>
|
||||
</div>
|
||||
<div class="psx-opts">
|
||||
<template v-if="preview">
|
||||
<span class="psx-live">👁 实时预览已生成</span>
|
||||
<button class="psx-btn primary" title="把预览结果写入图层(Ctrl+Z 可撤销)" @click="applyPreview()">
|
||||
✓ 应用消除
|
||||
</button>
|
||||
<button class="psx-btn" @click="cancelPreview()">✕ 取消预览</button>
|
||||
<span class="psx-tip">效果已在画布上显示,满意就应用</span>
|
||||
</template>
|
||||
<template v-if="tool === 'brush' || tool === 'eraser'">
|
||||
<span class="psx-lb">大小</span>
|
||||
<span class="psx-lb">{{ tool === 'eraser' && activeLayer?.type !== 'paint' ? '笔头' : '大小' }}</span>
|
||||
<input v-model.number="brushSize" type="range" min="1" max="200" class="psx-range">
|
||||
<span class="psx-num">{{ brushSize }}px</span>
|
||||
<template v-if="tool === 'brush'">
|
||||
<span class="psx-lb">颜色</span>
|
||||
<input v-model="brushColor" type="color" class="psx-color" title="画笔颜色">
|
||||
</template>
|
||||
<span v-if="tool === 'eraser' && activeLayer?.type !== 'paint'" class="psx-tip">涂文字上,松手立即消除</span>
|
||||
</template>
|
||||
<template v-else-if="tool === 'text'">
|
||||
<input v-model="newText" class="psx-textin" placeholder="文字内容,点击画布放置">
|
||||
@@ -1434,12 +1627,12 @@ onBeforeUnmount(() => {
|
||||
</template>
|
||||
<template v-else-if="tool === 'marquee' && selection">
|
||||
<span class="psx-num">选区 {{ selection.w }}×{{ selection.h }}</span>
|
||||
<button class="psx-btn primary" title="识别选区内的文字/水印并从背景重建(无痕消除)" @click="inpaintSelection">
|
||||
🧹 消除选区内容
|
||||
<button v-if="!preview" class="psx-btn primary" title="识别选区内的文字/水印并从背景重建(无痕消除)" @click="inpaintSelection">
|
||||
🧹 生成消除预览
|
||||
</button>
|
||||
<button class="psx-btn" @click="fillSelection">填充选区</button>
|
||||
<button class="psx-btn" @click="clearSelection">清除选区</button>
|
||||
<button class="psx-btn" @click="selection = null; scheduleRender()">取消选区</button>
|
||||
<button class="psx-btn" @click="selection = null; cancelPreview()">取消选区</button>
|
||||
</template>
|
||||
<template v-else-if="tool === 'crop'">
|
||||
<button v-if="dragRect" class="psx-btn primary" @click="applyCrop">✂ 应用裁剪 {{ dragRect.w }}×{{ dragRect.h }}</button>
|
||||
@@ -1451,9 +1644,10 @@ onBeforeUnmount(() => {
|
||||
<template v-if="selHit">
|
||||
<input v-model="hitText" class="psx-textin" :placeholder="selHit.text" @keyup.enter="applyHitReplace()">
|
||||
<button class="psx-btn primary" @click="applyHitReplace()">✓ 替换</button>
|
||||
<button class="psx-btn" title="识别文字笔画并从周边背景扩散重建,无痕删除" @click="applyHitErase()">
|
||||
<button v-if="!preview" class="psx-btn" title="识别文字笔画并从周边背景扩散重建,无痕删除" @click="applyHitErase()">
|
||||
🧹 消除文字
|
||||
</button>
|
||||
<span v-else class="psx-tip">已实时预览 → 点上方「✓ 应用消除」</span>
|
||||
</template>
|
||||
<span class="psx-flex" />
|
||||
<button class="psx-btn" title="适应窗口 (0)" @click="fit()">适应</button>
|
||||
@@ -1714,6 +1908,18 @@ onBeforeUnmount(() => {
|
||||
}
|
||||
}
|
||||
|
||||
&-live {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
padding: 3px 9px;
|
||||
border-radius: 999px;
|
||||
background: rgba(37, 99, 235, 0.14);
|
||||
color: #2563eb;
|
||||
font-weight: 600;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
&-flex {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user