How to set the height of a Circle using FabricJS?


In this tutorial, we are going to learn how to set the height 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. We can manipulate a circle object by changing its position, opacity, stroke and also its dimension. FabricJS allows us to control an object's dimensions by using the width and height properties.

Syntax

new fabric.Circle({ height: 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 height is a property.

Options Keys

  • height − This property accepts a Number allows us to specify the object's height. The value that is assigned, determines the height.

Example 1

Default behaviour of height property

Let's see an example to understand the default behaviour of the height property.

<!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 height of a Circle using FabricJS</h2>
      <p>Note that the dimensions of the circle are controlled by its radius. Select the object and observe the height of its controlling borders. Here we have not used the <b>height</b> property and this is the default appearnace. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "white",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
         });

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

Example 2

 Passing height property as key

Since the dimensions of a circle are determined by its radius, there will not be any changes in its size. However, as we can see, the height of the controlling borders has changed after assigning the height property a value.

<!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 height of a Circle using FabricJS</h2>
      <p>Select the object and notice the height of its controlling borders. Here we have set the <b>height</b> to 150px. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var circle = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "white",
            radius: 50,
            stroke: "#c154c1",
            strokeWidth: 5,
            height: 150
         });

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

Updated on: 25-May-2022

248 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements