GSDP:GAM100/CProcessing/scale()
From Inside
< GSDP:GAM100 | CProcessing
scale()
Description
The function scale() uniformly changes the scale of the canvas using by a given scalar. Scalar must be non-zero and positive.
Parameters
scale(float scalar)
- scalar - (float) the amount to scale the canvas by
Example
// Create variable to keep track of scale
// IMPORTANT: initialize to 1 because scale should not be 0
static float counter = 1;
void update()
{
// Expected behavior: Camera will zoom in and out, depending on the mouse wheel
// Clear screen every frame
background(color(255, 0, 0, 255));
int zoom = mouseWheel(); // 1 zooms in, -1 zooms out
float zoom_amount = 0.1f; // Small steps to smooth zooming
if (zoom > 0)
{
++counter;
}
else if (zoom < 0)
{
--counter;
}
// Change the scale of the canvas every frame
scale(counter * zoom_amount);
// Fixed points
// Note: some parameters are multiplied by 1.0f to convert int to float
rect(0, 0, 100, 50); // Draw rect clouds at the top left corner
rect(canvasWidth * 1.0f - 100, 0, 100, 50); // Draw rect clouds at the top right corner
rect(0, canvasWidth * 1.0f - 200, canvasWidth * 1.0f, 200); // Draw the ground
ellipse(canvasWidth / 2.0f, canvasWidth / 2.0f, 20, 20); // Draw the sun in the middle of the screen
}