fix(annotate): 快速效果在 WKWebView 下全部失效(canvas ctx.filter 不支持)→ 加逐像素/缩放兜底 v1.20.3
This commit is contained in:
1 parent
136fdf58f5
commit
a74978d09a
3 files changed
+194
-10
No files matched your search
+94
-4
@@ -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
|
||||
|
||||
Reference in new issue
Block a user