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 make the controlling corners of a Circle transparent using FabricJS?
In this tutorial, we are going to learn how to make the controlling corners of Circle transparent using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we will create an instance of fabric.Circle class and add it to the canvas. The transparentCorners property allows us to control whether the selection corners are transparent or opaque.
Syntax
new fabric.Circle({ transparentCorners: Boolean }: Object)
Parameters
-
options (optional) ? This parameter is an Object which provides additional customizations to our circle. 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 transparentCorners is a property.
Options Keys
-
transparentCorners ? This property accepts a Boolean value which allows us to render the controlling corners of an object as transparent. Its default value is true.
Example 1: Non-Transparent Corners
Passing transparentCorners property as key with a 'false' value
Let's see a code example to create a circle object with controlling corners that are not transparent. To achieve this, we need to pass the transparentCorners property a false value.
<!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>Making the controlling corners of a circle transparent using FabricJS</h2>
<p>Select the object and observe its controlling corners. Here we have set <b>transparentCorners</b> to false. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
fill: "#adff2f",
radius: 50,
stroke: "#228b22",
transparentCorners: false
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Example 2: Transparent Corners (Default)
Passing transparentCorners as key with a 'true' value
In this example, we are passing the transparentCorners property a true value. This will make sure that the controlling corners are rendered as transparent. Note that this is also the default behavior.
<!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>Making the controlling corners of a circle transparent using FabricJS</h2>
<p>Select the object and observe its controlling corners. Here we have set <b>transparentCorners</b> to true, so the corners are transparent. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
fill: "#adff2f",
radius: 50,
stroke: "#228b22",
transparentCorners: true
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Key Points
When
transparentCornersis set tofalse, the selection corners appear as solid, opaque handles.When set to
true(default), the corners are transparent, showing only their borders.This property affects the visual appearance of object selection handles, making them more or less prominent based on your UI preferences.
Conclusion
The transparentCorners property in FabricJS provides control over the visibility of object selection corners. Use false for solid corners or true for transparent ones based on your design requirements.
