12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <template>
- <canvas ref="canvas" :width="parseNumber(width)" :height="parseNumber(height)" />
- </template>
- <script type="text/javascript">
- import { decode } from 'blurhash';
- export default {
- props: {
- hash: {
- type: String,
- required: true
- },
- width: {
- type: [Number, String],
- default: 32
- },
- height: {
- type: [Number, String],
- default: 32
- },
- punch: {
- type: Number,
- default: 1
- }
- },
- mounted() {
- this.draw();
- },
- updated() {
- // this.draw();
- },
- beforeDestroy() {
- // this.hash = null;
- // this.$refs.canvas = null;
- },
- methods: {
- parseNumber(val) {
- return typeof val === 'number' ? val : parseInt(val, 10);
- },
- draw() {
- const width = this.parseNumber(this.width);
- const height = this.parseNumber(this.height);
- const punch = this.parseNumber(this.punch);
- const pixels = decode(this.hash, width, height, punch);
- const ctx = this.$refs.canvas.getContext('2d');
- const imageData = ctx.createImageData(width, height);
- imageData.data.set(pixels);
- ctx.putImageData(imageData, 0, 0);
- },
- }
- }
- </script>
|