Skip to content

Commit

Permalink
fix: image width not being set properly (#143)
Browse files Browse the repository at this point in the history
* Fix image width not being set properly

Sometimes it got set to 0 because the original image wasn't loaded yet.

* fix: eslint

Co-authored-by: dicedtomato <[email protected]>
  • Loading branch information
relaxtakenotes and diced authored Jun 5, 2022
1 parent 181833d commit 74f3b3f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/pages/[...id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ export default function EmbeddedImage({ image, user, pass }) {
const updateImage = async (url?: string) => {
const imageEl = document.getElementById('image_content') as HTMLImageElement;

const original = new Image;
original.src = url || dataURL('/r');

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;
});

img.src = url || dataURL('/r');
if (url) {
imageEl.src = url;
}

// Sometimes gives blank image on first load
if (original.width > innerWidth) imageEl.width = Math.floor(original.width * Math.min((innerHeight / original.height), (innerWidth / original.width)));
else imageEl.width = original.width;
};
};

useEffect(() => {
Expand Down

0 comments on commit 74f3b3f

Please sign in to comment.