Skip to content

Commit

Permalink
fix(Canvas): cleared the interval of heartbeatDetectionCanvasSize whe…
Browse files Browse the repository at this point in the history
…n willUnmount for lindelof#7
  • Loading branch information
RyanRoll committed Feb 9, 2020
1 parent 57b41d4 commit 150a1c2
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/particles/Canvas.jsx
Expand Up @@ -12,7 +12,6 @@ export default class Canvas extends React.Component {
componentDidMount() {
setTimeout(() => {
this.initCanvas();
this.resize = this.resize.bind(this);
window.addEventListener("resize", this.resize);
}, 100);

Expand All @@ -33,20 +32,23 @@ export default class Canvas extends React.Component {
}

heartbeatDetectionCanvasSize(canvas) {
setInterval(() => {
const newHeight = this.canvasRef.current.clientHeight;
if (newHeight !== this.size.height) {
const { width, height } = this.setCanvasSize(canvas);
this.props.onResize && this.props.onResize(width, height);
this.interval = setInterval(() => {
if (this.canvasRef.current) {
const newHeight = this.canvasRef.current.clientHeight;
if (newHeight !== this.size.height) {
const { width, height } = this.setCanvasSize(canvas);
this.props.onResize && this.props.onResize(width, height);
}
}
}, 1000 / 10);
}

componentWillUnmount() {
window.clearInterval(this.interval);
window.removeEventListener("resize", this.resize);
}

resize() {
resize = () => {
const canvas = this.canvasRef.current;
const { width, height } = this.setCanvasSize(canvas);
this.props.onResize && this.props.onResize(width, height);
Expand Down Expand Up @@ -85,15 +87,15 @@ export default class Canvas extends React.Component {
return style;
}

handleMouseDown(e) {
handleMouseDown = e => {
this.props.onMouseDown && this.props.onMouseDown(e);
}

render() {
return (
<canvas
ref={this.canvasRef}
onMouseDown={this.handleMouseDown.bind(this)}
onMouseDown={this.handleMouseDown}
style={this.getStyle()}
/>
);
Expand Down

0 comments on commit 150a1c2

Please sign in to comment.