Creative Coding - Week 1 - Exercise 2
Sketch Exercise 2 - 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, 02 - Draw your name! (part 2)
* 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.
* Press 'r' to erase your drawing and start with a blank screen
*
*/
// variables for the angle (in radians) and increment
float angle;
float inc;
void setup() {
size(500, 500);
background(0);
rectMode(CENTER); // rectangles drawn from the centre
// initialise angle and inc to 0
angle = 0;
inc = 0;
}
void draw() {
/* draw a rectangle at your mouse point while you are pressing
the left mouse button */
// map the mouse x position to the range (0.01, 0.08)
inc = map(mouseX, 0, width, 0.01, 0.08);
// incremment the current angle
angle = angle + inc;
if (mousePressed) {
stroke(170);
fill(120, 60);
rect(mouseX, mouseY, 2, 2);
line(mouseX, mouseY, pmouseX, pmouseY); // pmouse is the mouse position at the previous frame
// oscillate the radius over time
float radius = 30 * abs( sin(frameCount) );
float first_tempX = mouseX + radius * cos( angle);
float first_tempY = mouseY + radius * sin( angle);
float second_tempX = mouseX + radius * cos(-angle);
float second_tempY = mouseY + radius * sin(-angle);
// draw some lines and circles using transparency
stroke(110, 100);
line(mouseX, mouseY, first_tempX, first_tempY);
line(mouseX, mouseY, second_tempX, second_tempY);
float temp_w = random(3);
ellipse(first_tempX, first_tempY, temp_w, temp_w);
ellipse(second_tempX, second_tempY, temp_w, temp_w);
}
// 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(0);
}
}
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, 02 - Draw your name! (part 2)
* by Indae Hwang and Jon McCormack
* Copyright (c) 2014 Monash University
* Modified by Gerard Holden to add some colour.
* This program allows you to draw using the mouse.
* Press 's' to save your drawing as an image.
* Press 'r' to erase your drawing and start with a blank screen
*
*/
// variables for the angle (in radians) and increment
float angle;
float inc;
int rvalue = 64;
int gvalue = 128;
int bvalue = 192;
int rdir = 1;
int gdir = -1;
int bdir = 1;
void setup() {
size(500, 500);
background(0);
rectMode(CENTER); // rectangles drawn from the centre
// initialise angle and inc to 0
angle = 0;
inc = 0;
}
void draw() {
/* draw a rectangle at your mouse point while you are pressing
the left mouse button */
// map the mouse x position to the range (0.01, 0.08)
inc = map(mouseX, 0, width, 0.01, 0.08);
// incremment the current angle
angle = angle + inc;
if (mousePressed) {
rvalue = rvalue + rdir;
if (rvalue > 224) {
rdir = -1*int(random(5));
}
if (rvalue < 32) {
rdir = 1*int(random(5));
}
gvalue = gvalue + gdir;
if (gvalue > 224) {
gdir = -1*int(random(5));
}
if (gvalue < 32) {
gdir = 1*int(random(5));
}
bvalue = bvalue + bdir;
if (bvalue > 224) {
bdir = -1*int(random(5));
}
if (bvalue < 32) {
bdir = 1*int(random(5));
}
// stroke(rvalue,gvalue,bvalue,128);
// fill(120, 60);
// rect(mouseX, mouseY, 2, 2);
// line(mouseX, mouseY, pmouseX, pmouseY); // pmouse is the mouse position at the previous frame
// oscillate the radius over time
float radius = 30 * abs( sin(frameCount) );
float first_tempX = mouseX + radius * cos( angle);
float first_tempY = mouseY + radius * sin( angle);
float second_tempX = mouseX + radius * cos(-angle);
float second_tempY = mouseY + radius * sin(-angle);
// draw some lines and circles using transparency
stroke(rvalue,gvalue,bvalue,128);
line(mouseX, mouseY, first_tempX, first_tempY);
line(mouseX, mouseY, second_tempX, second_tempY);
float temp_w = random(3);
// ellipse(first_tempX, first_tempY, temp_w, temp_w);
// ellipse(second_tempX, second_tempY, temp_w, temp_w);
}
// 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(0);
}
}
I submitted my name for this exercise.
