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 horizontally using FabricJS?
In this tutorial, we are going to learn how we can flip an Ellipse object horizontally 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 horizontally using the flipX property.
Syntax
new fabric.Ellipse({ flipX: 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 flipX is a property.
Options Keys
-
flipX ? This property accepts a Boolean value. It allows us to flip an object horizontally.
Example 1: Default Orientation (flipX: false)
The following example shows us the default orientation of an ellipse object in FabricJS. Since we are passing the flipX property a "false" value, the ellipse object will not be flipped horizontally.
<!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 horizontally using FabricJS?</h2>
<p>Select the object and flip it horizontally.</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,
flipX: false,
});
// Create gradient fill
ellipse.set("fill", new fabric.Gradient({
type: "linear",
coords: {
x1: 0,
y1: 0,
x2: 50,
y2: 0
},
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: Horizontally Flipped (flipX: true)
In this example, we have an ellipse object of horizontal radius 100 and vertical radius 60 with a horizontal linear gradient fill. As we apply the flipX property to the ellipse object, it flips horizontally 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 horizontally using FabricJS?</h2>
<p>Select the object and try to flip it horizontally. Here we have set the <b>flipX</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,
flipX: true,
});
// Create gradient fill
ellipse.set("fill", new fabric.Gradient({
type: "linear",
coords: {
x1: 0,
y1: 0,
x2: 50,
y2: 0
},
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>
Key Points
- The
flipXproperty accepts a Boolean value (true/false) - Setting
flipX: truecreates a horizontal mirror image of the ellipse - The gradient fill also flips along with the ellipse shape
- The flip transformation affects the entire object including its fill patterns
Conclusion
The flipX property in FabricJS provides an easy way to horizontally flip ellipse objects. Set it to true to create a mirror image or false to maintain the default orientation.
