Last active
June 4, 2025 17:37
-
-
Save jbenner-radham/79f61c7a04615539253a2d0e7a2a9dd4 to your computer and use it in GitHub Desktop.
JS/TS Snippets
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
| const get2dCanvasContext = (): CanvasRenderingContext2D | null => { | |
| const canvas = document.createElement('canvas') | |
| const context = canvas.getContext('2d') | |
| if (!context) return context | |
| const { fontWeight, fontSize, fontFamily } = window.getComputedStyle(document.body) | |
| context.font = `${fontWeight} ${fontSize} ${fontFamily}` | |
| return context | |
| } | |
| const context = get2dCanvasContext() | |
| if (!context) { | |
| // Handle null context... | |
| } | |
| const textWidth = context.measureText('Hello world!').width |
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
| const pxPerRem = Number.parseFloat(window.getComputedStyle(document.documentElement).fontSize) |
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
| const sleep = (milliseconds: number = 0): Promise<void> => new Promise(resolve => setTimeout(resolve, milliseconds)); | |
| console.log('I feel asleep...'); | |
| await sleep(1_000); | |
| console.log('Wakey wakey!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment