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 style of controlling corners of a Triangle using FabricJS?
In this tutorial, we are going to learn how to set the style of controlling corners of Triangle using FabricJS. Triangle is one of the various shapes provided by FabricJS. In order to create a triangle, we will have to create an instance of fabric.Triangle class and add it to the canvas.
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 style by using the cornerStyle property.
Syntax
new fabric.Triangle({ cornerStyle: String }: 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 cornerStyle is a property.
Options Keys
-
cornerStyle ? This property accepts a String which allows us to specify the style of controlling corners that we want. Valid values are "rect" (default) and "circle".
Example 1: Default Style of Controlling Corners
Let's see a code example that shows the default style of controlling corners, which are rectangular:
<!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 style of controlling corners</h2>
<p>Select the triangle to see the default style 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: Setting cornerStyle to "circle"
We can specify the style or appearance of the controlling corners of an actively selected object by passing the values as "circle" or "rect". Passing the value as "circle" will make the controlling corners appear circular, as shown in the example below:
<!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 cornerStyle as key with the value "circle"</h2>
<p>Select the triangle to see that the style of controlling corners is circle now</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)",
cornerStyle: "circle",
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
Comparison of Corner Styles
| cornerStyle Value | Appearance | Default |
|---|---|---|
| "rect" | Square/rectangular corners | Yes |
| "circle" | Circular corners | No |
Conclusion
The cornerStyle property in FabricJS allows you to customize the appearance of controlling corners. Use "rect" for square corners or "circle" for rounded corners to match your design preferences.
