Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Canvas Transform

Posted in Canvas Tutorial
Updated on May 19, 2024
By Mari Selvan
👁️ 37 - Views
⏳ 4 mins
💬 1 Comment
Canvas Transform

Photo Credit to CodeToFun

🙋 Introduction

Canvas transformation is a powerful feature in HTML5 that allows developers to manipulate the drawing context of the <canvas> element. By applying transformations, such as scaling, rotation, translation, and skewing, developers can create complex and dynamic visual effects.

In this guide, we'll explore the syntax, methods, and examples of canvas transformations.

💡 Syntax

The canvas transformation functions are part of the 2D rendering context, accessed through the getContext('2d') method. Here's a basic syntax overview:

Syntax
Copied
Copy To Clipboard
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');

// Translate
ctx.translate(x, y);

// Scale
ctx.scale(scaleX, scaleY);

// Rotate
ctx.rotate(angle);

// Skew
ctx.transform(a, b, c, d, e, f);

// Reset transformations
ctx.setTransform(1, 0, 0, 1, 0, 0);

🧰 Methods and Parameters

  • translate(x, y): Moves the origin of the canvas to the specified (x, y) coordinates.
  • scale(scaleX, scaleY): Scales the canvas by the given factors along the x and y axes.
  • rotate(angle): Rotates the canvas around the origin by the specified angle (in radians).
  • transform(a, b, c, d, e, f): Applies a custom 2D transformation matrix to the canvas.
  • setTransform(a, b, c, d, e, f): Resets the current transformation matrix to the identity matrix.

📄 Example

Let's demonstrate a simple canvas transformation:

index.html
Copied
Copy To Clipboard
<canvas id="myCanvas" width="200" height="200"></canvas>
<script>
  const canvas = document.getElementById('myCanvas');
  const ctx = canvas.getContext('2d');

  // Translate the origin to (100, 100)
  ctx.translate(100, 100);

  // Rotate by 45 degrees
  ctx.rotate(Math.PI / 4);

  // Draw a square
  ctx.fillRect(-50, -50, 100, 100);
</script>

🧠 How it works?

In this example, we translate the origin to the center of the canvas, rotate it by 45 degrees, and then draw a square centered at the new origin.

🎉 Conclusion

Canvas transformations open up a world of possibilities for creating dynamic and interactive graphics in web applications.

By understanding the syntax and methods for applying transformations, developers can unleash their creativity and build engaging visual experiences.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy