Create a progressive image loading strategy using the Blurhash technique. The server computes a short hash string, the client decodes it into a small canvas placeholder immediately, then cross-fades to the full image once loaded. Package this in a reusable lazy-img web component.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Planning a custom element that decodes a blurhash into a canvas placeholder, then lazy-loads the full image with a cross-fade transition. The component uses IntersectionObserver and will import the blurhash decode function from a CDN for simplicity.
A BlurHash is a tiny (~20–30 char) string that encodes a low‑res, blurred version of an image. You can compute it on the server when you process/store the image, and send it to the client alongside the image URL.
On the client you decode that hash into pixels and paint them into a canvas immediately, then load the real image lazily and cross‑fade to it.
Below is a self‑contained <lazy-img> custom element that does exactly that. It exposes src, alt, hash, width and height attributes. It uses an IntersectionObserver to lazy‑load the real image and CSS transitions to fade it in over the blurred placeholder.
html
<!-- Include the blurhash decoder; you can also bundle it yourself -->
<script type="module">
import { decode } from 'https://unpkg.com/blurhash@1.1.4/dist/esm/blurhash.js';
class LazyImg extends HTMLElement {
static get observedAttributes() {
return ['src', 'alt', 'hash', 'width', 'height'];
}
constructor() {
super();
this.attachShadow({ mode: 'open' });
this._observer = null;
// Build the DOM
const wrapper = document.createElement('div');
wrapper.classList.add('wrapper');
this._canvas = document.createElement('canvas');
this._canvas.classList.add('placeholder');
this._img = document.createElement('img');
this._img.classList.add('full');
// Slot for fallback content if needed
const slot = document.createElement('slot');
const style = document.createElement('style');
style.textContent = `
:host {
display: inline-block;
position: relative;
overflow: hidden;
}
.wrapper {
position: relative;
width: 100%;
height: 100%;
}
canvas, img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
img.full {
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: opacity 300ms ease;
}
img.full.loaded {
opacity: 1;
}
`;
wrapper.appendChild(this._canvas);
wrapper.appendChild(this._img);
wrapper.appendChild(slot);
this.shadowRoot.append(style, wrapper);
// Bind event
this._onImgLoad = this._onImgLoad.bind(this);
}
connectedCallback() {
this._renderPlaceholder();
this._observe();
}
disconnectedCallback() {
if (this._observer) {
this._observer.disconnect();
}
this._img.removeEventListener('load', this._onImgLoad);
}
attributeChangedCallback(name, oldVal, newVal) {
if (oldVal === newVal) return;
if (name === 'src' && this._img) {
// reset state
this._img.classList.remove('loaded');
this._img.removeAttribute('src');
this._observe(); // re‑observe if src changes
}
if ((name === 'hash' || name === 'width' || name === 'height') && this.isConnected) {
this._renderPlaceholder();
}
if (name === 'alt' && this._img) {
this._img.alt = newVal || '';
}
}
_observe() {
if (this._observer) {
this._observer.disconnect();
}
this._observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
this._loadImage();
this._observer.disconnect();
}
});
}, { rootMargin: '100px' });
this._observer.observe(this);
}
_renderPlaceholder() {
const hash = this.getAttribute('hash');
if (!hash) return;
const w = parseInt(this.getAttribute('width'), 10) || 32;
const h = parseInt(this.getAttribute('height'), 10) || 32;
// Decode the hash to a pixel array and draw to the canvas
try {
const pixels = decode(hash, w, h);
this._canvas.width = w;
this._canvas.height = h;
const ctx = this._canvas.getContext('2d');
const imageData = ctx.createImageData(w, h);
imageData.data.set(pixels);
ctx.putImageData(imageData, 0, 0);
} catch (e) {
console.warn('Failed to decode blurhash', e);
}
}
_loadImage() {
const src = this.getAttribute('src');
if (!src) return;
this._img.alt = this.getAttribute('alt') || '';
this._img.addEventListener('load', this._onImgLoad, { once: true });
this._img.src = src;
}
_onImgLoad() {
this._img.classList.add('loaded');
// Optionally remove the placeholder after fade-in
setTimeout(() => {
if (this._canvas && this._canvas.parentNode) {
this._canvas.remove();
}
}, 350);
}
}
customElements.define('lazy-img', LazyImg);
</script>
<!-- Usage -->
<lazy-img
src="https://example.com/full/image.jpg"
hash="LGFFaXYk^6#M@-5c,1J5@[or[Q6."
width="32"
height="32"
alt="A beautiful scene"
style="width:400px;height:250px">
</lazy-img>
How it works:
Compute and store the BlurHash string for each image on the server (using a library like woltapp/blurhash in your backend language).
Render your pages/API responses with both the src and the hash (and optionally the original image’s aspect ratio to set width/height or CSS).
The <lazy-img> element decodes the hash into a tiny canvas immediately, giving the user instant feedback.
When the element scrolls into view, it starts loading the full image. On load it fades it in over the blurred placeholder.
You can adapt the fade duration, margins for the IntersectionObserver, and placeholder resolution to suit your UX.