- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set the angle of rotation of an Ellipse using FabricJS?
In this tutorial, we are going to set the angle of rotation of an Ellipse using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we have to create an instance of fabric.Ellipse class and add it to the canvas. The angle property in FabricJS defines the angle of 2D rotation of an object. We also have the centeredRotation property that allows us to use the center point of an ellipse as the origin of transformation.
Syntax
new fabric.Ellipse({ angle: Number, centeredRotation: Boolean }: Object)
Parameters
options (optional) − This parameter is an Object which provides additional customizations to our ellipse. Using this parameter color, cursor, stroke width and a lot of other properties can be changed related to the canvas of which angle and centeredRotation are properties.
Options Keys
angle − This property accepts a Number which specifies the angle of rotation of an ellipse in degrees.
centeredRotation − This property accepts a Boolean value which determines whether the center of the ellipse is the origin of transformation or not.
Example 1
Passing angle as key with a custom value and disabling the centered rotation for the ellipse
Let's take an example to set the angle of rotation of an Ellipse in FabricJS. A negative angle specifies counter-clockwise direction, whereas a positive angle specifies a clockwise direction. Since we have assigned centeredRotation a "false" value, the ellipse will rotate while using its corner point as the center of rotation.
<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>How to set the angle of rotation of an Ellipse using FabricJS?</h2> <p>Select the object and rotate it. Here we have set the angle of rotation at <b>-40</b></p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Initiate an Ellipse instance var ellipse = new fabric.Ellipse({ left: 180, top: 180, rx: 90, ry: 50, fill: "green", stroke: "blue", strokeWidth: 2, angle: -40, centeredRotation: false }) // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
Example 2
Enabling centered rotation for the ellipse
As we can see from this example, by setting the centeredRotation property as "true", our ellipse now uses its center as the center of rotation. Prior to version 1.3.4, centeredScaling and centeredRotation were enclosed within one single property called centerTransform.
<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>How to set the angle of rotation of an Ellipse using FabricJS?</h2> <p>Select the object and rotate it. You will notice that the object rotates around its center as we have set the <b>centeredRotation</b> property to True. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Initiate an Ellipse instance var ellipse = new fabric.Ellipse({ left: 180, top: 180, rx: 90, ry: 50, fill: "green", stroke: "blue", strokeWidth: 2, angle: -40, centeredRotation: true }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>