# How to add dashes to the border of a selection area on a canvas using FabricJS?

FabricJSJavascriptHTML5 Canvas

In this article, we are going to learn how to add dashes to the border of a selection area on a canvas using FabricJS. We can achieve this by using the selectionDashArray property. It allows us to make the border of a selection area dashed.

## Syntax

new fabric.Canvas(element: HTMLElement|String, { selectionDashArray: Array }: Object)

## Parameters

• element − This parameter is the <canvas> element itself which can be derived using document.getElementById() or the id of the <canvas> element itself. The FabricJS canvas will be initialized on this element.

• options (optional) − This parameter is an Object which provides additional customizations to our canvas. Using this parameter color, cursor, border width and a lot of other properties can be changed related to the canvas, of which selectionDashArray is a property. It accepts an array which determines the dash pattern that we want.

## Example 1

Passing the selectionDashArray as key to the class

selectionDashArray allows us to make the border of a selection area dashed. The way to define a dash pattern is by specifying the length of dashes in an array. In the example below we have taken a [7,6] array. This means, there would be a 7px long line followed by a 6px gap and so on.

<!DOCTYPE html>
<html>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
<body>
<h2>Adding dashes to the border of a selection area on a canvas</h2>
<p>Select an area around the object. The border of the selection area would have dashed lines.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas", {
selectionDashArray: [7, 6],
selectionBorderColor: "red"
});
// Creating an instance of the fabric.Rect class
var circle = new fabric.Circle({
left: 200,
top: 100,
fill: "blue",
});
// Adding it to the canvas
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>

## Example 2

Using selectionDashArray in combination with selectionLineWidth and selectionBorderColor

The selectionDashArray property can be used in a number of ways. One way is to use it in combination with selectionLineWidth and selectionBorderColor which specify the width of the border of a selection and the color of the border of the selection, respectively.

<!DOCTYPE html>
<html>
<!-- Adding the Fabric JS Library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>
<body>
<h2>Adding dashes to the border of a selection area on a canvas</h2>
<p>Select an area around the object and observe the outline of the selection area. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas", {
selectionDashArray: [13, 16],
selectionLineWidth: 5,
selectionBorderColor: "green",
});
// Creating an instance of the fabric.Rect class
var circle = new fabric.Circle({
left: 200,
top: 100,
</html>