Gets canvas CSS width - clientWidth for HTMLCanvasElement, width/DPR for OffscreenCanvas.
(canvas: SupportedCanvas | null, devicePixelRatio: number = 1)
| 87 | |
| 88 | /** Gets canvas CSS width - clientWidth for HTMLCanvasElement, width/DPR for OffscreenCanvas. */ |
| 89 | function getCanvasCssWidth(canvas: SupportedCanvas | null, devicePixelRatio: number = 1): number { |
| 90 | if (!canvas) { |
| 91 | return 0; |
| 92 | } |
| 93 | if (isHTMLCanvasElement(canvas)) { |
| 94 | return canvas.clientWidth; |
| 95 | } |
| 96 | // OffscreenCanvas: width property is in device pixels. Convert to CSS pixels by dividing by DPR. |
| 97 | return canvas.width / devicePixelRatio; |
| 98 | } |
| 99 | |
| 100 | /** Gets canvas CSS height - clientHeight for HTMLCanvasElement, height/DPR for OffscreenCanvas. */ |
| 101 | function getCanvasCssHeight(canvas: SupportedCanvas | null, devicePixelRatio: number = 1): number { |
no test coverage detected