DEV Community

Discussion on: Integrating P5.js with React

Collapse
 
witherc0d3 profile image
WITHERC0D3

Hello, thanks so much for this constructive document answer. I am new to react and i am developing a project with my friends. I have a doubt regarding the same topic. is there a way to do this as a functional component?

Collapse
 
anshul16 profile image
Anshul Sachdev

import React, { useEffect } from 'react';
import p5 from 'p5';

const App = () => {

const myRef = React.createRef();

const Sketch = (p) => {

 p.setup = () => {
  p.createCanvas(300, 200);
  p.noStroke();
 }

 p.draw = () => {
  p.background('orangered');
  p.ellipse(150, 100, 100, 100);
 }
Enter fullscreen mode Exit fullscreen mode

}

useEffect(() => {
const myP5 = new p5(Sketch, myRef.current)
},[])

return (
  <div ref={myRef}>
    Hello
  </div>
)
Enter fullscreen mode Exit fullscreen mode

}

export default App;

Collapse
 
ndutared profile image
Abby Nduta

Thanks for the response @anshul16. It will surely help someone in the community.