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 Y-axis of a Rectangle using FabricJS?
In this tutorial, we are going to learn how to set the angle of skew on the y-axis of a Rectangle using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we will have to create an instance of fabric.Rect class and add it to the canvas.
Our rectangle 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.Rect({ skewY : Number }: Object)
Parameters
-
Options (optional) ? This parameter is an Object which provides additional customizations to our rectangle. 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 in degrees.
Example 1: Default Rectangle Without Skew
Let's see a code example to understand how our rectangle object appears when the skewY property is not applied. In this case, there will be no skew by any angle in our rectangle 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>Rectangle without skewY property</h2>
<p>You can see there is no skew by any angle on the rectangle 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 rectangle object
var rect = new fabric.Rect({
left: 105,
top: 70,
width: 170,
height: 70,
fill: "#8f9779",
stroke: "#8fbc8f",
strokeWidth: 9,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>
Example 2: Applying skewY Property
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 in degrees.
<!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>Rectangle with skewY property set to 30 degrees</h2>
<p>You can see the rectangle has been skewed by 30 degrees 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 rectangle object with skewY
var rect = new fabric.Rect({
left: 105,
top: 70,
width: 170,
height: 70,
fill: "#8f9779",
stroke: "#8fbc8f",
strokeWidth: 9,
skewY: 30,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>
Key Points
- The
skewYproperty accepts numerical values in degrees - Positive values skew the rectangle in one direction, negative values in the opposite direction
- The skew transformation is applied along the Y-axis, creating a slanted effect
- This property can be combined with other transformation properties like
skewX,scaleX, andscaleY
Conclusion
The skewY property in FabricJS provides an easy way to apply Y-axis skewing transformations to rectangle objects. By adjusting the numerical value, you can control the angle and direction of the skew effect.
