Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Transforming Shapes | Transformations and Interactivity
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Generative Art with JavaScript and P5.js

bookTransforming Shapes

index.html

index.html

copy

To create dynamic and visually interesting compositions in P5.js, you can use transformation functions to manipulate the position, orientation, and size of shapes. The three most common transformation functions are translate(), rotate(), and scale(). Each of these functions affects how shapes are drawn on the canvas, and the order in which you apply them is crucial.

The translate(x, y) function moves the origin of the drawing context by the specified x and y amounts. After calling translate(), all subsequent drawing commands are positioned relative to this new origin. This is especially useful for positioning shapes without manually adjusting their coordinates.

The rotate(angle) function rotates the drawing context by the specified angle (in radians) around the current origin. Any shapes drawn after rotate() will be rotated by this angle. Remember, rotation always happens around the current origin, so using translate() before rotate() changes the point around which the rotation occurs.

The scale(sx, sy) function changes the size of shapes by scaling the drawing context. The scale factors sx and sy stretch or shrink the context horizontally and vertically. Scaling is also performed relative to the current origin, so the position of the origin (after any translation) affects how scaling is applied.

The order of transformations matters because each one builds on the changes made by the previous ones. Typically, you translate first to set the origin, then rotate around that origin, and finally scale the shape as needed. Using the push() and pop() functions lets you isolate transformations so they only affect specific shapes, preventing unintended changes to other parts of your drawing.

Note
Study More

Matrix transformations are a fundamental concept in computer graphics. They allow you to combine multiple transformations (like translation, rotation, and scaling) into a single operation using matrix multiplication. Learning about transformation matrices can help you understand how graphics software and hardware efficiently apply complex transformations to shapes.

question mark

Which transformation function in P5.js changes the position of the origin for subsequent drawing commands?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you give an example of how to use these transformation functions together?

What happens if I change the order of translate, rotate, and scale?

How do push() and pop() work in practice with transformations?

bookTransforming Shapes

Swipe to show menu

index.html

index.html

copy

To create dynamic and visually interesting compositions in P5.js, you can use transformation functions to manipulate the position, orientation, and size of shapes. The three most common transformation functions are translate(), rotate(), and scale(). Each of these functions affects how shapes are drawn on the canvas, and the order in which you apply them is crucial.

The translate(x, y) function moves the origin of the drawing context by the specified x and y amounts. After calling translate(), all subsequent drawing commands are positioned relative to this new origin. This is especially useful for positioning shapes without manually adjusting their coordinates.

The rotate(angle) function rotates the drawing context by the specified angle (in radians) around the current origin. Any shapes drawn after rotate() will be rotated by this angle. Remember, rotation always happens around the current origin, so using translate() before rotate() changes the point around which the rotation occurs.

The scale(sx, sy) function changes the size of shapes by scaling the drawing context. The scale factors sx and sy stretch or shrink the context horizontally and vertically. Scaling is also performed relative to the current origin, so the position of the origin (after any translation) affects how scaling is applied.

The order of transformations matters because each one builds on the changes made by the previous ones. Typically, you translate first to set the origin, then rotate around that origin, and finally scale the shape as needed. Using the push() and pop() functions lets you isolate transformations so they only affect specific shapes, preventing unintended changes to other parts of your drawing.

Note
Study More

Matrix transformations are a fundamental concept in computer graphics. They allow you to combine multiple transformations (like translation, rotation, and scaling) into a single operation using matrix multiplication. Learning about transformation matrices can help you understand how graphics software and hardware efficiently apply complex transformations to shapes.

question mark

Which transformation function in P5.js changes the position of the origin for subsequent drawing commands?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt