Manipulate a Drawn Shape Code

by buechley

Code that you can use to manipulate a vector image. The image must be in the .svg format (here it’s called shape.svg) and should be in the same folder as the code.


import processing.pdf.*;

PShape s;

void setup() {
s = loadShape("shape.svg");
size(500, 500); //set the size of the window (& the pdf document)
smooth();
noLoop();
}

void draw() {
//set the background color to be white
background(255);

//this code randomly scales, rotates, and translates the shape
for (int i=0; i<10;i++)
{
pushMatrix(); //remember the current position and rotation
int randomSize=(int)random(100);
translate(random(width), random(height)); //move to a random position
rotate(random(radians(360))); //rotate a random angle
shape(s, 0, 0, randomSize, randomSize); //draw the shape at a random size
popMatrix(); //return to the original position and rotation
}

//this code creates a five pointed circular pattern from the shape
translate(width/2, height/2);
for (int i=0;i<5;i++)
{
pushMatrix(); //remember the current position and rotation
translate (10,10); //move 10 pixels away from the current position in the x and y directions
shape(s, 0, 0, 20,20); //draw the shape, size = 20 pixels
popMatrix(); //return to the original position and rotation

pushMatrix(); //remember the current position and rotation
translate (20,50); //move 20 pixels in x and 50 pixels in y
shape(s, 0, 0, 20,20); //draw the shape, size = 20 pixels
popMatrix(); //return to the original position and rotation

rotate(radians(72));
}
}

void keyPressed() {
//if the 'q' key is pressed, finish drawing the PDF & quit
if (key == 'q') {
String fileName= "drawing"+millis()+".pdf"; //create a unique name for your file
beginRecord(PDF, fileName);
draw();
endRecord();
exit();
}
//if any other key is pressed, draw a new pattern
else {
redraw();
}
}