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 size of the controlling corners of a Triangle using FabricJS?
In this tutorial, we are going to learn how to set the size of the controlling corners of a Triangle using FabricJS. The controlling corners of an object allow us to scale, stretch or change its position. We can customize our controlling corners in many ways such as adding a specific colour to it, changing its size etc. We can change the size by using the cornerSize property.
Syntax
new fabric.Triangle({ cornerSize: Number }: Object)
Parameters
-
Options (optional) ? This parameter is an Object which provides additional customizations to our triangle. 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 cornerSize is a property.
cornerSize Property
-
cornerSize ? This property accepts a Number which allows us to manipulate the size of the controlling corners of a selected object. Its default value is 13.
Example 1: Default Corner Size
Let's see a code example that depicts the default size of the controlling corners of a triangle object when it is actively selected.
<!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>Default size of the controlling corners</h2>
<p>Select the triangle to see the default size of controlling corners</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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 60,
width: 100,
height: 70,
fill: "#5f9ea0",
cornerColor: "rgba(255,103,0,0.9)",
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
Example 2: Custom Corner Size
In this example, we are passing the cornerSize property as key with a value of 17. We can see how that changes the size of our controlling corners when the triangle object is selected.
<!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 cornerSize as key with a custom value</h2>
<p>Select the triangle to see the increased size of the controlling corners.</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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 60,
width: 100,
height: 70,
fill: "#5f9ea0",
cornerColor: "rgba(255,103,0,0.9)",
cornerSize: 17,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
Key Points
- The cornerSize property controls the size of selection handles around objects
- Default corner size is 13 pixels
- Larger values make corners easier to grab but may cover more of the object
- The property works with cornerColor to create visually distinct selection handles
Conclusion
The cornerSize property in FabricJS allows you to customize the size of controlling corners for better user interaction. Use larger values for easier selection on touch devices or smaller values for precise control.
