/
BlurhashCanvas.tsx
46 lines (36 loc) · 1.02 KB
/
BlurhashCanvas.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import * as React from 'react';
import { decode } from 'blurhash';
export type Props = React.CanvasHTMLAttributes<HTMLCanvasElement> & {
hash: string;
height?: number;
punch?: number;
width?: number;
};
export default class BlurhashCanvas extends React.PureComponent<Props> {
static defaultProps = {
height: 128,
width: 128,
};
canvas: HTMLCanvasElement = null;
componentDidUpdate() {
this.draw();
}
handleRef = (canvas: HTMLCanvasElement) => {
this.canvas = canvas;
this.draw();
};
draw = () => {
const { hash, height, punch, width } = this.props;
if (this.canvas) {
const pixels = decode(hash, width, height, punch);
const ctx = this.canvas.getContext('2d');
const imageData = ctx.createImageData(width, height);
imageData.data.set(pixels);
ctx.putImageData(imageData, 0, 0);
}
};
render() {
const { hash, height, width, ...rest } = this.props;
return <canvas {...rest} height={height} width={width} ref={this.handleRef} />;
}
}