Home  >  Article  >  Web Front-end  >  How to add a dashed stroke to a rectangle using FabricJS?

How to add a dashed stroke to a rectangle using FabricJS?

WBOY
WBOYforward
2023-08-25 12:29:09927browse

如何使用 FabricJS 向矩形添加虚线描边?

In this tutorial, we will learn how to add a dashed stroke to a rectangle using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we must create an instance of the Fabric.Rect class and add it to the canvas. The StrokeDashArray property allows us to specify a dash pattern for the object's strokes.

Syntax

new fabric.Rect( { strokeDashArray: Array }: Object)

Parameters

  • Options (optional) - This parameter is a ## that provides additional customization #Object to our rectangle. Using this parameter, you can change the color, cursor, stroke width, and many other properties associated with the object for which StrokeDashArray is a property.

Options Keys

  • StrokeDashArray - This property allows us to specify a dash pattern for the object's strokes. We can do this by passing it an array. For example, if we pass an array with values ​​[2,3], it represents a dash pattern of 2px dashes and 3px gaps, and this pattern repeats infinitely.

Example 1

Default appearance of object strokes

Let's look at an example describing the default appearance of a rectangular object stroke Code examples. Since we are not using the

StrokeDashArray property, the dash pattern is not displayed.

<!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&rsquo;s stroke</h2>
   <p>You can see that there is no dashed stroke on 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: 7,
      });

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

Example 2

Passing the StrokeDashArray property as a key

In this example, we pass a The value is [9,2]. This means that a dashed pattern will be created with a 9 pixel long line followed by a 2 pixel long gap, then again a 9 pixel long line will be drawn, and so on.

<!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 strokeDashArray property as key</h2>
   <p>You can see now a dashed stroke has been added 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: 7,
         strokeDashArray: [9, 2],
      });

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

The above is the detailed content of How to add a dashed stroke to a rectangle using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete