Files
image-workbench-desktop/src/canvas-grid.js
T
qiqi f02b3dcec2
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 32s
feat(settings): 设置窗可拖动 + 外观实时预览(v3.25.76)
2026-11-21 07:24:33 +08:00

84 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* canvas-grid.js —— 画布点阵背景(v3.25.70)
*
* 为什么改:原来网格是 CSS radial-gradient,靠每帧改 background-position 跟随平移。
* WebKit 下这等于**每一帧重绘整屏点阵**,是拖动时最贵的一笔固定开销。
*
* 现在:
* ① 把点阵预生成成一张 gap×gap 的小 PNG(canvas → dataURL),按 gap 缓存;
* 图片平铺比渐变绘制便宜得多(浏览器有贴图快路径)。
* ② 平移只用 transform 位移(合成层,不重绘),并对 gap 取模保证无限平铺。
* ③ 缩放时先按旧纹理做 scale,等提交后再按新 gap 重新生成纹理(避免滚动中反复生成)。
*
* 无 document / canvas 时(jsdom)返回 null,调用方回落到原来的 CSS 渐变。
*/
;(function (global) {
'use strict'
const cache = new Map() // key: gap|dot|color → dataURL
function createTile (gap, dot, color, style) {
if (typeof document === 'undefined') return null
const g = Math.max(4, Math.round(gap))
const st = (style === 'lines') ? 'lines' : 'dots'
const key = g + '|' + dot + '|' + color + '|' + st
const hit = cache.get(key)
if (hit) return hit
try {
const cv = document.createElement('canvas')
cv.width = g
cv.height = g
const ctx = cv.getContext('2d')
if (!ctx) return null
ctx.fillStyle = color
if (st === 'lines') {
// 网格线:画右/下两条 1px 线,平铺后即连续网格(v3.25.75)
const w = Math.max(1, Math.round(Math.min(2, dot * 0.6)))
ctx.fillRect(g - w, 0, w, g)
ctx.fillRect(0, g - w, g, w)
} else {
ctx.beginPath()
ctx.arc(g / 2, g / 2, Math.max(0.5, dot), 0, Math.PI * 2)
ctx.fill()
}
const url = cv.toDataURL('image/png')
if (cache.size > 24) cache.clear()
cache.set(key, url)
return url
} catch (e) { return null }
}
/** 计算网格样式(React 渲染用):返回 { backgroundImage, backgroundSize, transform } */
function styleOf (x, y, scale, opts) {
const o = opts || {}
const base = Math.max(4, Number(o.base) || 28)
const gap = Math.max(4, base * (Number(scale) > 0 ? Number(scale) : 1))
const url = createTile(gap, o.dot || 1.2, o.color || 'rgba(120,128,140,0.55)', o.style)
if (!url) return null
return {
backgroundImage: 'url(' + url + ')',
backgroundSize: gap + 'px ' + gap + 'px',
transform: 'translate(' + (x % gap) + 'px, ' + (y % gap) + 'px)',
}
}
/** 手势快路径:直接改 DOM(不重绘点阵,只移动/缩放贴图层) */
function applyTo (el, x, y, scale, opts) {
if (!el) return
const o = opts || {}
const base = Math.max(4, Number(o.base) || 28)
const gap = Math.max(4, base * (Number(scale) > 0 ? Number(scale) : 1))
const url = createTile(gap, o.dot || 1.2, o.color || 'rgba(120,128,140,0.55)', o.style)
if (url) {
if (el.dataset.gap !== String(gap) || el.dataset.style !== String(o.style || 'dots')) {
el.style.backgroundImage = 'url(' + url + ')'
el.style.backgroundSize = gap + 'px ' + gap + 'px'
el.dataset.gap = String(gap)
el.dataset.style = String(o.style || 'dots')
}
el.style.transform = 'translate(' + (x % gap) + 'px, ' + (y % gap) + 'px)'
}
}
global.IWBCanvasGrid = { styleOf, applyTo, createTile }
})(typeof window !== 'undefined' ? window : globalThis)