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 angle of skew on the Y-axis of a Textbox using FabricJS?
In this tutorial, we are going to learn how to set the angle of skew on the y-axis of a Textbox using FabricJS. We can customize, stretch or move around the text written in a textbox. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. Our textbox object can be customized in various ways like changing its dimensions, adding a background color or by changing the angle of skew on the y-axis. We can do this by using the skewY property.
Syntax
new fabric.Textbox(text: String, { skewY : Number }: Object)
Parameters
-
text ? This parameter accepts a String which is the text string that we want to display inside our textbox.
-
options (optional) ? This parameter is an Object which provides additional customizations to our textbox. 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 skewY is a property.
Options Keys
-
skewY ? This property accepts a Number which determines the angle of skew on y-axis of an object.
Example 1
When the skewY property is not applied
Let's see a code example to understand how our textbox object appears when the skewY property is not applied. In this case, there will be no skew by any angle in our textbox object.
<!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>When the skewY property is not applied</h2>
<p>You can see there is no skew by any angle on the textbox by default</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 textbox object
var textbox = new fabric.Textbox("A smile cures the wounding of a frown.", {
backgroundColor: "#fffff0",
width: 400,
left: 110,
top: 70,
fill: "violet",
strokeWidth: 2,
stroke: "blue",
textAlign: "center",
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Example 2
Passing skewY as key and assigning a custom value to it.
In this example, we will see how we can assign a numerical value to the skewY property. The value that is being passed will determine the skew along the Y-axis.
<!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 skewY as key and assigning a custom value to it.</h2>
<p>You can see the textbox has been skewed along the y-axis</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 textbox object
var textbox = new fabric.Textbox("A smile cures the wounding of a frown.", {
backgroundColor: "#fffff0",
width: 400,
left: 110,
top: 70,
fill: "violet",
strokeWidth: 2,
stroke: "blue",
textAlign: "center",
skewY: 30,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
How It Works
The skewY property applies a transformation to the textbox along the vertical axis. When set to 30 degrees (as in Example 2), the textbox appears slanted, creating an italicized or oblique effect. The value is measured in degrees, where positive values skew in one direction and negative values skew in the opposite direction.
Conclusion
The skewY property in FabricJS allows you to create visually dynamic textboxes by applying vertical skewing transformations. This feature is useful for creating stylized text effects in canvas-based applications.
