Skip to content

Instantly share code, notes, and snippets.

@nataliefreed
Created April 11, 2016 04:10
Show Gist options
  • Save nataliefreed/4f483738ffe7b5b4c5861eeb8f1b0d6d to your computer and use it in GitHub Desktop.
Save nataliefreed/4f483738ffe7b5b4c5861eeb8f1b0d6d to your computer and use it in GitHub Desktop.
//Tiny composition book cover generator
//Natalie Freed 2014
//Learned about using Perlin noise to make 2D textures from
//Daniel Shiffman's Nature of Code: http://natureofcode.com/book/introduction/
int labelCenterX, labelCenterY, labelWidth, labelHeight;
int labelMargin = 16;
int outlineMargin = 24;
int labelCorner = 10;
int lineMargin = 20;
int edgeBandWidth, textOffset, lineSpacing;
int lineXStart, lineXEnd, lineY;
void setup()
{
size(800, 525);
noLoop();
PFont font = loadFont("Rockwell-20.vlw");
textFont(font, 20);
textAlign(CENTER);
rectMode(CENTER);
edgeBandWidth = width/12;
labelCenterX = 3*width/4+edgeBandWidth/4;
labelCenterY = height/3;
labelWidth = width/4;
labelHeight = height/4;
textOffset = labelHeight/6;
lineSpacing = labelHeight/8;
lineXStart = labelCenterX-labelWidth/2+labelMargin+lineMargin;
lineXEnd = labelCenterX+labelWidth/2-labelMargin-lineMargin;
lineY = int(labelCenterY-textOffset+1.5*lineSpacing);
}
void draw()
{
background(200);
makeSome2DNoise();
//edge band
fill(0);
noStroke();
rect(width/2, height/2, edgeBandWidth, height);
//label
fill(255);
rect(labelCenterX, labelCenterY, labelWidth, labelHeight, labelCorner);
stroke(0);
noFill();
rect(labelCenterX, labelCenterY, labelWidth-labelMargin, labelHeight-labelMargin, labelCorner);
rect(labelCenterX, labelCenterY, labelWidth-outlineMargin, labelHeight-outlineMargin, labelCorner);
//label text
fill(0);
text("PROCESSING", labelCenterX, labelCenterY-textOffset);
//blank lines on label
line(lineXStart, lineY, lineXEnd, lineY);
line(lineXStart, lineY+lineSpacing, lineXEnd, lineY+lineSpacing);
line(lineXStart, lineY+2*lineSpacing, lineXEnd, lineY+2*lineSpacing);
saveFrame("mini composition book " + day()+hour()+minute()+second()+".png");
}
void makeSome2DNoise() //use Perlin noise to generate cover pattern
{
float noiseStep = random(0.05, 0.9);
int notebookColor = color(int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
float noiseLoc = 0;
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
if(noise(noiseLoc) > 0.4) { stroke(notebookColor); }
else { stroke(255); }
point(j, i);
noiseLoc+=noiseStep;
}
}
}
void mousePressed()
{
redraw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment