Skip to content

Commit

Permalink
Merge pull request #1572 from saitonakamura/feat-device-pixel-ratio-o…
Browse files Browse the repository at this point in the history
…verride

feat(CanvasContext): Added optional devicePixelRatio argument to resize
  • Loading branch information
ronyeh authored Jun 1, 2023
2 parents 6cf8770 + 4fe9a6b commit 253f769
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/canvascontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,19 @@ export class CanvasContext extends RenderContext {
return this;
}

resize(width: number, height: number): this {
resize(width: number, height: number, devicePixelRatio?: number): this {
const canvas = this.context2D.canvas;
const devicePixelRatio = globalObject().devicePixelRatio || 1;
const dpr: number = devicePixelRatio ?? globalObject().devicePixelRatio ?? 1;

// Scale the canvas size by the device pixel ratio clamping to the maximum supported size.
[width, height] = CanvasContext.sanitizeCanvasDims(width * devicePixelRatio, height * devicePixelRatio);
[width, height] = CanvasContext.sanitizeCanvasDims(width * dpr, height * dpr);

// Divide back down by the pixel ratio and convert to integers.
width = (width / devicePixelRatio) | 0;
height = (height / devicePixelRatio) | 0;
width = (width / dpr) | 0;
height = (height / dpr) | 0;

canvas.width = width * devicePixelRatio;
canvas.height = height * devicePixelRatio;
canvas.width = width * dpr;
canvas.height = height * dpr;

// The canvas could be an instance of either HTMLCanvasElement or an OffscreenCanvas.
// Only HTMLCanvasElement has a style attribute.
Expand All @@ -190,7 +190,7 @@ export class CanvasContext extends RenderContext {
canvas.style.height = height + 'px';
}

return this.scale(devicePixelRatio, devicePixelRatio);
return this.scale(dpr, dpr);
}

rect(x: number, y: number, width: number, height: number): this {
Expand Down

0 comments on commit 253f769

Please sign in to comment.