* 运行绘制并缓存
(
cacheKey: string,
x: number,
y: number,
width: number,
height: number,
func: (ctx: OffscreenCanvasRenderingContext2D, ratio: number) => void
)
| 401 | * 运行绘制并缓存 |
| 402 | */ |
| 403 | async customDrawWithCache( |
| 404 | cacheKey: string, |
| 405 | x: number, |
| 406 | y: number, |
| 407 | width: number, |
| 408 | height: number, |
| 409 | func: (ctx: OffscreenCanvasRenderingContext2D, ratio: number) => void |
| 410 | ): Promise<void> { |
| 411 | return new Promise<void>((resolve, reject) => { |
| 412 | const ratio = this.ratio * this.zoom; |
| 413 | const cache = this.imageCache.get(cacheKey); |
| 414 | if (cache) { |
| 415 | this.ctx.drawImage(cache, x, y, width, height); |
| 416 | resolve(); |
| 417 | return; |
| 418 | } |
| 419 | const imageBitmap = drawOffscreenCanvas(ratio, width, height, ctx => { |
| 420 | func(ctx, ratio); |
| 421 | }); |
| 422 | this.ctx.drawImage(imageBitmap, x, y, width, height); |
| 423 | this.imageCache.set(cacheKey, imageBitmap); |
| 424 | resolve(); |
| 425 | }); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * 清空缓存 |
no test coverage detected