Skip to content

Instantly share code, notes, and snippets.

@Craigson
Created September 17, 2014 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Craigson/aa3bfd061ce6d1043803 to your computer and use it in GitHub Desktop.
Save Craigson/aa3bfd061ce6d1043803 to your computer and use it in GitHub Desktop.
Simple dual-mode drawing program
//Craig Pickard
//ICM - Week 2: basic animation
//About this program:
/*
This program contains a button that switches between two modes,
there is a drawing mode, where a mousePressed event draws a
collection of circles (defined by random variables) around the mouse pointer.
clicking the button, and changing modes, spawns a collection of
randomly appearing variables. A mouseclick event enables the eraser,
which allows the user to erase the circles. The erasers brush size
can be adjusted using the '[' and ']' keys.
*/
//these global variables are shared by all of the different functions, not just draw
boolean click = false;
boolean kp = false;
boolean button = false;
int buttonR = 255;
int buttonG = 0;
int buttonB = 0;
float radius = 50;
float distance;
float brushSize = 15;
float sw = 50;
PFont f; //declaring font datatype for inputting text
void setup() {
size(1200, 700);
background(255);
frameRate(1000);
f = loadFont("Avenir-Book-48.vlw");
}
void draw() {
//specify font and size
fill(255);
textFont(f, 18);
//these are the local variables used inside the draw program
float xSpeed = random(-2, 2);
float ySpeed = random(-2, 2);
float x = random(0, width);
float y = random(0, height);
float w = random(5, 100);
float r = random(255);
float g = random(255);
float b = random(255);
float f = random(0, 60);
//the conditional statement changes the boolean variable 'button'
//from true to false, or vice versa, if the button is clicked, by
//calculating the distance between the ellipse's centre, the mouse's
//XY location and evaluating it against the radius's ellipse to
//check whether or not the mouse is located within its perimieter
if (mousePressed && distance < radius) {
button = !button;
}
//this is the code that runs the the eraser program
if (button) {
strokeWeight(1);
noStroke();
rectMode(CENTER);
fill(r, g, b, f);
buttonR = 224;
buttonG = 72;
buttonB = 82;
x += xSpeed;
y += ySpeed;
ellipse(x, y, w, w);
ellipse(x, y, w, w);
x += 10;
if (x>width) {
x=10;
}
noStroke();
fill(50, 50);
rect(1000, 600, 250, 120);
fill(255);
textAlign(CENTER);
text("Click to toggle eraser.", 1000, 580);
text("Change brush size using", 1000, 610);
text("'[' and ']' keys", 1000, 640);
if (click) {
fill(255);
strokeWeight(brushSize);
stroke(255);
line(pmouseX, pmouseY, mouseX, mouseY);
delay(100); //this ensures a smooth transition between program modes
}
} else { //this is the code that runs the circle-drawing program
noStroke();
buttonR = 163;
buttonG = 55;
buttonB = 63;
stroke(0);
if (mousePressed && distance > 200) {
noStroke();
fill(r, g, b, f);
ellipse(mouseX + random(-30, 30), mouseY + random(-30, 30), w/2, w/2);
}
noStroke();
fill(50, 50);
rect(1000, 600, 250, 120);
fill(255);
textAlign(CENTER);
text("Click and hold the mouse", 1000, 580);
text("to begin drawing. click", 1000, 610);
text("the button to change modes!", 1000, 640);
}
//draw the button for switching between programs
strokeWeight(2);
stroke(200);
fill(232);
ellipse(1100,100,2*radius+20,2*radius+20);
fill(buttonR, buttonG, buttonB);
ellipse(1100, 100, 2*radius, 2*radius);
println(distance + " " + button);
}
//------------------------------------------------------------
//when the mousePressed function is called, if the mouse pointer is
//over the button, it switches the boolean variable 'button' from true/false
//or else, if it is not within the confines of the button, it simply
//swtiches the boolean variable 'click' from true/false instead
void mousePressed() {
distance = dist(1100, 100, mouseX, mouseY);
if (distance < radius) {
background(255);
button = !button;
delay(200); // this ensures a smooth transition between programs
} else {
click = !click;
}
}
// ------------------------------------------------------------
//this function determines the size of the eraser, it can be enlarged
//by pressing the ']' key, or reduced by pressing the '[' key
void keyPressed() {
brushSize = constrain(sw, 5, 200); //if the brushsize is allowed to go below 0, the program would crash
if (key == '[') {
sw -= 5;
} else if (key == ']') {
sw += 5;
}
}
//--------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment