Built-in Variables:
So he explains a bit what a variable is, basically just a value in memory that can store data and can generally change to other non-fixed values inside of your program’s runtime. He showcases two different variables that are built into the p5.js library, this being mouseX and mouseY which are updated on every single frame of the program, returning the position of the user’s mouse.
They are good for having things follow the mouse or collisions or other such bs like that, and you use them just like they are typed out above.
He also mentioned two other variables width and height which hold the values of the canvas that you defined inside of the setup() function, so the size of the drawing window pretty much.
He then talks about getting the different moused clicked things, so basically you need to override the function inside of the p5.js library, which is done by calling in mousePressed() {} just like that in your sketch.js file. Doing this you can write whatever logic you wish to have whenever the user pressed the mouse button down. This is also done with keyPressed() {}, which does the same thing, but can specify the given key I assume.
So he basically declares a global variable using the var keyword and then names it and he just gives it no value and initializes it inside of the setup() function. He then doing this proceeds to use the variable inside of his draw functions instead of just hard-coding values. This is real nice because we can just abstract out these hard-coded values and change lots of values very easily, so instead of manually changing each value, you just change one.
You can also then do math on them and what not, and move all of the different values in reference to one another, really simple stuff, basically just how you use a variable in any one of these programs.
Naming your variables is really one of the hardest possible things you can do in programming if we are really being honest. He gives you 12 tips, the first is to avoid keywords so your fucking code can compile, a great tip.
You should avoid weird symbols so your code compiles, great fucking tip. He also probably is going to say to use things like camelCase, underscore values, make things short and descriptive, its naming variables not rocket science outright.
He also says that if you have too many different variables to describe a single then, then use an object to group them. Equally, you can use something like the ctrl + f or ctrl + h to find and replace all variables at once if you want to rename them.
Objects
So he says that we can basically combine a bunch of the different variables that we have into a given object to represent the different aspect. The variables and the data is still there, but now it is more organized under a given class instance, and so you can manipulate these values more in tandem to each instance of the object.
He does this really weirdly though, instead of like creating a class or something like that, what he does instead is create this sort of weird empty object? So its just an object without a class definition that he tosses a bunch of variables into:
This is then good for creating a kind of namespace that you can generally use in order to specify values for a given instance, it really is only for organization so you can have something different and give context to the code, but I really think that just using a class is better in this case.
Working with operators
So basically, you can do math, and that is really what the people have covered man. It’s pretty chill, you can use things like +, -, *, /, % all of that good kind of stuff. In reality its just the most logical item of things that do exist, you can really just do whatever math you want to change values.
I really don’t know what more they want me to take notes of here, this bald guys voice is a tad annoying I cannot like. He just mentioned things like = which is the assignment operator for changing a new value, and that is pretty worthy of saying that.
You can also do things like +=, -=, --, ++, *= and all of that good stuff pretty much, this is just those syntactic sugar things that end up being useful for letting you manipulate different values.
Mathematical functions
So he mentions a bunch of different functions, the first one he starts with the random() function, and what it does is return a random number as a decimal number, so you can use it to multiple a value to get a maximum range between that value and zero. You can do cool stuff with this of course as you can max things appear differently on each refresh of the page. Using this in the draw function can give you a cool strobe like thing
You can also provide two parameters for the random function random(int, int) and these two integer values then become the range of your given value, it is returned as a double of course.
He also then goes over min() and max() which both actually take in two values, and then either pick the smaller of the two, or the larger of the two, and that is really all that it does. This is good because it lets you not write your own if statement each time you want to check something, and is a clear way of saying “hey pick the bigger number or the smaller number between these two”.
It can also be good for things like picking a maximum value that a value can be and what not, so you can change a value until it becomes larger or smaller than whatever you want it to be, nice for things like animation or for things like stopping movement and what not.
The map function, which he does not even say anything about, is a value that is built into the p5.js library. What it does is re-scales a number from one range into another.
So what you do is basically give it FIVE arguments: value, start1, stop1, start2, stop2, withinBounds, with the last one being optional:
valuethis is the number that is going to become manipulated to change from the first range to the next range, so something likemouseXfor examplestart1, stop1this is the range that the value you want to change currently lives at, so for mouse X this would be0, widthstart2, stop2this is then the range that you are going to want to convert the value to, so the new maximum for this value to exist in, within of the same proportion it exists at in the first range.withinBoundsis an optional boolean value that determines if you want to clamp the result so it cannot go outside ofstart2-stop2
Under the hood it is just linear interpolation, which basically means you get your percentage of the difference between the two ratios, and then normalize the vector based on those differences:
start2 + (value - start1) * (stop2 - start2) / (stop1 - start1)