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 set the border colour of Image in FabricJS?
In this tutorial, we are going to learn how to set the border colour of Image in FabricJS. We can create an Image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to set the border colour of Image, we use the borderColor property.
Syntax
new fabric.Image( element: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String, { borderColor: String }: Object, callback: function)
Parameters
-
element ? This parameter accepts HTMLImageElement, HTMLCanvasElement, HTMLVideoElement or String which denotes the image element. The String should be a URL and would be loaded as an image.
-
options (optional) ? This parameter is an Object which provides additional customizations to our object. Using this parameter origin, stroke width and a lot of other properties can be changed related to the image object of which borderColor is a property.
-
callback (optional) ? This parameter is a function which is to be called after eventual filters are applied.
Options Keys
-
borderColor ? This property accepts a String which specifies the colour of the border when our image object is selected. Its default value is rgb(178,204,255).
Passing borderColor key with a String value
Let's see a code example of how we can assign a value to the borderColor property. We have assigned the value "red" to the borderColor key which helps to create the red border on selection of our image 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>You can select the image object to see that the border colour is red.
</p>
<canvas id="canvas"></canvas>
<img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating the image element
var imageElement = document.getElementById("img1");
// Initiate an Image object
var image = new fabric.Image(imageElement, {
top: 50,
left: 110,
borderColor: "red",
});
// Add it to the canvas
canvas.add(image);
</script>
</body>
</html>
Passing a rgba value to the borderColor key
Instead of passing simple colour names as a String, we can also use RGBA values, whose components specify the amount of Red, Green, Blue and Alpha, where alpha denotes the opacity. In this example we have used rgba(164,0, 0,1) which is the rgb value for the colour dark red.
<!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 a rgba value to the borderColor key</h2>
<p>
You can select the image object to see the border colour added using rgba value
</p>
<canvas id="canvas"></canvas>
<img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating the image element
var imageElement = document.getElementById("img1");
// Initiate an Image object
var image = new fabric.Image(imageElement, {
top: 50,
left: 110,
borderColor: "rgba(164,0,0,1)",
});
// Add it to the canvas
canvas.add(image);
</script>
</body>
</html>
Conclusion
The borderColor property in FabricJS allows you to customize the selection border color of Image objects using color names, hex values, or RGBA values. This property enhances the visual feedback when users interact with images on the canvas.
