Creative Coding - Week 1 - Exercise 1
Sketch Exercise 1 - as supplied.
Sample code was supplied that allows you to draw on the following canvas. You can draw by holding the left mouse button down and moving your mouse. To erase the drawing, press 'r'.
> View source code for preceding sketch.
/*
* Creative Coding
* Week 1, 01 - Draw your name!
* by Indae Hwang and Jon McCormack
* Copyright (c) 2014 Monash University
* This program allows you to draw using the mouse.
* Press 's' to save your drawing as an image to the file "yourName.jpg"
* Press 'r' to erase your drawing and start with a blank screen
*
*/
// setup function -- called once when the program begins
void setup() {
// set the display window to size 500 x 500 pixels
size(500, 500);
// set the background colour to white
background(255);
// set the rectangle mode to draw from the centre with a specified radius
rectMode(RADIUS);
}
// draw function -- called continuously while the program is running
void draw() {
/* draw a rectangle at your mouse point while you are pressing
the left mouse button */
if (mousePressed) {
// draw a rectangle with a small random variation in size
stroke(170); // set the stroke colour to a light grey
fill(0, 150); // set the fill colour to black with transparency
rect(mouseX, mouseY, random(6), random(6));
}
// save your drawing when you press keyboard 's'
if (keyPressed == true && key=='s') {
saveFrame("yourName.jpg");
}
// erase your drawing when you press keyboard 'r'
if (keyPressed == true && key == 'r') {
background(255);
}
}
Sketch Exercise 1 - as modified.
I modified the supplied code to add some colour. As before, you can draw by holding the left mouse button down and moving your mouse. To erase the drawing, press 'r'.
> View source code for preceding sketch.
/*
* Creative Coding
* Week 1, 01 - Draw your name!
* by Indae Hwang and Jon McCormack
* Copyright (c) 2014 Monash University
* This program allows you to draw using the mouse.
* Press 's' to save your drawing as an image to the file "yourName.jpg"
* Press 'r' to erase your drawing and start with a blank screen
*
* Updated to add colour by Gerard Holden
*/
// setup function -- called once when the program begins
void setup() {
// set the display window to size 500 x 500 pixels
size(500, 500);
// set the background colour to white
background(255);
// set the rectangle mode to draw from the centre with a specified radius
rectMode(RADIUS);
}
// draw function -- called continuously while the program is running
void draw() {
/* draw a rectangle at your mouse point while you are pressing
the left mouse button */
int red, green, blue;
if (mousePressed) {
// draw a rectangle with a small random variation in size
// stroke(170); // set the stroke colour to a light grey
red = int(random(240));
green = int(random(240));
blue = int(random(240));
stroke(red+16,green+16,blue+16);
fill(red,green,blue);
rect(mouseX, mouseY, random(6), random(6));
}
// save your drawing when you press keyboard 's'
if (keyPressed == true && key=='s') {
saveFrame("yourName.jpg");
}
// erase your drawing when you press keyboard 'r'
if (keyPressed == true && key == 'r') {
background(255);
}
}
I submitted my name for this exercise.
