hexojs/hexo-filter-lqip

Name: hexo-filter-lqip

Owner: Hexo

Description: A Hexo plugins which helps to introduce low quality image placeholders to the theme

Created: 2018-04-17 20:04:56.0

Updated: 2018-05-22 18:49:23.0

Pushed: 2018-05-22 18:49:24.0

Homepage: null

Size: 13135

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

hexo-filter-lqip

A Hexo plugins which helps to introduce low quality image placeholders to the theme

Installation
i hexo-filter-lqip --save
Usage

Install this plugin for the theme and use the view helper to render a placeholder.

lqipFor view helper
For(path_to_asset, options)

Returns a CSS value for background-image property, which is a simplified version of the original image.

Example for EJS


yle="background-image: <%- lqip_for('/content/my-photo.jpg') %>"
iv>
Front-end integration

To make it work as a real placeholder, there must be a piece of JavaScript code, which will eventually replace the placeholder with the original image. It can be done by adding a data attribute with the original image path:


yle="background-image: <%- lqip_for('/content/my-photo.jpg') %>"
ta-lazy-src="/content/my-photo.jpg"
iv>

and replacing the background-image CSS property with the original image once it's loaded:

ction () {
r lazyImages = document.querySelectorAll('[data-lazy-src]')

zyImages.forEach(function (img) {
var url = img.dataset.lazySrc
var image = new Image()
image.onload = function () {
  img.style.backgroundImage = 'url(' + url + ')'
}
image.src = url


For even more improvement, the script could load only images that are visible on the screen.

Available types
Potrace

Type name: potrace

Uses the posterize function from potrace to generate simplified SVG version of the image. The output is optimized with SVGO and inlined.

Color

Type name: color

Plain background, the dominant color extracted from the image.

Configuration

Put your configuration in the theme _config.yml under lqip key. You can also use the overriding theme config feature of Hexo. Available options are the following:

cache

Defaults to 'lqip-cache.json'. Could be a string with a file name of the cache. You can also set to false to disable caching.

Ideally, the cache file should not be checked in into repository.

priority

The priority of the after_generate filter. Defaults to 10. You can find more information about priority in Filter documentation.

default_type

Defaults to potrace. Use this type if not specified as a param to lqip_for helper.

potrace

Configuration specific to potrace type. All keys except canvas_size are passed to the posterize function of potrace

canvas_size: {width:, height:}

Before the image is passed to potrace, it's resized to this size.

Example configuration:
:
fault_type: potrace
trace:
canvas_size:
  width: 140
steps: 2
color: '#dedede'
background: transparent
Example project

You can see it put together in the hexo-lqip-example repository.

Out there in the wild
Inspirations

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.