1
Fork 0
mirror of https://github.com/diced/zipline.git synced 2025-04-11 23:31:17 -05:00

fix: image resizing (#527)

This commit is contained in:
diced 2024-02-26 20:21:11 -08:00
parent 1816e13879
commit 5b88b59724
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1

View file

@ -63,11 +63,15 @@ export default function EmbeddedFile({
const img = new Image();
img.addEventListener('load', function () {
if (this.naturalWidth > innerWidth)
imageEl.width = Math.floor(
this.naturalWidth * Math.min(innerHeight / this.naturalHeight, innerWidth / this.naturalWidth),
);
else imageEl.width = this.naturalWidth;
// my best attempt of recreating https://searchfox.org/mozilla-central/source/dom/html/ImageDocument.cpp#271-276
// and it actually works
const ratio = Math.min(innerHeight / this.naturalHeight, innerWidth / this.naturalWidth);
const newWidth = Math.max(1, Math.floor(ratio * this.naturalWidth));
const newHeight = Math.max(1, Math.floor(ratio * this.naturalHeight));
imageEl.width = newWidth;
imageEl.height = newHeight;
});
img.src = url || dataURL('/r');