675 B · text History 1ba5ec3
// Paints a photo's blurhash onto the <canvas> it's set on, as the
// placeholder shown until the image above it has loaded
// (CartosnapAppWeb.PhotoComponents.photo_image).
import {decode, isBlurhashValid} from "blurhash"

const SIZE = 32

export default {
  mounted() { this.paint() },
  updated() { this.paint() },

  paint() {
    const hash = this.el.dataset.hash
    if (!hash || hash === this.painted || !isBlurhashValid(hash).result) return
    const pixels = decode(hash, SIZE, SIZE)
    const ctx = this.el.getContext("2d")
    const image = ctx.createImageData(SIZE, SIZE)
    image.data.set(pixels)
    ctx.putImageData(image, 0, 0)
    this.painted = hash
  }
}