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

Use browser lazy loading attribute #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
163 changes: 39 additions & 124 deletions assets/gaussholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,134 +70,65 @@ window.Gaussholder = (function (header) {
* @param {HTMLImageElement} element Element to render placeholder for
*/
var handleElement = function (element) {
if ( element.complete ) {
return;
}

if ( ! ( 'gaussholder' in element.dataset ) ) {
return;
}

var canvas = document.createElement('canvas');
var final = element.dataset.gaussholderSize.split(',');

// Set the dimensions...
element.style.width = final[0] + 'px';
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';

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';
element.style.backgroundSize = 'cover';
element.style.backgroundImage = 'url("' + canvas.toDataURL() + '")';
});

element.onload = function () {
loadOriginal(element);
};
};

var loadOriginal = function (element) {
if ( ! ( 'originalsrc' in element.dataset ) && ! ( 'originalsrcset' in element.dataset ) ) {
return;
}

var data = element.dataset.gaussholderSize.split(','),
radius = parseInt( data[2] );

// Load our image now
var img = new Image();
// Filter property to use
var filterProp = ( 'webkitFilter' in element.style ) ? 'webkitFilter' : 'filter';
element.style[ filterProp ] = 'blur(' + radius * 0.5 + 'px)';

// Ensure blur doesn't bleed past image border
element.style.clipPath = 'url(#gaussclip)'; // Current FF
element.style.clipPath = 'inset(0)'; // Standard
element.style.webkitClipPath = 'inset(0)'; // WebKit

// Clear placeholder temporary image
// (We do this after setting the source, as doing it before can
// cause a tiny flicker)
element.style.backgroundImage = '';
element.style.backgroundRepeat = '';

var start = 0;
var anim = function (ts) {
if ( ! start ) start = ts;
var diff = ts - start;
if ( diff > fadeDuration ) {
element.style[ filterProp ] = '';
element.style.clipPath = '';
element.style.webkitClipPath = '';
return;
}

if ( element.dataset.originalsrc ) {
img.src = element.dataset.originalsrc;
}
if ( element.dataset.originalsrcset ) {
img.srcset = element.dataset.originalsrcset;
}
var effectiveRadius = radius * ( 1 - ( diff / fadeDuration ) );

img.onload = function () {
// Filter property to use
var filterProp = ( 'webkitFilter' in element.style ) ? 'webkitFilter' : 'filter';
element.style[ filterProp ] = 'blur(' + radius * 0.5 + 'px)';

// Ensure blur doesn't bleed past image border
element.style.clipPath = 'url(#gaussclip)'; // Current FF
element.style.clipPath = 'inset(0)'; // Standard
element.style.webkitClipPath = 'inset(0)'; // WebKit

// Set the actual source
element.src = img.src;
element.srcset = img.srcset;

// Cleaning source
element.dataset.originalsrc = '';
element.dataset.originalsrcset = '';

// Clear placeholder temporary image
// (We do this after setting the source, as doing it before can
// cause a tiny flicker)
element.style.backgroundImage = '';
element.style.backgroundRepeat = '';

var start = 0;
var anim = function (ts) {
if ( ! start ) start = ts;
var diff = ts - start;
if ( diff > fadeDuration ) {
element.style[ filterProp ] = '';
element.style.clipPath = '';
element.style.webkitClipPath = '';
return;
}

var effectiveRadius = radius * ( 1 - ( diff / fadeDuration ) );

element.style[ filterProp ] = 'blur(' + effectiveRadius * 0.5 + 'px)';
window.requestAnimationFrame(anim);
};
element.style[ filterProp ] = 'blur(' + effectiveRadius * 0.5 + 'px)';
window.requestAnimationFrame(anim);
};
};

var loadLazily = [];
var threshold = 1200;
var lastRun = 0,
loopTimeout = null;

var scrollHandler = function () {
var now = Date.now();
if ( ( lastRun + 40 ) > now ) {
if ( loopTimeout ) {
return;
}
loopTimeout = window.setTimeout(scrollHandler, 40);
return;
}
lastRun = now;
loopTimeout && (loopTimeout = null);

var next = [];
for (var i = loadLazily.length - 1; i >= 0; i--) {
var img = loadLazily[i];
var shouldShow = img.getBoundingClientRect().top <= ( window.innerHeight + threshold );
if ( ! shouldShow ) {
next.push(img);
continue;
}

loadOriginal(img);
}
loadLazily = next;
if (loadLazily.length < 1) {
window.removeEventListener('scroll', scrollHandler);
}
window.requestAnimationFrame(anim);
};

/**
Expand All @@ -207,23 +138,7 @@ window.Gaussholder = (function (header) {
var images = document.getElementsByTagName('img');

for (var i = images.length - 1; i >= 0; i--) {
var img = images[i];

// Ensure the blank GIF has loaded first
if ( img.complete ) {
handleElement(img);
} else {
img.onload = function () {
handleElement(this);
}
}
}

loadLazily = images;
scrollHandler();

if (loadLazily.length > 0) {
window.addEventListener('scroll', scrollHandler);
handleElement(images[i]);
}
};

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.

16 changes: 2 additions & 14 deletions inc/frontend/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@ function mangle_images( $content ) {

$new_attrs = array();

// Replace src with our blank GIF
$new_attrs[] = 'src="' . esc_attr( $blank_url ) . '"';

// Remove srcset
$new_attrs[] = 'srcset=""';

// Add the actual placeholder
$placeholder = Gaussholder\get_placeholder( $id, $size );
$new_attrs[] = 'data-gaussholder="' . esc_attr( $placeholder ) . '"';
Expand Down Expand Up @@ -163,14 +157,8 @@ function mangle_images( $content ) {
);

$mangled_tag = str_replace(
array(
' srcset="',
' src="',
),
array(
' data-originalsrcset="',
' ' . implode( ' ', $new_attrs ) . ' data-originalsrc="',
),
' src="',
' ' . implode( ' ', $new_attrs ) . ' src="',
$tag
);

Expand Down