Skip to content

Commit

Permalink
feat(CanvasContext): Added optional devicePixelRatio argument to resize
Browse files Browse the repository at this point in the history
It's mainly useful in context of rendering in web worker,
because web worker doesn't have access to devicePixelRatio,
so resize code will be split between
1. main thread asking for a render, providing devicePixelRatio
2. Worker rendering and resizing CanvasContext w/ provided ratio
3. Worker giving back main thread the resize target
4. Main thread applying css width/height provided by the worker
  • Loading branch information
saitonakamura committed May 30, 2023
1 parent 54036f5 commit 1bfe2cf
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/canvascontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ export class CanvasContext extends RenderContext {
return this;
}

resize(width: number, height: number): this {
resize(width: number, height: number, devicePixelRatioOverride?: number): this {
const canvas = this.context2D.canvas;
const devicePixelRatio = globalObject().devicePixelRatio || 1;
const devicePixelRatio =
devicePixelRatioOverride != null ? devicePixelRatioOverride : 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);
Expand Down

0 comments on commit 1bfe2cf

Please sign in to comment.