Skip to content

Instantly share code, notes, and snippets.

@nataliefreed
Created September 26, 2016 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nataliefreed/a1640832e6792ded390c8ea3dcd19125 to your computer and use it in GitHub Desktop.
Save nataliefreed/a1640832e6792ded390c8ea3dcd19125 to your computer and use it in GitHub Desktop.
Wrapper function to help scale shapes in P5
function setup() {
createCanvas(600, 600);
noFill();
strokeWeight(3);
//example: draw something, unscaled
yourFunctionThatDrawsAThing(100, 200);
//example: make a half scale version
var scaledByHalf = autoScale(yourFunctionThatDrawsAThing, 0.5);
//run the half scale version, placed at specific coordinates on the screen
scaledByHalf(100, 200);
//example: make a three times scale version
var scaledByThree = autoScale(yourFunctionThatDrawsAThing, 3);
//run the three times scale version, placed at specific coordinates on the screen
scaledByThree(10, 300);
}
//replace this function with your own function that draws a shape
//first two arguments (inputs) must be its x and y coordinates, others can be anything
function yourFunctionThatDrawsAThing(x, y) {
push();
translate(x, y);
bezier(0, 0, 50, 70, 200, -85, 300, 0);
pop();
}
//leave this function as-is, it will help you scale things
function autoScale(shape, scaleFactor) {
return function() {
push();
translate((1-scaleFactor)*arguments[0], (1-scaleFactor)*arguments[1]);
scale(scaleFactor);
shape.apply(null, arguments);
pop();
}
}
function draw() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment