-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Inconsistent natural image dimensions in IE #324
Changes from all commits
4094262
991f919
d26ab3f
3a5e7ea
d9f7261
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ class ImageBaseViewer extends BaseViewer { | |
return; | ||
} | ||
|
||
this.setOriginalImageSize(this.imageEl); | ||
this.loadUI(); | ||
this.zoom(); | ||
|
||
|
@@ -167,6 +168,42 @@ class ImageBaseViewer extends BaseViewer { | |
this.bindControlListeners(); | ||
} | ||
|
||
/** | ||
* Sets the original image width and height on the img element. Can be removed when | ||
* naturalHeight and naturalWidth attributes work correctly in IE 11. | ||
* | ||
* @private | ||
* @param {Image} imageEl - The image to set the original size attributes on | ||
* @return {Promise} A promise that is resolved if the original image dimensions were set. | ||
*/ | ||
setOriginalImageSize(imageEl) { | ||
const image = imageEl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @JustinHoldstock mentioned at some point he prefers just disabling the eslint rule that warns against this when you mean to explicitly modify the passed in param. We also generally tend to keep the 'El' portion of a name to indicate an HTML Element. Would look like:
|
||
const promise = new Promise((resolve, reject) => { | ||
// Do not bother loading a new image when the natural size attributes exist | ||
if (imageEl.naturalWidth > 0 && imageEl.naturalHeight > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the preferred way of checking whether natural size attributes are available? What happens for 0x0 or 0 by Y / X by 0 images? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For 0x0, the original width and height will be set to 1x1. It is not ideal but the original width/height are used to measure scale. Therefore if they are set to 0 we will have division by 0 that results in scale = Infinity. naturalWidth and naturalHeight are supposed to be supported in IE 9+ but it doesn't appear to hold true. The reason for this PR is due to naturalWidth and naturalHeight being 0x0 on a non-zero image. |
||
image.originalWidth = imageEl.naturalWidth; | ||
image.originalHeight = imageEl.naturalHeight; | ||
resolve(); | ||
} else { | ||
const originalImg = new Image(); | ||
image.originalWidth = 1; | ||
image.originalHeight = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of adding properties directly to an HTML element, it's better to add a data attribute, see: https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
|
||
|
||
originalImg.error = () => { | ||
reject(); | ||
}; | ||
originalImg.onload = () => { | ||
image.originalWidth = originalImg.width || 1; | ||
image.originalHeight = originalImg.height || 1; | ||
resolve(); | ||
}; | ||
originalImg.src = imageEl.src; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to do this? This implementation makes sense for the generic use case, but is probably less efficient for the exact use case you're solving for here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had previously tested out just checking image.width and image.height in the onload function. It appears to have already been styled at that point, hence the need for this. I can go back through and see if there is a point in which the original loaded image can set its data attributes. |
||
} | ||
}); | ||
|
||
return promise; | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// Event Listeners | ||
//-------------------------------------------------------------------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{HTMLElement}