From 80ea601a5bec06ebb57813a55c086996919be1f8 Mon Sep 17 00:00:00 2001 From: Sean Boult <996134+Hacksore@users.noreply.github.com> Date: Sat, 29 Oct 2022 12:29:18 +0000 Subject: [PATCH] Add attribute `willReadFrequently` to all calls to `getContext` More info https://github.com/fserb/canvas2D/blob/master/spec/will-read-frequently.md --- src/process.ts | 10 +++++----- test/test.ts | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/process.ts b/src/process.ts index 918a164..26353b5 100644 --- a/src/process.ts +++ b/src/process.ts @@ -112,7 +112,7 @@ export function loadSkinToCanvas(canvas: TextureCanvas, image: TextureSource): v } } - const context = canvas.getContext("2d") as CanvasContext; + const context = canvas.getContext("2d", { willReadFrequently: true }) as CanvasContext; if (isOldFormat) { const sideLength = image.width; canvas.width = sideLength; @@ -150,7 +150,7 @@ export function loadCapeToCanvas(canvas: TextureCanvas, image: TextureSource): v canvas.width = 64 * scale; canvas.height = 32 * scale; - const context = canvas.getContext("2d") as CanvasContext; + const context = canvas.getContext("2d", { willReadFrequently: true }) as CanvasContext; context.clearRect(0, 0, canvas.width, canvas.height); context.drawImage(image, 0, 0, image.width, image.height); } @@ -233,7 +233,7 @@ export function inferModelType(canvas: TextureCanvas): ModelType { // If the 4 areas are all black or all white, the skin is also considered as slim. const scale = computeSkinScale(canvas.width); - const context = canvas.getContext("2d") as CanvasContext; + const context = canvas.getContext("2d", { willReadFrequently: true }) as CanvasContext; const checkTransparency = (x: number, y: number, w: number, h: number): boolean => hasTransparency(context, x * scale, y * scale, w * scale, h * scale); const checkBlack = (x: number, y: number, w: number, h: number): boolean => @@ -275,7 +275,7 @@ export function loadEarsToCanvas(canvas: TextureCanvas, image: TextureSource): v canvas.width = 14 * scale; canvas.height = 7 * scale; - const context = canvas.getContext("2d") as CanvasContext; + const context = canvas.getContext("2d", { willReadFrequently: true }) as CanvasContext; context.clearRect(0, 0, canvas.width, canvas.height); context.drawImage(image, 0, 0, image.width, image.height); } @@ -290,7 +290,7 @@ export function loadEarsToCanvasFromSkin(canvas: TextureCanvas, image: TextureSo const h = 7 * scale; canvas.width = w; canvas.height = h; - const context = canvas.getContext("2d") as CanvasContext; + const context = canvas.getContext("2d", { willReadFrequently: true }) as CanvasContext; context.clearRect(0, 0, w, h); context.drawImage(image, 24 * scale, 0, w, h, 0, 0, w, h); } diff --git a/test/test.ts b/test/test.ts index 30e3369..59e6568 100644 --- a/test/test.ts +++ b/test/test.ts @@ -43,7 +43,9 @@ describe("detect model of texture", () => { describe("process skin texture", () => { const expectTransparent = (canvas: HTMLCanvasElement, x0: number, y0: number, w: number, h: number) => { - const data = canvas.getContext("2d")!.getImageData(x0, y0, w, h).data; + const ctx = canvas.getContext("2d", { willReadFrequently: true }) + + const data = ctx!.getImageData(x0, y0, w, h).data; for (let x = 0; x < w; x++) { for (let y = 0; y < h; y++) { expect(data[(y * h + x) * 4 + 3], `pixel (${x0 + x}, ${y0 + y})`).to.equal(0);