2021-06-24 19:51:11 +03:00
|
|
|
import React, { createRef } from "react";
|
2021-06-25 11:20:03 +03:00
|
|
|
import "context-filter-polyfill";
|
2021-06-24 19:51:11 +03:00
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
width?: number;
|
|
|
|
height?: number;
|
2021-06-25 11:20:03 +03:00
|
|
|
backgroundImage?: CanvasImageSource;
|
2021-06-24 19:51:11 +03:00
|
|
|
blur?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default class BackdropPanel extends React.PureComponent<IProps> {
|
|
|
|
private canvasRef: React.RefObject<HTMLCanvasElement> = createRef();
|
|
|
|
private ctx: CanvasRenderingContext2D;
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
blur: "60px",
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentDidMount() {
|
|
|
|
this.ctx = this.canvasRef.current.getContext("2d");
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentDidUpdate() {
|
|
|
|
if (this.props.backgroundImage) {
|
|
|
|
requestAnimationFrame(this.refreshBackdropImage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private refreshBackdropImage = (): void => {
|
|
|
|
const { width, height, backgroundImage } = this.props;
|
|
|
|
this.canvasRef.current.width = width;
|
|
|
|
this.canvasRef.current.height = height;
|
|
|
|
|
2021-06-25 11:20:03 +03:00
|
|
|
const imageWidth = (backgroundImage as ImageBitmap).width
|
|
|
|
|| (backgroundImage as HTMLImageElement).naturalWidth;
|
|
|
|
const imageHeight = (backgroundImage as ImageBitmap).height
|
|
|
|
|| (backgroundImage as HTMLImageElement).naturalHeight;
|
|
|
|
|
|
|
|
const destinationX = width - imageWidth;
|
|
|
|
const destinationY = height - imageHeight;
|
2021-06-24 19:51:11 +03:00
|
|
|
|
|
|
|
this.ctx.filter = `blur(${this.props.blur})`;
|
|
|
|
this.ctx.drawImage(
|
|
|
|
backgroundImage,
|
|
|
|
Math.min(destinationX, 0),
|
|
|
|
Math.min(destinationY, 0),
|
2021-06-25 11:20:03 +03:00
|
|
|
Math.max(width, imageWidth),
|
|
|
|
Math.max(height, imageHeight),
|
2021-06-24 19:51:11 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2021-06-25 11:20:03 +03:00
|
|
|
return <canvas
|
|
|
|
ref={this.canvasRef}
|
|
|
|
className="mx_BackdropPanel"
|
|
|
|
/>;
|
2021-06-24 19:51:11 +03:00
|
|
|
}
|
|
|
|
}
|