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 flip an Ellipse vertically using FabricJS?
In this tutorial, we are going to learn how we can flip an Ellipse object vertically using FabricJS. Ellipse is one of the various shapes provided by FabricJS. In order to create an ellipse, we will have to create an instance of fabric.Ellipse class and add it to the canvas. We can flip an ellipse object vertically using the flipY property.
Syntax
new fabric.Ellipse({ flipY: Boolean }: 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 flipY is a property.
Options Keys
-
flipY ? This property accepts a Boolean value. It allows us to flip an object vertically.
Example 1: Default Orientation (flipY: false)
Let's see an example that shows us the default orientation of an ellipse object in FabricJS. Since we are passing the flipY property a "false" value, the ellipse object will not flip vertically.
<!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 flip an Ellipse vertically using FabricJS?</h2>
<p>Select the object and try to flip it vertically.</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: 215,
top: 100,
fill: "green",
rx: 100,
ry: 60,
stroke: "#228b22",
strokeWidth: 2,
flipY: false,
});
// Create gradient fill
ellipse.set("fill", new fabric.Gradient({
type: "linear",
coords: {
x1: 0,
y1: 0,
x2: 0,
y2: 50
},
colorStops: [{
offset: 0,
color: "red"
}, {
offset: 1,
color: "green"
}],
}));
// Adding them to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
Example 2: Flipped Vertically (flipY: true)
In this example, we have an ellipse object of horizontal radius 100 and vertical radius 60 with a vertical linear gradient fill. As we apply the flipY property to the ellipse object, it flips vertically and thus we see that the gradient has flipped as well.
<!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 flip an Ellipse vertically using FabricJS?</h2>
<p>Select the object and try to flip it vertically. Here we have set the <b>flipY</b> property as True.</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: 215,
top: 100,
fill: "green",
rx: 100,
ry: 60,
stroke: "#228b22",
strokeWidth: 2,
flipY: true,
});
// Create gradient fill
ellipse.set("fill", new fabric.Gradient({
type: "linear",
coords: {
x1: 0,
y1: 0,
x2: 0,
y2: 50
},
colorStops: [{
offset: 0,
color: "red"
}, {
offset: 1,
color: "green"
}],
}));
// Adding them to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
How It Works
The flipY property performs a vertical transformation on the ellipse object. When set to true, it mirrors the ellipse along its horizontal axis. This is particularly visible when the ellipse has asymmetric features like gradients, patterns, or text.
Conclusion
The flipY property in FabricJS provides an easy way to vertically flip ellipse objects. Set it to true to flip vertically, or false for the default orientation.
