From a74978d09aabe697ac283bdd6c153d27f277e546 Mon Sep 17 00:00:00 2001 From: qiqi Date: Tue, 17 Nov 2026 20:37:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(annotate):=20=E5=BF=AB=E9=80=9F=E6=95=88?= =?UTF-8?q?=E6=9E=9C=E5=9C=A8=20WKWebView=20=E4=B8=8B=E5=85=A8=E9=83=A8?= =?UTF-8?q?=E5=A4=B1=E6=95=88=EF=BC=88canvas=20ctx.filter=20=E4=B8=8D?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=89=E2=86=92=20=E5=8A=A0=E9=80=90?= =?UTF-8?q?=E5=83=8F=E7=B4=A0/=E7=BC=A9=E6=94=BE=E5=85=9C=E5=BA=95=20v1.20?= =?UTF-8?q?.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- image-annotate/package.json | 2 +- image-annotate/plugin/editor/effects.js | 98 +++++++++++++++++++++- image-annotate/src/index.js | 104 ++++++++++++++++++++++-- 3 files changed, 194 insertions(+), 10 deletions(-) diff --git a/image-annotate/package.json b/image-annotate/package.json index d99d1b2..143ea68 100644 --- a/image-annotate/package.json +++ b/image-annotate/package.json @@ -1,7 +1,7 @@ { "name": "@iwb-plugin/image-annotate", "private": true, - "version": "1.20.2", + "version": "1.20.3", "description": "IWB 插件:图片标注 / 修图(编辑器移植自 iwb-plaus 扩展,纯 Canvas 2D)", "ibwPlugin": { "id": "image-annotate", diff --git a/image-annotate/plugin/editor/effects.js b/image-annotate/plugin/editor/effects.js index e381450..e071b3c 100644 --- a/image-annotate/plugin/editor/effects.js +++ b/image-annotate/plugin/editor/effects.js @@ -46,12 +46,38 @@ import { toast } from './commands.js' invert: { label: '反白', hint: '反相:明暗翻转,白底变黑底', param: '强度', unit: '%', min: 0, max: 100, step: 1, def: () => 100, - render: (canvas, v) => { if (v > 0) filterCanvas(canvas, 'invert(' + (v / 100) + ')') } + render: (canvas, v) => { + if (v <= 0) return + const k = v / 100 + if (!FILTER_SUPPORTED) { + const bright = 1 + 0.4 * k, contrast = 1 + 0.25 * k, sat = 1 + 0.2 * k + mapPixels(canvas, (r, g, b) => { + const y = luma(r, g, b) + const out = [r, g, b].map((c) => { + let v2 = y + (c - y) * sat + v2 = (v2 - 128) * contrast + 128 + return clamp255(v2 * bright) + }) + return out + }) + return + } + if (FILTER_SUPPORTED) { filterCanvas(canvas, 'invert(' + k + ')'); return } + mapPixels(canvas, (r, g, b) => [clamp255(255 - (255 - r) * k), clamp255(255 - (255 - g) * k), clamp255(255 - (255 - b) * k)]) + } }, gray: { label: '灰度', hint: '去色,保留明暗层次', param: '强度', unit: '%', min: 0, max: 100, step: 1, def: () => 100, - render: (canvas, v) => { if (v > 0) filterCanvas(canvas, 'grayscale(' + (v / 100) + ')') } + render: (canvas, v) => { + if (v <= 0) return + const k = v / 100 + if (FILTER_SUPPORTED) { filterCanvas(canvas, 'grayscale(' + k + ')'); return } + mapPixels(canvas, (r, g, b) => { + const y = luma(r, g, b) + return [clamp255(r + (y - r) * k), clamp255(g + (y - g) * k), clamp255(b + (y - b) * k)] + }) + } }, boost: { label: '增强', hint: '提亮并加对比,适合发灰的截图', @@ -71,13 +97,24 @@ import { toast } from './commands.js' render: (canvas, v) => { if (v <= 0) return const k = v / 100 - filterCanvas(canvas, 'grayscale(' + k.toFixed(3) + ') contrast(' + (1 + 1.6 * k).toFixed(3) + ')') + if (FILTER_SUPPORTED) { filterCanvas(canvas, 'grayscale(' + k.toFixed(3) + ') contrast(' + (1 + 1.6 * k).toFixed(3) + ')'); return } + const c = 1 + 1.6 * k + mapPixels(canvas, (r, g, b) => { + const y = luma(r, g, b) + const gray = y * k + ((r + g + b) / 3) * (1 - k) + const v2 = (gray - 128) * c + 128 + return [clamp255(v2), clamp255(v2), clamp255(v2)] + }) } }, soft: { label: '柔化', hint: '模糊,弱化背景', param: '半径', unit: 'px', min: 0, max: 16, step: 0.5, def: (canvas) => softRadius(canvas), - render: (canvas, v) => { if (v > 0) filterCanvas(canvas, 'blur(' + v + 'px)') } + render: (canvas, v) => { + if (v <= 0) return + if (FILTER_SUPPORTED) filterCanvas(canvas, 'blur(' + v + 'px)') + else blurCanvas(canvas, v) + } }, mosaic: { label: '马赛克', hint: '像素化,用于打码手机号/地址等隐私信息', @@ -115,6 +152,59 @@ import { toast } from './commands.js' ctx.restore() } + // ctx.filter 支持性:Chrome/Edge 支持,macOS WKWebView 长期不支持(赋值会被忽略)。 + // 不支持时下面全部走像素级兜底,否则「只有马赛克有效」——马赛克本来就不依赖 filter。 + const FILTER_SUPPORTED = (function () { + try { + const c = document.createElement('canvas') + const ctx = c.getContext('2d') + if (!ctx) return false + ctx.filter = 'invert(1)' + return ctx.filter === 'invert(1)' + } catch (e) { return false } + })() + + /** 逐像素处理(在设备像素坐标系里直接写回,避免受图层视图变换影响) */ + function mapPixels(canvas, fn) { + const w = canvas.width, h = canvas.height + if (!w || !h) return + const ctx = canvas.getContext('2d', { willReadFrequently: true }) + const img = ctx.getImageData(0, 0, w, h) + const d = img.data + for (let i = 0; i < d.length; i += 4) { + if (d[i + 3] === 0) continue + const out = fn(d[i], d[i + 1], d[i + 2]) + d[i] = out[0]; d[i + 1] = out[1]; d[i + 2] = out[2] + } + ctx.setTransform(1, 0, 0, 1, 0, 0) + ctx.putImageData(img, 0, 0) + } + + function luma(r, g, b) { return 0.2126 * r + 0.7152 * g + 0.0722 * b } + function clamp255(v) { return v < 0 ? 0 : (v > 255 ? 255 : v) } + + /** 模糊兜底:缩到 1/n 再平滑放大,效果接近 blur 且比卷积快得多 */ + function blurCanvas(canvas, radius) { + const w = canvas.width, h = canvas.height + if (!w || !h || radius <= 0) return + const n = Math.max(2, Math.min(16, Math.round(radius * 1.6))) + const sw = Math.max(1, Math.round(w / n)) + const sh = Math.max(1, Math.round(h / n)) + 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 = true + ctx.drawImage(tmp, 0, 0, w, h) + ctx.restore() + } + /** 马赛克:缩到 1/block 再关掉平滑放大,比逐像素 getImageData 快一个量级 */ function pixelateCanvas(canvas, block) { const w = canvas.width, h = canvas.height diff --git a/image-annotate/src/index.js b/image-annotate/src/index.js index 4bee409..b049a86 100644 --- a/image-annotate/src/index.js +++ b/image-annotate/src/index.js @@ -9,7 +9,7 @@ var __IWB_ANNOTATE_MANIFEST__ = { id: 'image-annotate', name: '标注 / 修图', - version: '1.20.2', + version: '1.20.3', apiVersion: 1, capabilities: ['commands', 'nodeActions', 'canvas', 'canvasWrite'], settings: { @@ -3899,7 +3899,26 @@ var __IWB_ANNOTATE_MANIFEST__ = { step: 1, def: () => 100, render: (canvas, v) => { - if (v > 0) filterCanvas(canvas, "invert(" + v / 100 + ")"); + if (v <= 0) return; + const k = v / 100; + if (!FILTER_SUPPORTED) { + const bright = 1 + 0.4 * k, contrast = 1 + 0.25 * k, sat = 1 + 0.2 * k; + mapPixels(canvas, (r, g, b) => { + const y = luma(r, g, b); + const out = [r, g, b].map((c) => { + let v2 = y + (c - y) * sat; + v2 = (v2 - 128) * contrast + 128; + return clamp255(v2 * bright); + }); + return out; + }); + return; + } + if (FILTER_SUPPORTED) { + filterCanvas(canvas, "invert(" + k + ")"); + return; + } + mapPixels(canvas, (r, g, b) => [clamp255(255 - (255 - r) * k), clamp255(255 - (255 - g) * k), clamp255(255 - (255 - b) * k)]); } }, gray: { @@ -3912,7 +3931,16 @@ var __IWB_ANNOTATE_MANIFEST__ = { step: 1, def: () => 100, render: (canvas, v) => { - if (v > 0) filterCanvas(canvas, "grayscale(" + v / 100 + ")"); + if (v <= 0) return; + const k = v / 100; + if (FILTER_SUPPORTED) { + filterCanvas(canvas, "grayscale(" + k + ")"); + return; + } + mapPixels(canvas, (r, g, b) => { + const y = luma(r, g, b); + return [clamp255(r + (y - r) * k), clamp255(g + (y - g) * k), clamp255(b + (y - b) * k)]; + }); } }, boost: { @@ -3942,7 +3970,17 @@ var __IWB_ANNOTATE_MANIFEST__ = { render: (canvas, v) => { if (v <= 0) return; const k = v / 100; - filterCanvas(canvas, "grayscale(" + k.toFixed(3) + ") contrast(" + (1 + 1.6 * k).toFixed(3) + ")"); + if (FILTER_SUPPORTED) { + filterCanvas(canvas, "grayscale(" + k.toFixed(3) + ") contrast(" + (1 + 1.6 * k).toFixed(3) + ")"); + return; + } + const c = 1 + 1.6 * k; + mapPixels(canvas, (r, g, b) => { + const y = luma(r, g, b); + const gray = y * k + (r + g + b) / 3 * (1 - k); + const v2 = (gray - 128) * c + 128; + return [clamp255(v2), clamp255(v2), clamp255(v2)]; + }); } }, soft: { @@ -3955,7 +3993,9 @@ var __IWB_ANNOTATE_MANIFEST__ = { step: 0.5, def: (canvas) => softRadius(canvas), render: (canvas, v) => { - if (v > 0) filterCanvas(canvas, "blur(" + v + "px)"); + if (v <= 0) return; + if (FILTER_SUPPORTED) filterCanvas(canvas, "blur(" + v + "px)"); + else blurCanvas(canvas, v); } }, mosaic: { @@ -3995,6 +4035,60 @@ var __IWB_ANNOTATE_MANIFEST__ = { ctx.drawImage(tmp, 0, 0); ctx.restore(); } + var FILTER_SUPPORTED = function() { + try { + const c = document.createElement("canvas"); + const ctx = c.getContext("2d"); + if (!ctx) return false; + ctx.filter = "invert(1)"; + return ctx.filter === "invert(1)"; + } catch (e) { + return false; + } + }(); + function mapPixels(canvas, fn) { + const w = canvas.width, h = canvas.height; + if (!w || !h) return; + const ctx = canvas.getContext("2d", { willReadFrequently: true }); + const img = ctx.getImageData(0, 0, w, h); + const d = img.data; + for (let i = 0; i < d.length; i += 4) { + if (d[i + 3] === 0) continue; + const out = fn(d[i], d[i + 1], d[i + 2]); + d[i] = out[0]; + d[i + 1] = out[1]; + d[i + 2] = out[2]; + } + ctx.setTransform(1, 0, 0, 1, 0, 0); + ctx.putImageData(img, 0, 0); + } + function luma(r, g, b) { + return 0.2126 * r + 0.7152 * g + 0.0722 * b; + } + function clamp255(v) { + return v < 0 ? 0 : v > 255 ? 255 : v; + } + function blurCanvas(canvas, radius) { + const w = canvas.width, h = canvas.height; + if (!w || !h || radius <= 0) return; + const n = Math.max(2, Math.min(16, Math.round(radius * 1.6))); + const sw = Math.max(1, Math.round(w / n)); + const sh = Math.max(1, Math.round(h / n)); + 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 = true; + ctx.drawImage(tmp, 0, 0, w, h); + ctx.restore(); + } function pixelateCanvas(canvas, block) { const w = canvas.width, h = canvas.height; if (!w || !h) return;