- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set the border colour of a Line in FabricJS?
In this tutorial, we are going to learn about how to set the border colour of Line in FabricJS. A Line element is one of the basic elements provided in FabricJS. It is used for creating straight lines. Because line elements are geometrically one-dimensional and do not contain an interior, they are never filled.
We can create a line object by creating an instance of fabric.Line, specifying the x and y coordinates of the line and adding it to the canvas. The property borderColor allows us to manipulate the colour of the border when our object is active.
Syntax
new fabric.Line( points: Array, { borderColor: String }: Object)
Parameters
points − This parameter accepts an Array of points which determines the (x1, y1) and (x2, y2) values, those being the x- and y-axis coordinates of the start and end of the line respectively.
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 object of which borderColor is a property.
Options Keys
borderColor − This property accepts a String which specifies the colour of the border when our line object is selected. Its default value is rgb(178,204,255).
Passing borderColor key with a String value
Example
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 line 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 line object to see that the border colour is red. </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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, borderColor: "red" }); // Add it to the canvas canvas.add(line); </script> </body> </html>
Passing a rgba value to the borderColor key
Example
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 line object to see the border colour added using rgba value </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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, borderColor: "rgba(164,0,0,1)" }); // Add it to the canvas canvas.add(line); </script> </body> </html>
- Related Articles
- How to set the border colour of Image in FabricJS?
- How to set the background colour of Line in FabricJS?
- FabricJS – How to set the colour of the controlling corners of a Line?
- FabricJS – How to set the border opacity of a Line while moving it?
- How to set the border colour of text object while in editing mode in FabricJS?
- FabricJS – How to set the stroke colour of the controlling corners of a Line?
- How to create a Circle with border colour using FabricJS?
- How to create a Triangle with border colour using FabricJS?
- How to create a Rectangle with border colour using FabricJS?
- How to create a Textbox with border colour using FabricJS?
- How to set the angle of a Line in FabricJS?
- How to set the fill colour of a Triangle using FabricJS?
- How to set the fill colour of a Rectangle using FabricJS?
- How to set the scale factor (border) of a Circle using FabricJS?
- How to set the background colour of selection of a Triangle using FabricJS?
