如何使用 FabricJS 垂直翻转椭圆?
在本教程中,我们将学习如何使用 FabricJS 垂直翻转椭圆对象。椭圆是 FabricJS 提供的各种形状之一。为了创建椭圆,我们将必须创建一个 fabric.Ellipse 类的实例并将其添加到画布中。我们可以使用 flipY 属性垂直翻转椭圆对象。
语法
new fabric.Ellipse({ flipY: Boolean }: Object)参数
options (可选) - 此参数是一个 对象,它为我们的椭圆提供了额外的自定义选项。使用此参数,可以更改与对象的许多属性相关联的颜色、光标、描边宽度等,其中 flipY 是一个属性。
选项键
flipY - 此属性接受一个 布尔值。它允许我们垂直翻转对象。
示例 1
将 flipY 作为键传递,值为“false”
让我们来看一个示例,它向我们展示了 FabricJS 中椭圆对象的默认方向。由于我们将 flipY 属性设置为“false”,因此椭圆对象不会垂直翻转。
<!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>示例 2
将 flipY 属性作为键传递,值为“true”
在此示例中,我们有一个水平半径为 100、垂直半径为 60 且具有垂直线性渐变填充的椭圆对象。当我们将 flipY 属性应用于椭圆对象时,它会垂直翻转,因此我们看到渐变也翻转了。
<!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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP