Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
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
40 changes: 26 additions & 14 deletions assets/gaussholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Comment thread
roborourke marked this conversation as resolved.
Outdated
var actual = element.getBoundingClientRect();
var size = element.dataset.gaussholderSize.split(',');
Comment thread
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
*
Expand All @@ -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 );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is already happening in the image's onload event handler. The timeout here is just trying to work around some issues where the dimensions weren't calculated properly, because apparently the "onload" event fires when the file load is complete, but before the DOM rendering is necessarily complete.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

});
};

Expand Down
2 changes: 1 addition & 1 deletion assets/gaussholder.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.