Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
High resolution rendering with Processing, Part 2
PGraphics render;
int printWidth = 10;
int printHeight = 6;
int printDpi = 300;
int previewDpi = 72;
boolean renderHighRes = false;
boolean firstFrame = true;
int renderWidth;
int renderHeight;
float scaleFactor = 1;
int seed = 0;
int maxRadius = 50;
void setup() {
size(1024, 1024);
hint(ENABLE_STROKE_PURE);
doReset();
}
void doReset() {
int dpi = renderHighRes ? printDpi : previewDpi;
scaleFactor = dpi / (float)previewDpi;
renderWidth = printWidth * dpi;
renderHeight = printHeight * dpi;
render = createGraphics(renderWidth, renderHeight);
firstFrame = true;
maxRadius = 50;
randomSeed(seed);
}
void keyPressed() {
switch (key) {
case 's':
String dateString = String.format("%d-%02d-%02d %02d.%02d.%02d", year(), month(), day(), hour(), minute(), second());
saveFrame(dateString + ".scr.png");
render.save(dateString + ".png");
break;
case 'r':
seed = (int)System.currentTimeMillis();
renderHighRes = false;
doReset();
break;
case 'h':
renderHighRes = true;
doReset();
break;
}
}
void draw() {
render.beginDraw();
if (firstFrame) {
firstFrame = false;
render.background(255);
}
doSomethingMagical(render);
render.endDraw();
int outWidth, outHeight;
float ratio = renderWidth / (float)renderHeight;
if (ratio > 1) {
outWidth = 1024;
outHeight = (int)(outWidth / ratio);
} else {
outHeight = 1024;
outWidth = (int)(outHeight * ratio);
}
background(192);
image(render, (1024 - outWidth) / 2, (1024 - outHeight) / 2, outWidth, outHeight);
}
// No actual magic involved
void doSomethingMagical(PGraphics r) {
if (maxRadius == 0)
return;
r.noStroke();
r.blendMode(MULTIPLY);
r.fill(random(64, 192));
for (int i = 0; i < 100; ++i) {
float radius = random(maxRadius / 10, maxRadius) * scaleFactor;
r.ellipse(random(0, renderWidth), random(0, renderHeight), radius, radius);
}
r.blendMode(ADD);
r.fill(random(16, 128));
for (int i = 0; i < 100; ++i) {
float radius = random(maxRadius / 10, maxRadius) * scaleFactor;
r.ellipse(random(0, renderWidth), random(0, renderHeight), radius, radius);
}
--maxRadius;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment