a→ab

computation, game design, and experimentation


front page | about | archives | code dump | c.s. for mere mortals | tags | rss feed

A quick prototyping grid in Processing

January 8, 2013
tags:  processingprototypinggridcode-dump

First post of the new year!

This one is a quick one though, just wanted to share a quick little idea I whipped up in Processing that could help others make Processing sketches. It's kind of a meta-sketch.

Basically, it's a customizable gridline maker, so that you can easily line things up in your sketches without doing too much math, and just focus on making art. When you don't want the grids anymore, just change the GRID_DRAW variable to false, and you're good to go!

Code is in this gist:

// gridlines
// grid specific vars
// grid spacing in pixels
int GRID_MINOR_SPACING = 10;
int GRID_MAJOR_SPACING = 100;
// gridline colors
color GRID_MINOR_COLOR = color(147, 161, 247, 127);
color GRID_MAJOR_COLOR = color(0, 19, 137, 127);
// gridline weights in pixels
int GRID_MINOR_WEIGHT = 1;
int GRID_MAJOR_WEIGHT = 2;
// when true, grid is drawn, else grid is not
boolean GRID_DRAW = true;
// sketch vars go here
int background_color = 255; //white
// grid code
public void drawGrid() {
if (GRID_DRAW) {
int num_minor_x = width/GRID_MINOR_SPACING;
int num_minor_y = height/GRID_MINOR_SPACING;
int num_major_x = width/GRID_MAJOR_SPACING;
int num_major_y = height/GRID_MAJOR_SPACING;
pushStyle();
strokeCap(PROJECT);
strokeWeight(GRID_MINOR_WEIGHT);
stroke(GRID_MINOR_COLOR);
for (int i = 0; i < num_minor_y; i++) {
int y = i * GRID_MINOR_SPACING;
line(0, y, width, y);
}
for (int i = 0; i < num_minor_x; i++) {
int x = i * GRID_MINOR_SPACING;
line(x, 0, x, height);
}
strokeWeight(GRID_MAJOR_WEIGHT);
stroke(GRID_MAJOR_COLOR);
for (int i = 0; i < num_major_y; i++) {
int y = i * GRID_MAJOR_SPACING;
line(0, y, width, y);
}
for (int i = 0; i < num_major_x; i++) {
int x = i * GRID_MAJOR_SPACING;
line(x, 0, x, height);
}
popStyle();
}
}
public void setup() {
size(400,600);
background(background_color);
}
public void draw() {
background(background_color);
// do your stuff here
fill(0);
rect(40,35, 234,34);
// draw grid on top -- won't draw when GRID_DRAW == false
drawGrid();
}
view raw gridlines.pde hosted with ❤ by GitHub

It's in the public domain, so go mad.

Thanks for reading.