Skip to content
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: Image viewer flickering in IE11 during reset zoom #594

Merged
merged 16 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions src/lib/viewers/image/ImageViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,26 @@ class ImageViewer extends ImageBaseViewer {
zoom(type) {
let newWidth;
let newHeight;
const imageCurrentDimensions = this.imageEl.getBoundingClientRect(); // Getting bounding rect does not ignore transforms / rotates
let imageCurrentDimensions;
if (type === 'reset') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we keep this in the switch statement? You can set imageCurrentDimensions here and then update it in the switch statement if we are resetting. It feels a little weird to check for the 'reset' case and then switch on type right after.

// when it is a reset, take the original dimensions of the image
// rather than setting the resetting height and width and calling
// getBoundingClientRect as it will cause flickering on IE11
const height = this.imageEl.getAttribute('originalHeight');
const width = this.imageEl.getAttribute('originalWidth');
imageCurrentDimensions = {
height,
width
};
} else {
imageCurrentDimensions = this.imageEl.getBoundingClientRect(); // Getting bounding rect does not ignore transforms / rotates
}

const { height, width } = imageCurrentDimensions;
const aspect = width / height;
const viewport = {
width: this.wrapperEl.clientWidth - IMAGE_PADDING,
height: this.wrapperEl.clientHeight - IMAGE_PADDING
width: this.wrapperEl.offsetWidth - 2 * IMAGE_PADDING,
height: this.wrapperEl.offsetHeight - 2 * IMAGE_PADDING
};

// For multi page tifs, we always modify the width, since its essentially a DIV and not IMG tag.
Expand All @@ -157,29 +171,12 @@ class ImageViewer extends ImageBaseViewer {
newHeight = height / IMAGE_ZOOM_SCALE;
}
break;

case 'reset':
// Reset the dimensions to their original values by removing overrides
// Doing so will make the browser render the image in its natural size
// Then we can proceed by recalculating stuff from that natural size.
this.imageEl.style.width = '';
this.imageEl.style.height = '';

this.adjustImageZoomPadding();

// Image may still overflow the page, so do the default zoom by calling zoom again
// This will go through the same workflow but end up in another case block.
this.zoom();

// Kill further execution
return;
/* istanbul ignore next */
default:
// If the image is overflowing the viewport, figure out by how much
// Then take that aspect that reduces the image the maximum (hence min ratio) to fit both width and height
if (width > viewport.width || height > viewport.height) {
const ratio = Math.min(viewport.width / width, viewport.height / height);

if (modifyWidthInsteadOfHeight) {
newWidth = width * ratio;
} else {
Expand Down
30 changes: 26 additions & 4 deletions src/lib/viewers/image/__tests__/ImageViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,38 @@ describe('lib/viewers/image/ImageViewer', () => {
});

it('should reset dimensions and adjust padding when called with reset', () => {
image.imageEl.style.width = '10px';
image.imageEl.style.height = '20px';
image.imageEl.style.width = '1000px';
image.imageEl.style.height = '2000px';
const naturalHeight = 10;
const naturalWidth = 5;
image.imageEl.setAttribute('originalHeight', naturalHeight);
image.imageEl.setAttribute('originalWidth', naturalWidth);

sandbox.spy(image, 'zoom');

image.zoom('reset');

expect(image.imageEl.style.width).to.equal('');
expect(image.imageEl.style.height).to.equal(`${naturalHeight}px`);
expect(stubs.adjustZoom).to.be.called;
});

it('when rotated should reset dimensions and adjust padding when called with reset', () => {
image.currentRotationAngle = -90;
image.imageEl.style.width = '1000px';
image.imageEl.style.height = '2000px';
const naturalHeight = 5;
const naturalWidth = 10;
image.imageEl.setAttribute('originalHeight', naturalHeight);
image.imageEl.setAttribute('originalWidth', naturalWidth);

sandbox.spy(image, 'zoom');

image.zoom('reset');

expect(image.imageEl.style.width).to.equal('');
expect(image.imageEl.style.height).to.equal('');
expect(image.imageEl.style.height).to.equal(`${naturalHeight}px`);
expect(stubs.adjustZoom).to.be.called;
expect(image.zoom).to.be.calledWith();
});
});

Expand Down