Skip to content

Instantly share code, notes, and snippets.

@stepankuzmin
Created October 15, 2024 15:06
Show Gist options
  • Save stepankuzmin/e44a701a02361401d0aa5fabd6ca69fd to your computer and use it in GitHub Desktop.
Save stepankuzmin/e44a701a02361401d0aa5fabd6ca69fd to your computer and use it in GitHub Desktop.
Log OffscreenCanvas rendering Context in the browser console
export function logOffscreenCanvasContext(context, scaleFactor = 1) {
const canvas = document.createElement('canvas');
canvas.width = context.canvas.width;
canvas.height = context.canvas.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(context.canvas, 0, 0);
const displayWidth = canvas.width * scaleFactor;
const displayHeight = canvas.height * scaleFactor;
console.log('%c ', `
font-size: 1px;
padding: ${displayHeight / 2}px ${displayWidth / 2}px;
line-height: 0;
background: url(${canvas.toDataURL()}) no-repeat center;
background-size: ${displayWidth}px ${displayHeight}px;
border: 1px solid red;
`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment