Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Last active January 8, 2019 17:19
Show Gist options
  • Save ajcrites/d694f802e6005398e227be19f804dc14 to your computer and use it in GitHub Desktop.
Save ajcrites/d694f802e6005398e227be19f804dc14 to your computer and use it in GitHub Desktop.
export class TimesTable extends React.Component<{ timesTable, pointCount, lineColor }> {
canvasRef = React.createRef();
redraw = () => {
this.canvasRef.current.height = window.innerHeight;
this.canvasRef.current.width = window.innerWidth;
this.forceUpdate();
};
componentDidMount() {
const canvas = this.canvasRef.current;
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
window.addEventListener('resize', this.redraw);
}
componentWillUnmount() {
window.removeEventListener('resize', this.redraw);
}
componentDidUpdate() {
// Setting width/height even to the same value clears the canvas
this.canvasRef.current.width = this.canvasRef.current.width;
}
render() {
return (
<canvas ref={this.canvasRef}>
<Viz ctx={this.canvasRef.current ? this.canvasRef.current.getContext('2d') : null} {...this.props} />
</canvas>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment