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 hide the controlling corners of a Circle using FabricJS?
In this tutorial, we are going to learn how to hide the controlling corners of a Circle using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we have to create an instance of fabric.Circle class and add it to the canvas. The controlling corners of an object allow us to increase or decrease its scale, stretch or change its position. We can customize our controlling corners in many ways such as adding a specific colour to it, changing their size etc. However, we can also hide them using the hasControls property.
Syntax
new fabric.Circle({ hasControls: 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 hasControls is a property.
Options Keys
-
hasControls ? This property accepts a Boolean value that allows us to display or hide the controlling corners of an actively selected object. Its default value is true.
Example 1: Default Appearance of Controlling Corners
Let's see a code that shows the default appearance of the controlling corners. Since the default value of hasControls property is true, the controlling corners will not be hidden.
<!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>Hiding the controlling corners of a circle using FabricJS</h2>
<p>Select the object and observe its controlling corners. This is the default appearance. Even though we have not applied the <b>hasControls</b> property, it is by default set to True.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 215,
top: 100,
fill: "white",
radius: 50,
stroke: "#c154c1",
strokeWidth: 5
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Example 2: Hiding Controlling Corners
In this example, we will see how the controlling corners are hidden by using the hasControls property. We need to assign the hasControls key a 'false' value. By doing that, the controlling corners will be hidden.
<!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>Hiding the controlling corners of a circle using FabricJS</h2>
<p>Select the object and you will notice that the controlling corners are no longer there. Here we have set <b>hasControls</b> to False.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 215,
top: 100,
fill: "white",
radius: 50,
stroke: "#c154c1",
strokeWidth: 5,
hasControls: false
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Key Points
The
hasControlsproperty is a Boolean value that controls the visibility of object controlsWhen set to
true(default), controlling corners are visible and allow interactionWhen set to
false, controlling corners are hidden, preventing resize and rotation actionsThis property affects only the visual controls, not the object's selectability
Conclusion
The hasControls property in FabricJS provides an easy way to hide controlling corners of canvas objects. Setting it to false removes the visual controls while keeping the object selectable, useful for read-only or display-only scenarios.
