Hi! I am currently learning P5.js, and I’m planning on using it with my personal portfolio.
I managed to do what I want in p5.js, however, when I try to import it to my react project i’m not able to do it.
This is what i’m trying to do. I’m trying to use this npm package
I’m having trouble loading the images. How would I go about doing that?
import React from "react";
import Sketch from "react-p5";
let x = 50;
let y = 50;
let imgURL = './Ellipse1.png'
let img;
export default (props) => {
const setup = (p5, canvasParentRef) => {
img = p5.loadImage(imgURL)
// use parent to render the canvas in this ref
// (without that p5 will render the canvas outside of your component)
p5.createCanvas(500, 500).parent(canvasParentRef);
};
const draw = (p5) => {
p5.background(0);
p5.ellipse(x, y, 70, 70);
p5.image(img,10,10)
// NOTE: Do not use setState in the draw function or in functions that are executed
// in the draw function...
// please use normal variables or class properties for these purposes
x++;
};
return <Sketch setup={setup} draw={draw} />;
};