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


In this tutorial, we are going to learn how to set the width of stroke of a Rectangle using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we will have to create an instance of fabric.Rect 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.Rect( { strokeWidth: Number }: Object)

Parameters

  • Options (optional) − This parameter is an Object which provides additional customizations to our rectangle. 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 strokeWidth is a property.

Options keys

  • strokeWidth − This property accepts a Number 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

Let’s see a code example that depicts the default appearance of the stroke of a rectangle 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>Default appearance of an object’s stroke</h2>
   <p>You can see the default object width of the stroke around the rectangle.</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a rectangle object
      var rect = new fabric.Rect({
         left: 55,
         top: 70,
         width: 170,
         height: 70,
         fill: "#f4f0ec",
         stroke: "#2a52be",
      });

      // Add it to the canvas
      canvas.add(rect);
   </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 rectangle 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>Passing strokeWidth property as key</h2>
   <p>You can now see that the stroke width is 5px wide around the rectangle.</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a rectangle object
      var rect = new fabric.Rect({
         left: 55,
         top: 70,
         width: 170,
         height: 70,
         fill: "#f4f0ec",
         stroke: "#2a52be",
         strokeWidth: 5,
      });

      // Add it to the canvas
      canvas.add(rect);
   </script>
</body>
</html>

Updated on: 30-Jun-2022

378 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements