I have a website where I post my p5/processing/visual sketches made using p5js. I made the website from scratch, and it sorta became dated and cumbersome to update. So I figured I would try out ReactJS (NextJS to be specific) for the website scaffolding. It all seemed to work until I noticed that p5 sketches run really slow when loaded through ReactJS. I don't really understand why because two frameworks are independent. Code is the same as far as p5 is concerned. Outside React, they seem to run about 60 fps, with React at about 1-10fps.
I first started the project using P5Wrapper, but eventually switched to modified version like
import React from 'react'
import p5 from 'p5'
export default class P5Container extends React.Component
{
componentDidMount()
{
this.canvas = new p5(this.props.sketch, this.wrapper)
if( this.canvas.myCustomRedrawAccordingToNewPropsHandler )
{
this.canvas.myCustomRedrawAccordingToNewPropsHandler(this.props)
}
}
componentWillReceiveProps(newprops)
{
if(this.props.sketch !== newprops.sketch)
{
this.canvas.remove()
this.canvas = new p5(newprops.sketch, this.wrapper)
}
if( this.canvas.myCustomRedrawAccordingToNewPropsHandler )
{
this.canvas.myCustomRedrawAccordingToNewPropsHandler(newprops)
}
}
componentWillUnmount()
{
this.canvas.remove()
}
render()
{
return <div ref={wrapper => this.wrapper = wrapper}></div>
}
}
And usage like this:
<P5Container sketch={sketch} params={this.props.query} />
I also found an article describing the process here: Medium, but that didn't solve the issue.
I also found older questions about the same thing: here and here, but both were not answered / solved.
Is there something about React frame work that causes slower rendering? Or maybe the way p5 is loaded causes slow downs? any ideas? please halp!