If you're looking to create mesmerizing generative art, you'll want to dive headfirst into the world of transformations in p5.js. Transformations allow you to manipulate shapes and objects on the canvas in various ways, such as translating, rotating, and scaling. In this article, we'll explore these transformation functions and demonstrate how you can use them to level up your generative art game.
What Are Transformations?
In the realm of p5.js, transformations are functions that modify the position, rotation, or size of your canvas elements. They essentially act as a set of instructions for the renderer to follow when drawing your shapes and objects. Transformations are handy when you want to create complex, dynamic visuals without needing to manually calculate every single coordinate or angle.
Types of Transformations
There are three primary types of transformations in p5.js:
- Translate: This function shifts the origin point (0, 0) of the canvas to a new location. It's useful when you want to move an entire group of objects or change the center point of your canvas.
- Rotate: The rotate function spins your entire canvas around the origin point by a specified angle (in radians). This is helpful when you want to create rotating visuals or manipulate objects around a central point.
- Scale: Lastly, the scale function resizes your canvas elements by a given factor, either uniformly or non-uniformly. This is great for creating zooming effects, modifying object sizes, or generating perspective illusions.
Using Transformations in p5.js
Now that we know what transformations are and their different types, let's see how to use them in a p5.js sketch.
translate() function in p5.js moves the origin point of the canvas to a new location. It accepts two arguments,
y, which define the new origin point.
In this example, we translate the origin point to (100, 100), and then draw a rectangle. The rectangle's top-left corner is now at the new origin point, (100, 100).
rotate() function in p5.js rotates the entire canvas around the origin point by a specified angle, given in radians. To convert degrees to radians, you can use the
In this example, we first translate the origin point and then rotate the canvas by 45 degrees. The rectangle we draw will be rotated by 45 degrees around the new origin point (100, 100).
scale() function in p5.js resizes your canvas elements by a given factor. You can provide one or two arguments to scale uniformly or non-uniformly, respectively.
In this example, we first translate the origin point and then scale the canvas by a factor of 2. The rectangle we draw will be twice its original size, having its top-left corner at the new origin point (100, 100).
By combining these transformation functions, you can create intricate and mesmerizing generative art. Remember that the order in which you apply transformations matters, as they build upon each other.
In this example, we translate the origin point to the center of the canvas, rotate it based on the frame count, and scale it using a sine wave. This creates a captivating spinning and pulsating square animation.
By mastering transformations in p5.js, you'll be well on your way to creating stunning generative art that mesmerizes and engages audiences. Happy coding!