How to set the position of a Circle from left using FabricJS?

FabricJSJavascriptHTML5 Canvas

In this tutorial, we are going to learn how to set the position of a Circle from left 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. We can manipulate a circle object by changing its position, opacity, stroke and also its dimension. The position from left can be changed by using the left property.

Syntax

new fabric.Circle( { left: Number }: Object)

Parameters

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

Options Keys

  • left − This property accepts a Number which sets the left position of an object. The value determines how far from left the object will be placed.

Example 1

Default placement of the circle object

Let's see a code example to understand the default placement of a circle object in the canvas when its position has not been changed.

<!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>Setting the position of Circle from left using FabricJS</h2>
      <p>This is the default placement of the circle. Here we have not used the <b>left</b> property. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            fill: "white",
            radius: 100,
            stroke: "yellow",
            strokeWidth: 3
         });
         
         // Adding it to the canvas
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>

Example 2

Passing left property as key

In this example, we are assigning the left property with a custom value. Since it accepts a Number, you must assign it a numerical value which will represent its position from the left.

<!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>Setting the position of Circle from left using FabricJS</h2>
      <p>Here we have used the <b>left</b> property and assigned it a custom value to set the position of the circle from left. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 115,
            fill: "white",
            radius: 100,
            stroke: "yellow",
            strokeWidth: 3,
         });

         // Adding it to the canvas
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>
raja
Updated on 27-May-2022 07:27:49

Advertisements