Skip to content

Commit

Permalink
refactor(hdom-components): remove adaptDPI()
Browse files Browse the repository at this point in the history
BREAKING CHANGE: re-use adaptDPI() from new @thi.ng/adapt-dpi pkg

- update deps
  • Loading branch information
postspectacular committed Jun 6, 2020
1 parent 7250041 commit 2b89ad4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/hdom-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"typescript": "^3.9.2"
},
"dependencies": {
"@thi.ng/adapt-dpi": "^0.0.1",
"@thi.ng/api": "^6.11.0",
"@thi.ng/checks": "^2.7.0",
"@thi.ng/math": "^1.7.10",
Expand Down
28 changes: 3 additions & 25 deletions packages/hdom-components/src/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { adaptDPI } from "@thi.ng/adapt-dpi";

export type CanvasContext =
| CanvasRenderingContext2D
| WebGLRenderingContext
Expand Down Expand Up @@ -81,7 +83,7 @@ const _canvas = (
},
release(hctx: any, ...args: any[]) {
handlers.release && handlers.release(el, ctx, hctx, ...args);
}
},
};
};

Expand Down Expand Up @@ -136,27 +138,3 @@ export const canvas2D = (
handlers: Partial<CanvasHandlers<CanvasRenderingContext2D>>,
opts?: Canvas2DContextAttributes
) => _canvas("2d", handlers, opts);

/**
* Sets the canvas size to given `width` & `height` and adjusts style to
* compensate for HDPI devices. Note: For 2D canvases, this will
* automatically clear any prior canvas content.
*
* @param canvas -
* @param width - uncompensated pixel width
* @param height - uncompensated pixel height
*/
export const adaptDPI = (
canvas: HTMLCanvasElement,
width: number,
height: number
) => {
const dpr = window.devicePixelRatio || 1;
if (dpr != 1) {
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
}
canvas.width = width * dpr;
canvas.height = height * dpr;
return dpr;
};

0 comments on commit 2b89ad4

Please sign in to comment.