UAL CCI: Introduction to Creative Computing Intensive 2022: ⚙️ & ✍🏿: Lecture 3: Functions and Drawing.
Back to slide index.
By the end of this lecture, we'll have learnt about:
The two main things we are going to think about today are various different functions and thinking in different numbers of dimensions - both feed into how to draw on computers.
Functions are things in JavaScript (and most programming languages) that take an input, do some work and then give an output - or things that you can ask a question of and you'll get an answer. This is the way that you create a function in JavaScript:
function addTwoNumbers(firstValue, secondValue) {
  let answer = firstValue + secondValue;
  return answer;
}
let myAnswer = addTwoNumbers(3, 5);
//myAnswer is now 8
We have a function called addTwoNumbers (camelCase!), which takes two numbers as parameters, adds them together inside the function and then returns the answer. Some functions don't have any inputs and some don't have any outputs, but they all do something in the middle, otherwise there would be no point in calling them. The values you put into parameters are called arguments.
Dimensions could also be thought of as independent variables - like when you are drawing you might think about how far left and right you need to be on a page and (independently) how far up and down you need to be to draw the picture you want to.
I'm going to start by talking about drawing on paper, before looking at how we can draw on a computer using p5.js.
Drawing in black and white (or greyscale) using p5.js needs two things: data types (either variables or constants) and commands (or functions or subroutines).
Let's start with drawing on paper. How many numbers do you need to describe where you are on a piece of paper?
To think about drawing on paper, let's try an exercise. One person is going to be the model, one person is going to be the blind drawing robot and one person is going to be the sighted drawing instruction robot - because this lecture is remote, I'm going to have to play all the roles for you!
What was hard (or funny) about that?
I had to be really precise with myself, right? I had to break up something simple like "Draw the number 8" into lots of smaller commands AND I had to have a way of saying where on the page the robot should draw. That's exactly what we are going to have do when we write drawing code in p5.js - a way of saying where we are on the screen and a way of drawing simple shapes that we can build up into more complicated ones.
Let's go through the p5.js shape primitives example together. You can use and edit any of the p5.js examples directly on their web pages, but I'm going to copy and paste them into the p5.js editor so you see how to use it.
Let's go through the p5.js data variables example together.
Let's go through this tutorial on Coordinate System and Shapes together. Remember, a Co-ordinate system is a system for describing position.
As well as commands that specify exact positions, you can use transformations to move your digital drawing tool around like I commanded myself earlier. See this Points and Lines example for how to use translate() to do that. You can also rotate(), scale().
N.B. you can use push() and pop() to remember different drawing states. Chicken and egg in terms of colour, but I wanted you to know about push and pop, before we get on to some INTERACTION!
Now let's go through the p5.js Get Started tutorial together, using the p5.js editor. Remember the Chicken and Egg problem for learning coding from the first lecture? We are going to encouter that here when you see an "if" statement. Don't worry about that for now, the most important thing is when we start using variables instead of constant (or fixed) values to draw with. This is the most important thing you are going to learn today and maybe for the whole course - that computers can have values that change over time, and that they'll do the work of changing them for you - allowing you to make INTERACTIVE animations or music or art or anything!
Thanks! Time for a short break!
Back to slide index.