Last active
June 7, 2023 06:17
-
-
Save MikuroXina/0426ae5129edd373088c344f781d2268 to your computer and use it in GitHub Desktop.
Make `<canvas>` prepared for more pixel density display.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Make `<canvas>` prepared for more pixel density display and returns a 2D rendering context. | |
| * | |
| * @param canvas {HTMLCanvasElement} - To be preapared. | |
| * @param devicePixelRatio {number} - The pixel ratio by device. | |
| * @returns {Canvas2DRenderingContext} - The scaled 2D rendering context. | |
| */ | |
| export function setupCanvas(canvas, devicePixelRatio) { | |
| const { width, height } = canvas; | |
| canvas.width *= devicePixelRatio; | |
| canvas.height *= devicePixelRatio; | |
| canvas.style.width = width + "px"; | |
| canvas.style.height = height + "px"; | |
| const ctx = canvas.getContext("2d"); | |
| ctx.scale(devicePixelRatio, devicePixelRatio); | |
| return ctx; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment