feat: improve map and upload experience
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user