Now that you know the basics of p5 it is time to write and upload your own “Hello World!” sketch to the web! Please follow along the next few pages to complete your homework for the week.
In your /120-work
directory, duplicate the empty-example/
directory and label it /hw-3
.
Within that directory create a new html document labeled index.html
.
Your 120-work/
directory should now look like the following;
Open this new hw-3/
directory in Atom. Then navigate to and open your blank sketch.js
file within the hw-3/
directory.
setup()
FunctionNow, in sketch.js
, write and declare the setup()
function. Remember, in this function you should;
createCanvas()
function.background()
function.Your setup()
function might look something like the following;
1
2
3
4
5
6
7
8
9
// Declare the setup function
function setup() {
// create a canvas 600px wide and 400px high
createCanvas( 600, 400 );
// color the background 'pink'
background( 'pink' );
}
You should now open your sketch.js
file and make sure you successfully created a canvas and set the background of this canvas.
Your browser should show something like the following;
draw()
Function and Add “Hello World!”Finally, underneath the setup()
function, you should write and declare a draw()
function.
Inside this draw()
function, I want you to write a statement to print the text string “Hello World!
” on the sketch canvas. You can do this using the text()
function. (Remember to include comments!)
I would suggest you read about the
text()
function to try and unsertand more about it and how it works. (It’s OK that you don’t unsertand it all right now. Just try to get use to reading reference pages.)
Your code should look something like the following;
1
2
3
4
5
6
7
// Declare the draw function
function draw() {
// tell p5 to print the text string "Hello World!"
// This is going to be positioned at ( x: 20, y: 30 ).
text( "Hello World!", 20, 30 );
}
After adding the draw()
function declaration and saving your sketch.js
file, you should;
Mine, looks like the following;
Here is a live example, with the complete code under the JavaScript tab.
[ Code Download ] | [ View on GitHub ] | [ Live Example ] |
Now that you have finished up Part 1 of your weekly homework assignment, you should;
After pushing your changes, visit your repository on GitHub to insure that your changes and code was successfully sync’d.