Skip to content

Instantly share code, notes, and snippets.

@julioterra
Last active December 22, 2015 08:29
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 julioterra/6445046 to your computer and use it in GitHub Desktop.
Save julioterra/6445046 to your computer and use it in GitHub Desktop.
This is the rainbow sketch with the Spacebrew library fully integrated.
/**
* Rainbow.
* Click the mouse anywhere on the sketch to activate the rainbow animation.
*
*/
//////////////////////////////
import spacebrew.*;
Spacebrew spacebrewConnection;
String server="sandbox.spacebrew.cc";
String name = "julio's rainbow";
String description = "simple rainbow animation chain reaction sketch";
//////////////////////////////
int barWidth = 20;
int barCurrent = 0;
int barTotal = 0;
boolean drawingRainbow = false;
void setup()
{
size(640, 360); // create window
frameRate(20); // set framerate
background(0); // draw a black background
colorMode(HSB, width, height, height); // set color mode to Hue/Saturation/Brightness
noStroke(); // turn off stroke
barTotal = width / barWidth; // calculate total number of bars that fit on window
//////////////////////////////
spacebrewConnection = new Spacebrew( this );
// add each thing you publish to
spacebrewConnection.addSubscribe( "chain", "boolean" );
spacebrewConnection.addPublish( "chain", "boolean", true );
// connect!
spacebrewConnection.connect(server, name, description );
//////////////////////////////
}
void draw() {
// draw rainbow when flag set to true
if (drawingRainbow == true) {
// determine the X location of the current bar
int drawBarXpos = barCurrent * barWidth;
// step 1: draw rainbow bars
if (barCurrent < barTotal ) {
fill(drawBarXpos, height, height); // set fill color
rect(drawBarXpos, 0, barWidth, height); // draw bar at appropriate position
barCurrent += 1; // increment the number of the current bar
}
// step 2: finish animation
else {
drawingRainbow = false; // set drawing flag to false
background(0); // set background to black
//////////////////////////////
spacebrewConnection.send("pass", true);
//////////////////////////////
}
}
}
void mousePressed() {
barCurrent = 0; // start drawing at beginning
drawingRainbow = true; // set draw flag to true
}
//////////////////////////////
void onBooleanMessage( String name, boolean value ) {
barCurrent = 0; // start drawing at beginning
drawingRainbow = true; // set draw flag to true
}
//////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment