Progressive Image Rendering in Vue.js with v-lazy-image

Alex Jover Morales

Often, you come across web pages which have lots of images, and with most of them out of the viewport, the browsers still loading them even thought they’re not visible.

There’s lazy loading, but the downside is that while the images are loading you see an unpleasant empty space. That’s where progressive image rendering comes into play.

Progressive Image Rendering

Progressive image rendering is a technique used to improve the user experience for lazy loaded images by starting loading a very lightweight image as a placeholder while loading the real image, so that the user doesn’t see the empty space until the actual image is loaded.

There are several ways to do the placeholder. Some examples:

  • Using a blur effect on a low-res image, just like Medium or Quora do it.
  • Animating an edged SVG version of the original image. @jmperezperez showed that technique on this talk from Render Conf 2017.
  • Fill the placeholder with a color, just like Google Images or Pinterest.

In this articles we’ll focus on the blur effect placeholder.

Getting Started with v-lazy-image

v-lazy-image is a Vue.js component built on top of the example shown in the article Lazy Image Component using the Intersection Observer API, with some features added.

P.S.: the component is written by yours trully. 😉

First, you need to install it first in a Vue.js app:

$ npm install v-lazy-image

Then, import the component and use it by passing an src property to it:

<template>
  <v-lazy-image src="http://lorempixel.com/400/200/" />
</template>

<script>
import VLazyImage from "v-lazy-image";

export default {
  components: {
    VLazyImage
  }
};
</script>

Just like that, the image will be lazy loaded as soon as it becomes visible. You can pass any attributes like alt and they’ll be added to the <img> tag.

Progressive Rendering on v-lazy-image

v-lazy-image allows to set a placeholder by using the src-placeholder property:

<v-lazy-image
  src="https://cdn-images-1.medium.com/max/1600/1*xjGrvQSXvj72W4zD6IWzfg.jpeg"
  src-placeholder="https://cdn-images-1.medium.com/max/80/1*xjGrvQSXvj72W4zD6IWzfg.jpeg"
  />

Additionally, it exposes the class v-lazy-image-loaded in order to apply any kind of CSS animation and styling. For example, you can easily apply a blur effect:

.v-lazy-image {
  filter: blur(10px);
  transition: filter 0.7s;
}

.v-lazy-image-loaded {
  filter: blur(0);
}

In this demo you can see this one and other funny examples of CSS animations applied to v-lazy-image.

More Complex Examples

v-lazy-image also exposes the @load and @intersect events in case you need more control.

For example, animating the blur filter by CSS is quite heavy, but we can build a more performant version using an SVG:

<template>
  <div>
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="filter hidden">
      <defs>
        <filter id="blur">
          <feGaussianBlur in="SourceGraphic" :stdDeviation="deviation" />
        </filter>
      </defs>
    </svg>
     <v-lazy-image
      :src="src"
      :src-placeholder="srcPlaceholder"
      @load="animate"
    ></v-lazy-image>
  </div>
</template>

<script>
export default {
  props: {
    src: String,
    srcPlaceholder: String,
    blurLevel: {
      type: Number,
      default: 30
    },
    duration: {
      type: Number,
      default: 1000
    }
  },
  data: () => ({ rate: 1 }),
  computed: {
    deviation() {
      return this.blurLevel * this.rate;
    }
  },
  methods: {
    animate() {
      const start = Date.now() + this.duration;

      const step = () => {
        const remaining = start - Date.now();

        if (remaining < 0) {
          this.rate = 0;
        } else {
          this.rate = remaining / this.duration;
          requestAnimationFrame(step);
        }
      };

      requestAnimationFrame(step);
    }
  }
};
</script>

<style scoped>
.filter {
  display: none;
}

.v-lazy-image {
  width: 100%;
  filter: url("#blur");
}
</style>

It gets more complex indeed, but is more performant and you can wrap it in a reusable component like that one.

You can see it running in this Codesandbox.

Wrapping Up

There are multiple and creative ways to do progressive image rendering. v-lazy-image makes that easy by providing a performant and customizable lazy image component, so you don’t have to. If you want to give any feedback, that’d be much appreciated 😉.

Stay cool 🦄

  Tweet It

🕵 Search Results

🔎 Searching...