Last active
November 20, 2022 00:21
-
-
Save greggman/80195bb8ad9995a6798996e2b02d98d9 to your computer and use it in GitHub Desktop.
An Image's sizes
This file contains 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
pre { margin: 0; } |
This file contains 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
/*bug-in-github-api-content-can-not-be-empty*/ |
This file contains 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
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(); |
This file contains 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
{"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