How to implement a design with react-spring?











up vote
0
down vote

favorite












In the project's repo the react-redux app is set with CSS in JS files. Now I'm supposed to animate pictures with mouse hover just like this website : https://www.madwell.com/



The component was initially a functional component and I have changed that to a class based component as follows:



```



class BannerContainer extends React.Component{
constructor(props){
super(props);
this.state = {
x: 0,
y: 0
};

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

componentDidMount(){
window.addEventListener("mousemove", this.handleMouseMove);
}

componentWillUnmount(){
window.removeEventListener('mousemove', this.handleMouseMove);
}

handleMouseMove({ x, y }){
this.setState({
x : x / window.innerWidth,
y : y / window.innerHeight
})
}

render(){
const { banner = {} } = this.props;

const {
title = '&nbsp;<br>&nbsp;',
text = '&nbsp;<br>&nbsp;',
image__google_350x80 = '',
image__app_350x80 = '',
image__bg1_1166x1878 = '',
image__bg2_961x1782 = '',
image__curve_730x151 = ''
} = banner;

return (
<Container>
<BGPatch>
{console.log(this.state.x , this.state.y)}
<img src={'/images/bg_purple.jpg'} alt="" />
</BGPatch>


```



In this example I am able to get listen to the mouse-move event and and get the x & y coordinates accordingly. But now I have to use react-spring library to implement it so how do I do that? Also the CSS should be written in separate JS file for each component where as in react-spring example they directly modify the opacity or trasform in the Spring component directly as well which is what I don't want



the spring component example given on their docs



<Spring from={{ opacity: 0 }} to={{ opacity: 1 }}>
{props => <div style={props}>hello</div>}
</Spring>









share|improve this question




























    up vote
    0
    down vote

    favorite












    In the project's repo the react-redux app is set with CSS in JS files. Now I'm supposed to animate pictures with mouse hover just like this website : https://www.madwell.com/



    The component was initially a functional component and I have changed that to a class based component as follows:



    ```



    class BannerContainer extends React.Component{
    constructor(props){
    super(props);
    this.state = {
    x: 0,
    y: 0
    };

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

    componentDidMount(){
    window.addEventListener("mousemove", this.handleMouseMove);
    }

    componentWillUnmount(){
    window.removeEventListener('mousemove', this.handleMouseMove);
    }

    handleMouseMove({ x, y }){
    this.setState({
    x : x / window.innerWidth,
    y : y / window.innerHeight
    })
    }

    render(){
    const { banner = {} } = this.props;

    const {
    title = '&nbsp;<br>&nbsp;',
    text = '&nbsp;<br>&nbsp;',
    image__google_350x80 = '',
    image__app_350x80 = '',
    image__bg1_1166x1878 = '',
    image__bg2_961x1782 = '',
    image__curve_730x151 = ''
    } = banner;

    return (
    <Container>
    <BGPatch>
    {console.log(this.state.x , this.state.y)}
    <img src={'/images/bg_purple.jpg'} alt="" />
    </BGPatch>


    ```



    In this example I am able to get listen to the mouse-move event and and get the x & y coordinates accordingly. But now I have to use react-spring library to implement it so how do I do that? Also the CSS should be written in separate JS file for each component where as in react-spring example they directly modify the opacity or trasform in the Spring component directly as well which is what I don't want



    the spring component example given on their docs



    <Spring from={{ opacity: 0 }} to={{ opacity: 1 }}>
    {props => <div style={props}>hello</div>}
    </Spring>









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      In the project's repo the react-redux app is set with CSS in JS files. Now I'm supposed to animate pictures with mouse hover just like this website : https://www.madwell.com/



      The component was initially a functional component and I have changed that to a class based component as follows:



      ```



      class BannerContainer extends React.Component{
      constructor(props){
      super(props);
      this.state = {
      x: 0,
      y: 0
      };

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

      componentDidMount(){
      window.addEventListener("mousemove", this.handleMouseMove);
      }

      componentWillUnmount(){
      window.removeEventListener('mousemove', this.handleMouseMove);
      }

      handleMouseMove({ x, y }){
      this.setState({
      x : x / window.innerWidth,
      y : y / window.innerHeight
      })
      }

      render(){
      const { banner = {} } = this.props;

      const {
      title = '&nbsp;<br>&nbsp;',
      text = '&nbsp;<br>&nbsp;',
      image__google_350x80 = '',
      image__app_350x80 = '',
      image__bg1_1166x1878 = '',
      image__bg2_961x1782 = '',
      image__curve_730x151 = ''
      } = banner;

      return (
      <Container>
      <BGPatch>
      {console.log(this.state.x , this.state.y)}
      <img src={'/images/bg_purple.jpg'} alt="" />
      </BGPatch>


      ```



      In this example I am able to get listen to the mouse-move event and and get the x & y coordinates accordingly. But now I have to use react-spring library to implement it so how do I do that? Also the CSS should be written in separate JS file for each component where as in react-spring example they directly modify the opacity or trasform in the Spring component directly as well which is what I don't want



      the spring component example given on their docs



      <Spring from={{ opacity: 0 }} to={{ opacity: 1 }}>
      {props => <div style={props}>hello</div>}
      </Spring>









      share|improve this question















      In the project's repo the react-redux app is set with CSS in JS files. Now I'm supposed to animate pictures with mouse hover just like this website : https://www.madwell.com/



      The component was initially a functional component and I have changed that to a class based component as follows:



      ```



      class BannerContainer extends React.Component{
      constructor(props){
      super(props);
      this.state = {
      x: 0,
      y: 0
      };

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

      componentDidMount(){
      window.addEventListener("mousemove", this.handleMouseMove);
      }

      componentWillUnmount(){
      window.removeEventListener('mousemove', this.handleMouseMove);
      }

      handleMouseMove({ x, y }){
      this.setState({
      x : x / window.innerWidth,
      y : y / window.innerHeight
      })
      }

      render(){
      const { banner = {} } = this.props;

      const {
      title = '&nbsp;<br>&nbsp;',
      text = '&nbsp;<br>&nbsp;',
      image__google_350x80 = '',
      image__app_350x80 = '',
      image__bg1_1166x1878 = '',
      image__bg2_961x1782 = '',
      image__curve_730x151 = ''
      } = banner;

      return (
      <Container>
      <BGPatch>
      {console.log(this.state.x , this.state.y)}
      <img src={'/images/bg_purple.jpg'} alt="" />
      </BGPatch>


      ```



      In this example I am able to get listen to the mouse-move event and and get the x & y coordinates accordingly. But now I have to use react-spring library to implement it so how do I do that? Also the CSS should be written in separate JS file for each component where as in react-spring example they directly modify the opacity or trasform in the Spring component directly as well which is what I don't want



      the spring component example given on their docs



      <Spring from={{ opacity: 0 }} to={{ opacity: 1 }}>
      {props => <div style={props}>hello</div>}
      </Spring>






      javascript css reactjs animation react-spring






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 at 7:25









      WC2

      1989




      1989










      asked Nov 15 at 7:00









      Divyesh Parmar

      169




      169
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Parallaxing effects are relatively easy to do if you have mouse coordinates, here's an example with a similar effect: https://twitter.com/0xca0a/status/1058505720574959616



          It uses hooks, but the same thing applies to a regular spring. Though this is probably a good example even, because it doesn't re-render the component on mousemove, which is very fast. You wouldn't use rotates in your case, translates i guess, but the point is, you receive x/y and use it to interpolate your images into position.



          EDIT:
          I've forked the example, this version uses translates: https://codesandbox.io/embed/r5x34869vq






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314014%2fhow-to-implement-a-design-with-react-spring%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            Parallaxing effects are relatively easy to do if you have mouse coordinates, here's an example with a similar effect: https://twitter.com/0xca0a/status/1058505720574959616



            It uses hooks, but the same thing applies to a regular spring. Though this is probably a good example even, because it doesn't re-render the component on mousemove, which is very fast. You wouldn't use rotates in your case, translates i guess, but the point is, you receive x/y and use it to interpolate your images into position.



            EDIT:
            I've forked the example, this version uses translates: https://codesandbox.io/embed/r5x34869vq






            share|improve this answer



























              up vote
              1
              down vote



              accepted










              Parallaxing effects are relatively easy to do if you have mouse coordinates, here's an example with a similar effect: https://twitter.com/0xca0a/status/1058505720574959616



              It uses hooks, but the same thing applies to a regular spring. Though this is probably a good example even, because it doesn't re-render the component on mousemove, which is very fast. You wouldn't use rotates in your case, translates i guess, but the point is, you receive x/y and use it to interpolate your images into position.



              EDIT:
              I've forked the example, this version uses translates: https://codesandbox.io/embed/r5x34869vq






              share|improve this answer

























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                Parallaxing effects are relatively easy to do if you have mouse coordinates, here's an example with a similar effect: https://twitter.com/0xca0a/status/1058505720574959616



                It uses hooks, but the same thing applies to a regular spring. Though this is probably a good example even, because it doesn't re-render the component on mousemove, which is very fast. You wouldn't use rotates in your case, translates i guess, but the point is, you receive x/y and use it to interpolate your images into position.



                EDIT:
                I've forked the example, this version uses translates: https://codesandbox.io/embed/r5x34869vq






                share|improve this answer














                Parallaxing effects are relatively easy to do if you have mouse coordinates, here's an example with a similar effect: https://twitter.com/0xca0a/status/1058505720574959616



                It uses hooks, but the same thing applies to a regular spring. Though this is probably a good example even, because it doesn't re-render the component on mousemove, which is very fast. You wouldn't use rotates in your case, translates i guess, but the point is, you receive x/y and use it to interpolate your images into position.



                EDIT:
                I've forked the example, this version uses translates: https://codesandbox.io/embed/r5x34869vq







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 at 11:00

























                answered Nov 15 at 9:56









                hpalu

                1779




                1779






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314014%2fhow-to-implement-a-design-with-react-spring%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents