How to set the width of stroke of Ellipse using FabricJS?

FabricJSJavascriptHTML5 Canvas

In this tutorial, we are going to learn how to set the width of stroke of an Ellipse using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we will create an instance of fabric.Ellipse class and add it to the canvas. The strokeWidth property allows us to specify the width of a stroke for an object.

Syntax

new fabric.Ellipse( { strokeWidth: Number }: 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 object of which strokeWidth is a property.

Options Keys

  • strokeWidth − This property accepts a Number value which allows us to specify the width of a stroke for an object. Its default value is 1.

Example 1

Default appearance of an object's stroke

The following example depicts the default appearance of the stroke of an ellipse object. Since we have not used the strokeWidth property, the default width is being rendered.

<!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 width of stroke of an Ellipse using FabricJS</h2>
      <p>Here we haven't set the <b>strokeWidth</b> explicitly. By default, it is set to 1. </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: 115,
            top: 50,
            rx: 80,
            ry: 50,
            fill: "#ffdead",
            stroke: "#cd853f",
         });

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

Example 2

Passing strokeWidth property as key

In this example, we are passing the strokeWidth property a value of 5. This will make sure that our ellipse object is rendered with a stroke that has a width of 5 pixels.

<!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 width of stroke of an Ellipse using FabricJS</h2>
      <p>Observe the stroke width of the ellipse. Here we have set the <b>strokeWidth</b> at 5 pixels.</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: 115,
            top: 50,
            rx: 80,
            ry: 50,
            fill: "#ffdead",
            stroke: "#cd853f",
            strokeWidth: 5,
         });

         // Adding it to the canvas
         canvas.add(ellipse);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>
raja
Updated on 25-May-2022 11:09:28

Advertisements