question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

This is great! Thank you so much for this.

I noticed that when the browser window is resized it causes the canvas to stretch/shrink which causes the confetti particles to look pretty ugly. I solved it in user-land using the code below. But I was wondering if this wouldn’t better be solved by the library itself?

I wouldn’t mind opening a PR, I just wanted to get your input first before I work on it.

  constructor(props) {
    super(props);

    this.state = {
      confetti: {
        width: '100%',
        height: '100%',
      },
    };

    this.updateDimensions = this.updateDimensions.bind(this);
  }

  componentDidMount() {
    window.addEventListener('resize', this.updateDimensions);
  }

  componentWillUnmount() {
    window.removeEventListener('resize', this.updateDimensions);
  }

  updateDimensions() {
    const main = document.querySelectorAll('#main')[0].getBoundingClientRect();
    this.setState({
      confetti: {
        width: main.width,
        height: main.height,
      },
    });
  }

  render() {
    return (
      <App
        {...this.props}
        title="Registration Complete"
        innerRef={(c) => { this.app = c; }}
      >
        <Confetti
          gravity={0.05}
          numberOfPieces={100}
          colors={[
            'rgba(255, 255, 255, 0.1)',
          ]}
          style={{ top: '-1px' }} // fix bug that shows black line
          {...this.state.confetti} // allows for browser resizing without stretching
        />
        <FlexHelper>
          <p><strong>Registration Complete</strong></p>
          <p>
            ...
          </p>
        </FlexHelper>
      </App>
    );
  }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
alamproscommented, Jan 4, 2017

@aesopwolf Glad you’re getting some use out of it and thanks for the suggestion!

In the interest of keeping the component simple, I think it might be best to keep all the dimension operations external.

I actually just solved this same problem in another project using react-dimensions. Very helpful little util.

import React from 'react'
import Confetti from 'react-confetti'
import Dimensions from 'react-dimensions'

export default Dimensions()(class MyComponent extends React.PureComponent {
  render() {
    return (
      <Confetti
        width={this.props.containerWidth}
        height={this.props.containerHeight}
      />
    )
  }
})
0reactions
alamproscommented, Feb 23, 2017

Thanks @adrianmcli !

I just updated the demo to actually use the react-dimensions also.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Window: resize event - Web APIs | MDN
The resize event fires when the document view (window) has been resized. This event is not cancelable and does not bubble.
Read more >
JavaScript window resize event - Stack Overflow
You should use ResizeObserver. It is a browser-native solution that has a much better performance than to use the resize event. In addition,...
Read more >
.resize() | jQuery API Documentation
Description: Bind an event handler to the "resize" JavaScript event, ... The resize event is sent to the window element when the size...
Read more >
onresize Event - W3Schools
The onresize event occurs when the browser window has been resized. Tip: To get the size of an element, use the clientWidth, clientHeight,...
Read more >
How to resize a window - Computer Hope
Press Alt + Spacebar to open the window's menu. · If the window is maximized, arrow down to Restore and press Enter ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found