Reference scale()

scale()

Scales the coordinate system.

By default, shapes are drawn at their original scale. A rectangle that's 50 pixels wide appears to take up half the width of a 100 pixel-wide canvas. The scale() function can shrink or stretch the coordinate system so that shapes appear at different sizes. There are two ways to call scale() with parameters that set the scale factor(s).

The first way to call scale() uses numbers to set the amount of scaling. The first parameter, s, sets the amount to scale each axis. For example, calling scale(2) stretches the x-, y-, and z-axes by a factor of 2. The next two parameters, y and z, are optional. They set the amount to scale the y- and z-axes. For example, calling scale(2, 0.5, 1) stretches the x-axis by a factor of 2, shrinks the y-axis by a factor of 0.5, and leaves the z-axis unchanged.

The second way to call scale() uses a p5.Vector object to set the scale factors. For example, calling scale(myVector) uses the x-, y-, and z-components of myVector to set the amount of scaling along the x-, y-, and z-axes. Doing so is the same as calling scale(myVector.x, myVector.y, myVector.z).

By default, transformations accumulate. For example, calling scale(1) twice has the same effect as calling scale(2) once. The push() and pop() functions can be used to isolate transformations within distinct drawing groups.

Note: Transformations are reset at the beginning of the draw loop. Calling scale(2) inside the draw() function won't cause shapes to grow continuously.

scale() can also be used in shaders with p5.strands. Inside a shader callback, it takes a transform created with transform2D() or transform3D() as its first parameter, as in scale(myTransform, 2). Instead of resizing the coordinate system, it returns a new transform with the resize added on and leaves the one passed in untouched, so the result has to be assigned back. The finished transform is applied to a position with transformPoint(). See its reference for a more in-depth explanation of how that works. The following example uses scale() to stretch a sphere.

Examples

Syntax

scale(s, [y], [z])
scale(scales)
scale(transform, x, [y], [z])

Parameters

s
Number|p5.Vector|Number[]: amount to scale along the positive x-axis.
y
Number: amount to scale along the positive y-axis. Defaults to s.
z
Number: amount to scale along the positive z-axis. Defaults to y.
scales
p5.Vector|Number[]: vector whose components should be used to scale.
transform
a transform created with transform2D() or transform3D(), inside a p5.strands shader callback.
x
Number: amount to resize by along the x-axis, or along every axis if it's the only amount given.

Returns

a new transform with the resize applied.

Related References