perf(canvas): 网格贴图化 + LOD 滞回(v3.25.70)
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 41s
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 41s
This commit is contained in:
1 parent
7e544b9e10
commit
fb3a3f3623
17 files changed
+168
-29
No files matched your search
@@ -0,0 +1,74 @@
|
||||
/* 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) {
|
||||
if (typeof document === 'undefined') return null
|
||||
const g = Math.max(4, Math.round(gap))
|
||||
const key = g + '|' + dot + '|' + color
|
||||
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
|
||||
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)')
|
||||
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)')
|
||||
if (url) {
|
||||
if (el.dataset.gap !== String(gap)) {
|
||||
el.style.backgroundImage = 'url(' + url + ')'
|
||||
el.style.backgroundSize = gap + 'px ' + gap + 'px'
|
||||
el.dataset.gap = String(gap)
|
||||
}
|
||||
el.style.transform = 'translate(' + (x % gap) + 'px, ' + (y % gap) + 'px)'
|
||||
}
|
||||
}
|
||||
|
||||
global.IWBCanvasGrid = { styleOf, applyTo, createTile }
|
||||
})(typeof window !== 'undefined' ? window : globalThis)
|
||||
Reference in new issue
Block a user