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 Text using FabricJS?
In this tutorial, we are going to learn how to set the style of controlling corners of Text 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 style by using the cornerStyle property.
Syntax
new fabric.Text(text: String, { cornerStyle: String }: Object)
Parameters
text ? This parameter accepts a String which is the text string that we want to display.
options (optional) ? This parameter is an Object which provides additional customizations to our text. Using this parameter 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. Available values are "rect" (default) and "circle".
Example 1: Default Corner Style
Let's see a code example that shows the default rectangular style of controlling corners.
<!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>You can select the text object 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 text object
var text = new fabric.Text("Add Sample Text Here", {
top: 70,
left: 60,
cornerColor: "#87a96b",
});
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Example 2: Circle Corner Style
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 we have done 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>You can select the text object to see that the corner style has changed to circle</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 text object
var text = new fabric.Text("Add Sample Text Here", {
top: 70,
left: 60,
cornerColor: "#87a96b",
cornerStyle: "circle",
});
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Available Corner Styles
| Value | Description | Appearance |
|---|---|---|
"rect" |
Default rectangular corners | Square/rectangular control handles |
"circle" |
Circular corners | Round control handles |
Key Points
The
cornerStyleproperty only affects the visual appearance of control corners when an object is selected.You can combine
cornerStylewith other corner properties likecornerColorandcornerSizefor complete customization.The corner style applies to all corners of the selected object uniformly.
Conclusion
The cornerStyle property in FabricJS allows you to customize the appearance of object control corners. Use "rect" for square corners or "circle" for round corners to match your application's design requirements.
