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 X-axis of Ellipse using FabricJS?
In this tutorial, we are going to learn how to set the angle of skew on X-axis of an Ellipse using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we will create an instance of fabric.Ellipse class and add it to the canvas. Our ellipse object can be customized in various ways like changing its dimensions, adding a background color or by changing the angle of skew on X-axis. We can do this by using the skewX property.
Syntax
new fabric.Ellipse({ skewX : Number }: Object)
Parameters
-
options (optional) ? This parameter is an Object which provides additional customizations to our ellipse. Using this parameter color, cursor, stroke width and a lot of other properties can be changed related to the object of which skewX is a property.
Options Keys
-
skewX ? This property accepts a Number which determines the angle of skew on X-axis of an object. The value represents degrees, where positive values skew clockwise and negative values skew counterclockwise.
Example 1: Default Ellipse (No skewX Applied)
Let's take an example to understand how our ellipse object appears when the skewX property is not applied. In this case, there will be no skew by any angle in our ellipse 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>Setting the angle of skew on the X-axis of an Ellipse using FabricJS</h2>
<p>Notice that by default the object is not skewed. Here we have not applied the <b>skewX</b> property.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 115,
top: 50,
rx: 80,
ry: 50,
fill: "#ff1493",
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Example 2: Applying skewX Property
In this example, we will see how we can assign a numerical value to the skewX property. The value that is being passed will determine the skew along the X-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>How to set the angle of skew on x-axis of Ellipse using FabricJS?</h2>
<p>Notice that the ellipse is skewed on the X-axis by 50 degrees in the clockwise direction because here we have applied the <b>skewX</b> property and given it a value of 50.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 115,
top: 50,
rx: 80,
ry: 50,
fill: "#ff1493",
skewX: 50,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Key Points
The
skewXproperty accepts numerical values representing degrees of skew.Positive values create clockwise skew, while negative values create counterclockwise skew.
The skew transformation affects the visual appearance without changing the object's actual dimensions.
You can combine
skewXwith other FabricJS properties for complex transformations.
Conclusion
The skewX property in FabricJS allows you to create visually appealing skewed ellipses by specifying the angle of transformation on the X-axis. This property is useful for creating dynamic visual effects and geometric transformations in your canvas applications.
