Skip to content

Instantly share code, notes, and snippets.

@greggman
Created November 20, 2022 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggman/8a8c1fda421809ec50b50eaa82e21faf to your computer and use it in GitHub Desktop.
Save greggman/8a8c1fda421809ec50b50eaa82e21faf to your computer and use it in GitHub Desktop.
An image's width and height do not affect its backingstore

An image's width and height do not affect its backingstore

view on jsgist

pre { margin: 0; }
canvas { background: #ddd; border: 1px solid black; }
<canvas></canvas>
async function main() {
const img = new Image();
document.body.appendChild(img);
img.src = 'https://greggman.github.io/placeholders/images/200x100.jpg'
await img.decode();
img.width = 22;
img.height = 11;
ctx = document.querySelector('canvas').getContext('2d');
ctx.drawImage(img, 0, 0);
};
main();
{"name":"An image's width and height do not affect its backingstore","settings":{},"filenames":["index.html","index.css","index.js"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment