Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active November 20, 2022 00:21
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/80195bb8ad9995a6798996e2b02d98d9 to your computer and use it in GitHub Desktop.
Save greggman/80195bb8ad9995a6798996e2b02d98d9 to your computer and use it in GitHub Desktop.
An Image's sizes
pre { margin: 0; }
/*bug-in-github-api-content-can-not-be-empty*/
async function main() {
const img = new Image();
document.body.appendChild(img);
img.src = 'https://greggman.github.io/placeholders/images/200x100.jpg'
await img.decode();
logImageData(img, 'load a 200x100 image');
img.src = 'https://greggman.github.io/placeholders/images/100x50.jpg'
await img.decode();
logImageData(img, 'load a 100x50 image');
img.width = 250;
img.height = 150;
logImageData(img, 'set the width, height to 250x150');
img.src = 'https://greggman.github.io/placeholders/images/300x200.jpg'
await img.decode();
logImageData(img, 'load a 300x200 image');
};
function logImageData(img, msg) {
log('-----:', msg);
log('img.src', img.src);
log('img.width', img.width);
log('img.height', img.height);
log('img.naturalWidth', img.naturalWidth);
log('img.naturalHeight', img.naturalHeight);
log('img.clientWidth', img.clientWidth);
log('img.clientHeight', img.clientHeight);
log(' ');
}
function log(...args) {
const elem = document.createElement('pre');
elem.textContent = args.join(' ');
document.body.appendChild(elem);
}
main();
{"name":"An Image's sizes","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