Created
January 21, 2026 07:21
-
-
Save jdmichaud/68349feac3e8d95c67bd047693fc8590 to your computer and use it in GitHub Desktop.
Display image in the browser's console
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
| (window as any).console.image = (canvas: HTMLCanvasElement, targetWidth?: number, targetHeight?: number) => { | |
| const url = canvas.toDataURL(); | |
| const image = new Image(); | |
| image.src = url; | |
| image.onload = () => { | |
| // 1. Determine dimensions: Use target resolution if provided, | |
| // otherwise default to the canvas's own dimensions. | |
| const w = targetWidth ?? canvas.width; | |
| const h = targetHeight ?? canvas.height; | |
| const style = [ | |
| 'font-size: 1px;', | |
| // 2. Padding creates the "box". We use half the width/height | |
| // because padding is added to both sides of the character. | |
| `padding: ${h / 2}px ${w / 2}px;`, | |
| `background: url(${url}) no-repeat;`, | |
| // 3. Force the background image to fit the exact box size | |
| `background-size: ${w}px ${h}px;`, | |
| 'line-height: 0;', | |
| ].join(' '); | |
| // Use a single space character as the "container" for the background | |
| console.log('%c ', style); | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment