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 an Ellipse with a border color using FabricJS?
In this tutorial, we are going to create an Ellipse with a border color using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we will create an instance of fabric.Ellipse class and add it to the canvas. Since FabricJS is extremely flexible, we are allowed to customize our ellipse object in any way we like. One of the properties that FabricJS provides is borderColor which allows us to manipulate the color of the border when our object is active.
Syntax
new fabric.Ellipse({ borderColor: String }: Object)
Parameters
-
options (optional) ? This parameter is an Object which provides additional customizations to our ellipse. Using this parameter color, 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 determines the color of the border when our object is actively selected. Its default value is rgb(178,204,255).
Example 1: Using Color Names
Passing borderColor key with a String value
Let's see an 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 ellipse 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>How to create an Ellipse with a border color using FabricJS?</h2>
<p>Select the object to see the "blue" border color.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an Ellipse instance
var ellipse = new fabric.Ellipse({
left: 215,
top: 100,
rx: 90,
ry: 50,
fill: "red",
borderColor: "blue",
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Example 2: Using RGB Values
Passing a RGB value to the borderColor key
Instead of passing simple color names as a String, we can also use RGB values, whose components specify the amount of Red, Green, and Blue. In this example, we have used rgb(128,0,128) which is the RGB value for the color 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>How to create an Ellipse with a border color using FabricJS?</h2>
<p>Select the object to see the "purple" border color.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an Ellipse instance
var ellipse = new fabric.Ellipse({
left: 215,
top: 100,
rx: 90,
ry: 50,
fill: "red",
borderColor: "rgb(128,0,128)",
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Key Points
- The borderColor property only becomes visible when the ellipse object is actively selected
- You can use color names like "blue", "red", "purple" or RGB/HEX values
- The default border color is a light blue: rgb(178,204,255)
- The border color affects the selection controls around the object
Conclusion
The borderColor property in FabricJS provides an easy way to customize the selection border of ellipse objects. You can use either color names or RGB values to achieve the desired visual effect when objects are selected.
