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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-08-25 12:29:09
forward
1102 people have browsed it

如何使用 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)
Copy after login

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>
Copy after login

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>
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template