So order is super important in p5.js. And like I had mentioned earlier, you'll start to see these different patterns that emerge as we put these tiles together into a grid. If you rotate them, you'll start to see different shapes. And you can play around with, like, the color and even, like, what an individual tile looks like.
So back to what we're building today, this trusset tile generator. So like I said earlier, we're going to be using React to manage the state of user inputs and p5.js to draw on the canvas. So I find that React is a lot better at managing complex state than p5.js. P5.js' primary purpose is for drawing on a canvas. And React also will allow us if we want to create user input elements, we can position them using, like, a tree. We can position them using, like, CSS for the positioning as opposed to in the context of p5.js. Everything is within the canvas, and we have to determine, like, what the X and the Y coordinates are. So it's a lot more challenging to deal with, like, reflowing the layout if you're using p5.js. So I definitely recommend if you have more than one or two fields to consider using something like React or another library to manage that.
So when using p5.js with React, there is a handy p5 wrapper package that I would recommend using to help make your life easier. So with the p5.js React p5 wrapper, we can import that wrapper and then we can render it within our React application. And you may notice here that I'm checking that the window is not undefined because if it is, I don't want to render or I don't want to try to call the p5 library because it's a client-side only library. There is a node version of p5 if you wanted to create a, like, I created a blue sky bot at one point that was using p5 to post images. So there is, like, a node package, but typically it's a client-side only library.
So in addition to the setup function, there's also the draw function, and p5.js is one of the core functions that you would normally see when you have a p5.js sketch. And that's something that's continuously drawing on the canvas and by default it runs 60 times per second. And when using p5.js with React or with any other library, you want to use p5.js in instance mode. So that means everything is going to be namespaced to a p5 object, whereas if you want to quickly use p5.js, you would just use it in global mode, which means you don't have to namespace it. So you would just call set up and draw. But in instance mode, you have to put p5.setup, p5.draw, and that p5 can really be named anything. It just needs to be referencing a p5 object. So with instance mode, we can have multiple sketches on a page. It's compatible with React or other libraries. The issue when you don't namespace it is that there could be a collision. If there's more than one JavaScript library that has the same naming conventions, things just start to fall apart.
Comments