-
Notifications
You must be signed in to change notification settings - Fork 15
Update image dimensions inside of a timeout to ensure image replacement is rendered on screen before measuring #48
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
Changes from 3 commits
ca13105
af6d9f5
f555afa
fbda87f
1f42004
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 |
|---|---|---|
|
|
@@ -64,6 +64,29 @@ window.Gaussholder = (function (header) { | |
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Recalculate image ratio when an image renders on the page. | ||
| * | ||
| * @param {HTMLImageElement} Image element to recalculate dimensions for. | ||
| */ | ||
| var calculateDimensionStyle = function (element) { | ||
| var actual = element.getBoundingClientRect(); | ||
| var size = element.dataset.gaussholderSize.split(','); | ||
|
roborourke marked this conversation as resolved.
Outdated
|
||
| var width = parseInt( size[0], 10 ), height = parseInt( size[1], 10 ); | ||
|
|
||
| if ( actual.width < width ) { | ||
| // Rescale, keeping the aspect ratio | ||
| height = height * ( actual.width / width ); | ||
| width = actual.width; | ||
| } else if ( actual.height < height ) { | ||
| // Rescale, keeping the aspect ratio | ||
| width = width * ( actual.height / height ); | ||
| height = actual.height; | ||
| } | ||
|
|
||
| element.style.cssText += 'width: ' + width + 'px; height: ' + height + 'px;'; | ||
| }; | ||
|
|
||
| /** | ||
| * Render placeholder for an image | ||
| * | ||
|
|
@@ -82,25 +105,14 @@ window.Gaussholder = (function (header) { | |
| element.style.height = final[1] + 'px'; | ||
|
|
||
| // ...then recalculate based on what it actually renders as | ||
| var original = [ final[0], final[1] ]; | ||
| if ( element.width < final[0] ) { | ||
| // Rescale, keeping the aspect ratio | ||
| final[0] = element.width; | ||
| final[1] = final[1] * ( final[0] / original[0] ); | ||
| } else if ( element.height < final[1] ) { | ||
| // Rescale, keeping the aspect ratio | ||
| final[1] = element.height; | ||
| final[0] = final[0] * ( final[1] / original[1] ); | ||
| } | ||
|
|
||
| // Set dimensions, _again_ | ||
| element.style.width = final[0] + 'px'; | ||
| element.style.height = final[1] + 'px'; | ||
| calculateDimensionStyle( element ); | ||
|
|
||
| // Schedule an observer to update on any rendering changes. | ||
| render(canvas, element.dataset.gaussholder.split(','), final, function () { | ||
| // Load in as our background image | ||
| element.style.backgroundImage = 'url("' + canvas.toDataURL() + '")'; | ||
| element.style.backgroundRepeat = 'no-repeat'; | ||
| setTimeout( function() { calculateDimensionStyle( element ); }, 200 ); | ||
|
Contributor
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. Could we use the image element load event perhaps? This still seems like it could be brittle
Contributor
Author
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. This is already happening in the image's I could probably remove the 200ms timeout and just leave it in a timeout or a requestAnimationFrame to make sure the image renders on screen before measuring it. You're right, this does feel brittle - I'll clean it up and see what I can do to make the callback happen as soon as possible.
Contributor
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. Ah ok, so if this is really just adding a small extra buffer right now before the blurred image is getting swapped out then I think it's ok. I'll test it out on local and see what happens to see if I have any other ideas. |
||
| }); | ||
| }; | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.