Jump to content

Pixi and React


Petar
 Share

Recommended Posts

I'm playing around with PIXI and React... I have a basic animation running from within a React component... the function that handles this gets called on componentDidMount()... the parent component passes down a prop called valueIsMatched... does anyone see any way that I can get the animation loop to console.log "value is matched" when the parent state changes to true?
 

Quote

componentDidMount() {
    this.pixiInit();
  }
  pixiInit = () => {
    var VELOCITY = 100;
    const stage = new PIXI.Container();
    const renderer = PIXI.autoDetectRenderer(
      500,
      400,
      {view : this.theCanvas}
    );
    const rectangle = new Rectangle();
    stage.addChild(rectangle);
    let lastTime = new Date().getTime();
    requestAnimationFrame(update);
    function update() {
      const currentTime = new Date().getTime();
      const delta = (currentTime-lastTime)/1000;
      rectangle.position.x -= VELOCITY*delta;
      if (valueIsMatched === true) {
        console.log('value is matched');
      }
      renderer.render(stage);
      requestAnimationFrame(update);
      lastTime = currentTime;
    }
  };



Also, I am aware of React-Pixi, but I was not able to get in running with latest React. 

Link to comment
Share on other sites

I was able to solve the issue by doing:

Quote

    const update = () => {
      const currentTime = new Date().getTime();
      const delta = (currentTime-lastTime)/1000;
      rectangle.position.x -= VELOCITY*delta;
      if (valueIsMatched === true) {
        console.log('value is matched');
      }
      renderer.render(stage);
      requestAnimationFrame(update);
      lastTime = currentTime;
    };
    requestAnimationFrame(update);

Using an arrow function gave me access to "this", which gave me access to "this.props", which allowed me to react to changing parent component state from within the animation loop of child component. 

I just had to make sure to only call update() after it was defined, also.

Link to comment
Share on other sites

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...