Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.