How to set the angle of rotation of a Circle using FabricJS?


In this tutorial, we are going to set the angle of rotation of a Circle using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we have to create an instance of fabric.Circle 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 a circle as the origin of transformation.

Syntax

new fabric.Circle({ angle: Number, centeredRotation: Boolean }: Object)

Parameters

  • options (optional) − This parameter is an Object which provides additional customizations to our object. Using this parameter, properties such as colour, cursor, stroke width and a lot of other properties can be changed related to the circle of which angle and centeredRotation are properties.

Options Keys

  • angle − This property accepts a Number which specifies the angle of rotation of a circle in degrees.

  • centeredRotation − This property accepts a Boolean value which determines whether the center of the circle is the origin of transformation or not.

Example 1

Passing angle as key with a custom value and disabling the centered rotation for the circle

The following example demonstrates how to set the angle of rotation of a Circle 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 circle 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>Set the angle of rotation of a Circle using FabricJS</h2>
      <p>Select the object and observe its controlling corners. You will notice that the angle of rotation is set at 60 degrees in the anti-clockwise direction, as we have set the <b>angle</b> at <b>-60</b>. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            radius: 50,
            fill: "green",
            stroke: "blue",
            strokeWidth: 2,
            angle: -60,
            centeredRotation: false,
         });

         // Adding it to the canvas
         canvas.add(cir);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

Example 2

Enabling centered rotation for the circle

As we can see from this example, by setting the centeredRotation property as True, our circle 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>Set the angle of rotation of a Circle using FabricJS</h2>
      <p>Select the object and observe its controlling corners. Here we have set the angle of rotation at 60 degrees in the anti-clockwise direction, and when you rotate the circle, it will rotate around its center, as we have set <b>centeredRotation</b> to True. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            radius: 50,
            fill: "green",
            stroke: "blue",
            strokeWidth: 2,
            angle: -60,
            centeredRotation: true,
         });

         // Adding it to the canvas
         canvas.add(cir);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

Updated on: 25-May-2022

246 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements