The scale() function in p5.js is used to increase or decrease the size of a shape or model by expanding or contracting its vertices. The scale values are specified as decimal percentages, that is, a scale value of “2.0” would increase the dimensions of the shape by 200%. Similarly, a negative of “-2.0” would decrease the dimensions of the shape by 200%
The objects always scale from their relative origin to the coordinate system. The z parameter of this function is only available in the WebGL mode for scaling across the z-axis.
scale() is a transformation, every transformation that happens after one call multiplies the effect. If
scale() is called within the
draw() loop, then the transformation is reset when the loop begins again.
scale( s, [y], [z] )
scale( scales )
Parameters: This function accepts four parameters as mentioned above and described below.
- s: It is a p5.Vector, Number or array of Numbers which define the percent to scale the object. If multiple arguments are given, it denotes the percent to scale the object in the x direction.
- y: It is a Number which denotes the percent to scale the object in the y direction. It is an optional parameter.
- z: It is a Number which denotes the percent to scale the object in the z direction. It is an optional parameter.
- scales: It is a p5.Vector or array of Numbers which specify per-axis percents to scale the object.
The program below illustrate the scale() function in p5.js: