Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jimpick/75278 to your computer and use it in GitHub Desktop.
Save jimpick/75278 to your computer and use it in GitHub Desktop.
// Script to Generate Random Graphics for Moo Cards
// Example: http://www.flickr.com/photos/jpick/3335073434/
// Modified from Google example - same license as Top Draw (Google BSD-style)
var name = "Jim Pick";
//var fontName = "Data70SH-Regular";
var fontName = "Helvetica";
// Fill with desktop with Colored rect with same top left and bottom right colors
var db = new Rect(0, 0, 1040, 696);
var target = new Layer(db);
var bltr = new Color;
var tl = new Color(0.15);
var br = new Color(0.4);
// Coloring clockwise from bottom left
target.coloredRect(db, bltr, tl, bltr, br);
var rnd = new Randomizer(0.1, 1);
// Green & blue Particles
var p1 = new Particles;
p1.velocityXRandomizer = new Randomizer(100, 300);
p1.velocityYRandomizer = new Randomizer(50, 80);
p1.accelerationXRandomizer = new Randomizer(-5, 5);
p1.accelerationYRandomizer = new Randomizer(-5, 5);
p1.maxParticles = 20;
p1.location = new Point(0, db.height * rnd.floatValue);
p1.gravity = new Point(0, -19);
p1.alphaDelta = 0.01;
p1.alphaDelay = 30;
p1.maxAge = 90;
p1.trailWidth = 4;
var c1 = new Color(0, 1, 0, 0.0);
var c2 = new Color(0.3, 0, 1, 0.0);
p1.addColor(c1.vary(0, 0.3, 0.3, 0.0));
p1.addColor(c2.vary(0.1, 0, 0.5, 0.0));
// Red and orange
var p2 = new Particles;
p2.velocityXRandomizer = new Randomizer(-5, 25);
p2.velocityYRandomizer = new Randomizer(20, 100);
p2.accelerationXRandomizer = new Randomizer(-10, 10);
p2.accelerationYRandomizer = new Randomizer(5, 10);
p2.maxParticles = 90;
p2.location = new Point(db.width * rnd.floatValue * 0.75, 0);
p2.gravity = new Point(0, -9);
p2.alphaDelta = -0.003;
p2.trailWidth = 2;
p2.addColor(new Color(1, 0, 0, 0.4));
var orange = new Color("orange");
orange.a = 0.3;
p2.addColor(orange);
// Gravity well
var randomXLoc = new Randomizer(500, db.width);
var randomYLoc = new Randomizer(200, db.height);
var loc = new Point(randomXLoc.floatValue, randomYLoc.floatValue);
var gp = new GravityPoint();
gp.location = loc;
gp.gravity = -240;
p1.addGravityPoint(gp);
p2.addGravityPoint(gp);
// Simulator gets drawn in its own layer
var simulatorLayer = new Layer(db);
var simulator = new Simulator;
simulator.addSimulatorObject(p1);
simulator.addSimulatorObject(p2);
// The time step controls the "fine" detail or smoothness
simulator.timeStep = 0.2;
// The run parameter is the duration of the simulation
simulator.runInLayer(simulatorLayer, 150);
// simulator.runInLayer(desktop2, 150);
// Bloom
var bloom = new Filter("CIBloom");
bloom.setKeyValue("inputRadius", 80);
bloom.setKeyValue("inputIntensity", 3);
// Blur
var blur = new Filter("CIGaussianBlur");
blur.setKeyValue("inputRadius", 16);
blur.inputFilter = bloom;
simulatorLayer.applyFilter(blur);
// Draw text randomly
var text = new Text(name);
var xRnd = new Randomizer(0, db.width);
var yRnd = new Randomizer(0, db.height);
var sizeRnd = new Randomizer(32, 512);
var count = 15 * compositor.screenCount;
var rotateRnd = new Randomizer(0, 2 * Math.PI);
var variation = 0.5;
var fontColor = new Color;
fontColor.a = 0.2;
var simImg = new Image(simulatorLayer);
target.drawImage(simImg, db);
var desktop2 = new Layer(db);
// Draw
for (var i = 0; i < count; ++i) {
text.fontName = fontName;
text.foregroundColor = fontColor.vary(variation, variation, variation, 0);
text.fontSize = sizeRnd.intValue / 3;
// Rotate randomly
var pt = new Point(xRnd.intValue, yRnd.intValue);
desktop2.save();
desktop2.translate(pt.x, pt.y);
desktop2.rotate(rotateRnd.floatValue);
desktop2.translate(-pt.x, -pt.y);
desktop2.drawText(text, pt);
desktop2.restore();
}
// Apply a filter to make it more interesting
var f;
var filterRnd = new Randomizer;
f = new Filter("CIGaussianBlur");
f.setKeyValue("inputRadius", (2 + filterRnd.floatValue * 3));
desktop2.applyFilter(f);
var d2img = new Image(desktop2);
target.drawImage(d2img, db);
var img = new Image(target);
desktop.drawImage(img, db);
// Get Unix time
var foo = new Date;
var unixtime_ms = foo.getTime();
var unixtime = parseInt(unixtime_ms / 1000);
// Write out file into ~/Library/Application\ Support/Google/TopDraw
img.exportImage("moocard-" + unixtime + ".png", "png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment