Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a Rectangle with border colour using FabricJS?
In this tutorial, we are going to create a Rectangle with border colour 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.
Since FabricJS is extremely flexible, we are allowed to customize our rectangle object in any way we like. One of the properties that FabricJS provides is borderColor which allows us to manipulate the colour of the border when our object is active.
Syntax
new fabric.Rect({ borderColor: String }: 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 borderColor is a property.
Options Keys
-
borderColor ? This property accepts a String which specifies the colour of the border when our rectangle object is selected. Its default value is rgb(178,204,255).
Example 1: Using Color Names
Let's see a code example of how we can assign a value to the borderColor property. We have assigned the value "blue" to the borderColor key which helps to create the blue border on selection of our rectangle object.
<!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 borderColor key with a String value</h2>
<p>Select the rectangle to see the border colour</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: 125,
top: 90,
width: 170,
height: 70,
fill: "#cf1020",
borderColor: "blue",
padding: 9,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>
Example 2: Using RGB Values
Instead of passing simple colour names as a String, we can also use RGB values, whose components specify the amount of Red, Green, Blue. In this example, we have used rgb(128,0,128) which is the RGB value for the colour purple.
<!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 an RGB value to the borderColor key</h2>
<p>Select the rectangle to see the border colour</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: 125,
top: 90,
width: 170,
height: 70,
fill: "#cf1020",
borderColor: "rgb(128,0,128)",
padding: 9,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>
Key Points
- The borderColor property only appears when the object is selected
- You can use color names, hex codes, RGB, or RGBA values
- The default border color is rgb(178,204,255) if not specified
- The border color works in conjunction with the padding property to create visible space
Conclusion
The borderColor property in FabricJS provides an easy way to customize the selection border of rectangle objects. You can use various color formats including color names and RGB values to achieve the desired visual effect.
