Skip to content

Commit

Permalink
Merge pull request #14 from bs-community/feat/use-will-read-frequently
Browse files Browse the repository at this point in the history
Add attribute `willReadFrequently` to all calls to `getContext`
  • Loading branch information
yushijinhun committed Feb 10, 2023
2 parents d4860ef + 80ea601 commit 7399a25
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
4 changes: 3 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7399a25

Please sign in to comment.