<template>
<div class="xubzgfgb" :class="{ cover }" :title="title">
<canvas v-if="!loaded" ref="canvas" :width="size" :height="size" :title="title"/>
<img v-if="src" :src="src" :title="title" :alt="alt" @load="onLoad"/>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { decode } from 'blurhash';
export default defineComponent({
props: {
src: {
type: String,
required: false,
default: null
},
hash: {
required: true
alt: {
default: '',
title: {
default: null,
size: {
type: Number,
default: 64
cover: {
type: Boolean,
default: true,
}
data() {
return {
loaded: false,
};
mounted() {
this.draw();
methods: {
draw() {
if (this.hash == null) return;
const pixels = decode(this.hash, this.size, this.size);
const ctx = (this.$refs.canvas as HTMLCanvasElement).getContext('2d');
const imageData = ctx!.createImageData(this.size, this.size);
imageData.data.set(pixels);
ctx!.putImageData(imageData, 0, 0);
onLoad() {
this.loaded = true;
});
</script>
<style lang="scss" scoped>
.xubzgfgb {
position: relative;
width: 100%;
height: 100%;
> canvas,
> img {
display: block;
> canvas {
position: absolute;
object-fit: cover;
object-fit: contain;
&.cover {
</style>