From the course: Coding for Visual Learners: Learning JavaScript from Scratch

Unlock this course with a free trial

Join today to access over 24,400 courses taught by industry experts.

MouseIsPressed

MouseIsPressed

- [Instructor] In the previous chapter, we learned about the p5.js frameCount variable that provides us with a number that represents the number of times the draw function is called. There are a bunch of other highly useful variables that we could be using in p5.js. We will learn about a few more in this chapter. MouseIsPressed is the first p5.js variable that we will see that allows us to add some interactivity to our programs. MouseIsPressed is a p5.js variable that gets the value true when the mouse is clicked on the canvas area and is false for every other time. Let's alter one of our prior examples to quickly see how we can use this variable. Here's a drawing from earlier where we were drawing a circle and a rectangle to the script. If you wanted to make the drawing of rectangle conditional to the mouse press, we can make use of the mouseIsPressed variable. To be able to use that, I will create an if condition…

Contents