feat: improve map and upload experience

This commit is contained in:
2026-05-21 16:54:38 +08:00
parent cb2581afb2
commit 2b7f393d7c
6 changed files with 386 additions and 85 deletions
+37
View File
@@ -0,0 +1,37 @@
const PATCH_FLAG = '__openCloudWillReadFrequentlyPatched__'
type CanvasLikePrototype = {
getContext?: (contextId: string, options?: unknown) => unknown
[PATCH_FLAG]?: boolean
}
function patch2DContextOptions(ctor: typeof HTMLCanvasElement | typeof OffscreenCanvas | undefined) {
const prototype = ctor?.prototype as CanvasLikePrototype | undefined
if (!prototype?.getContext || prototype[PATCH_FLAG]) return
const originalGetContext = prototype.getContext
prototype.getContext = function getContextWithReadHint(contextId: string, options?: unknown) {
if (contextId === '2d') {
const nextOptions =
options && typeof options === 'object'
? { ...options as Record<string, unknown>, willReadFrequently: true }
: { willReadFrequently: true }
return originalGetContext.call(this, contextId, nextOptions)
}
return originalGetContext.call(this, contextId, options)
}
prototype[PATCH_FLAG] = true
}
export function enableCanvasReadbackHint() {
if (typeof window === 'undefined') return
patch2DContextOptions(window.HTMLCanvasElement)
if (typeof window.OffscreenCanvas !== 'undefined') {
patch2DContextOptions(window.OffscreenCanvas)
}
}