diff --git a/image-annotate/package.json b/image-annotate/package.json index 143ea68..c0b8764 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.3", + "version": "1.20.4", "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 e071b3c..09fe3f7 100644 --- a/image-annotate/plugin/editor/effects.js +++ b/image-annotate/plugin/editor/effects.js @@ -63,7 +63,8 @@ import { toast } from './commands.js' 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)]) + const okInv = mapPixels(canvas, (r, g, b) => [clamp255(255 - (255 - r) * k), clamp255(255 - (255 - g) * k), clamp255(255 - (255 - b) * k)]) + if (!okInv) blendFallback(canvas, 'invert') } }, gray: { @@ -73,10 +74,11 @@ import { toast } from './commands.js' if (v <= 0) return const k = v / 100 if (FILTER_SUPPORTED) { filterCanvas(canvas, 'grayscale(' + k + ')'); return } - mapPixels(canvas, (r, g, b) => { + const okGray = 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)] }) + if (!okGray) blendFallback(canvas, 'gray') } }, boost: { @@ -154,22 +156,32 @@ import { toast } from './commands.js' // ctx.filter 支持性:Chrome/Edge 支持,macOS WKWebView 长期不支持(赋值会被忽略)。 // 不支持时下面全部走像素级兜底,否则「只有马赛克有效」——马赛克本来就不依赖 filter。 + // ctx.filter 支持性:不能只看「属性是否可赋值」——WKWebView 里属性存在但绘制时被忽略, + // 赋值检测会误判为支持。这里做功能性探测:红块过 invert(1) 之后必须变青。 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)' + const src = document.createElement('canvas') + src.width = 2; src.height = 2 + const sctx = src.getContext('2d') + sctx.fillStyle = '#ff0000' + sctx.fillRect(0, 0, 2, 2) + const dst = document.createElement('canvas') + dst.width = 2; dst.height = 2 + const dctx = dst.getContext('2d') + dctx.filter = 'invert(1)' + dctx.drawImage(src, 0, 0) + const d = dctx.getImageData(0, 0, 1, 1).data + return d[0] < 60 && d[1] > 180 && d[2] > 180 } catch (e) { return false } })() - /** 逐像素处理(在设备像素坐标系里直接写回,避免受图层视图变换影响) */ + /** 逐像素处理(设备像素坐标系直接写回);画布被跨域图污染时 getImageData 会抛错 → 返回 false */ function mapPixels(canvas, fn) { const w = canvas.width, h = canvas.height - if (!w || !h) return + if (!w || !h) return false const ctx = canvas.getContext('2d', { willReadFrequently: true }) - const img = ctx.getImageData(0, 0, w, h) + let img + try { img = ctx.getImageData(0, 0, w, h) } catch (e) { return false } const d = img.data for (let i = 0; i < d.length; i += 4) { if (d[i + 3] === 0) continue @@ -178,6 +190,19 @@ import { toast } from './commands.js' } ctx.setTransform(1, 0, 0, 1, 0, 0) ctx.putImageData(img, 0, 0) + return true + } + + /** 混合模式兜底(读不到像素时):反相用 difference+白,去色用 saturation+灰 */ + function blendFallback(canvas, mode) { + const ctx = canvas.getContext('2d') + ctx.save() + ctx.setTransform(1, 0, 0, 1, 0, 0) + ctx.globalAlpha = 1 + if (mode === 'invert') { ctx.globalCompositeOperation = 'difference'; ctx.fillStyle = '#ffffff' } + else { ctx.globalCompositeOperation = 'saturation'; ctx.fillStyle = '#808080' } + ctx.fillRect(0, 0, canvas.width, canvas.height) + ctx.restore() } function luma(r, g, b) { return 0.2126 * r + 0.7152 * g + 0.0722 * b } @@ -295,7 +320,12 @@ import { toast } from './commands.js' ctx.drawImage(st.source, 0, 0) ctx.restore() - fx.render(target.canvas, value) + try { + fx.render(target.canvas, value) + } catch (e) { + toast('效果失败:' + ((e && e.message) || e), 'error') + throw e + } // 像素变了:包围盒要重算,吸管合成缓存要作废 target._bounds = undefined diff --git a/image-annotate/src/index.js b/image-annotate/src/index.js index b049a86..c7ed0a9 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.3', + version: '1.20.4', apiVersion: 1, capabilities: ['commands', 'nodeActions', 'canvas', 'canvasWrite'], settings: { @@ -3918,7 +3918,8 @@ var __IWB_ANNOTATE_MANIFEST__ = { 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)]); + const okInv = mapPixels(canvas, (r, g, b) => [clamp255(255 - (255 - r) * k), clamp255(255 - (255 - g) * k), clamp255(255 - (255 - b) * k)]); + if (!okInv) blendFallback(canvas, "invert"); } }, gray: { @@ -3937,10 +3938,11 @@ var __IWB_ANNOTATE_MANIFEST__ = { filterCanvas(canvas, "grayscale(" + k + ")"); return; } - mapPixels(canvas, (r, g, b) => { + const okGray = 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)]; }); + if (!okGray) blendFallback(canvas, "gray"); } }, boost: { @@ -4037,20 +4039,34 @@ var __IWB_ANNOTATE_MANIFEST__ = { } 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)"; + const src = document.createElement("canvas"); + src.width = 2; + src.height = 2; + const sctx = src.getContext("2d"); + sctx.fillStyle = "#ff0000"; + sctx.fillRect(0, 0, 2, 2); + const dst = document.createElement("canvas"); + dst.width = 2; + dst.height = 2; + const dctx = dst.getContext("2d"); + dctx.filter = "invert(1)"; + dctx.drawImage(src, 0, 0); + const d = dctx.getImageData(0, 0, 1, 1).data; + return d[0] < 60 && d[1] > 180 && d[2] > 180; } catch (e) { return false; } }(); function mapPixels(canvas, fn) { const w = canvas.width, h = canvas.height; - if (!w || !h) return; + if (!w || !h) return false; const ctx = canvas.getContext("2d", { willReadFrequently: true }); - const img = ctx.getImageData(0, 0, w, h); + let img; + try { + img = ctx.getImageData(0, 0, w, h); + } catch (e) { + return false; + } const d = img.data; for (let i = 0; i < d.length; i += 4) { if (d[i + 3] === 0) continue; @@ -4061,6 +4077,22 @@ var __IWB_ANNOTATE_MANIFEST__ = { } ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.putImageData(img, 0, 0); + return true; + } + function blendFallback(canvas, mode) { + const ctx = canvas.getContext("2d"); + ctx.save(); + ctx.setTransform(1, 0, 0, 1, 0, 0); + ctx.globalAlpha = 1; + if (mode === "invert") { + ctx.globalCompositeOperation = "difference"; + ctx.fillStyle = "#ffffff"; + } else { + ctx.globalCompositeOperation = "saturation"; + ctx.fillStyle = "#808080"; + } + ctx.fillRect(0, 0, canvas.width, canvas.height); + ctx.restore(); } function luma(r, g, b) { return 0.2126 * r + 0.7152 * g + 0.0722 * b; @@ -4166,7 +4198,12 @@ var __IWB_ANNOTATE_MANIFEST__ = { ctx.imageSmoothingEnabled = true; ctx.drawImage(st.source, 0, 0); ctx.restore(); - fx.render(target.canvas, value); + try { + fx.render(target.canvas, value); + } catch (e) { + toast("效果失败:" + (e && e.message || e), "error"); + throw e; + } target._bounds = void 0; S.pickerComposite = null; }