如何使用 FabricJS 水平翻转圆形?
在本教程中,我们将学习如何使用 FabricJS 水平翻转圆形对象。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆形,我们必须创建一个fabric.Circle类的实例并将其添加到画布中。我们可以使用flipX属性水平翻转圆形对象。
语法
new fabric.Circle({ flipX: Boolean }: Object)参数
options (可选) − 此参数是一个对象,它为我们的圆形提供额外的自定义。使用此参数,可以更改与对象的许多属性相关联的属性,其中flipX就是一个属性,例如颜色、光标、笔触宽度等。
选项键
flipX − 此属性接受一个布尔值。它允许我们水平翻转对象。
示例 1
将flipX作为键传递,值为“false”
让我们来看一个示例,它向我们展示了 FabricJS 中圆形对象的默认方向。由于我们将flipX属性的值设置为“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 a Circle horizontally using FabricJS?</h2>
<p>Here the circle has not flipped horizontally as we have set <b>flipX</b> as False. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 215,
top: 50,
fill: "green",
radius: 80,
stroke: "#228b22",
strokeWidth: 2,
flipX: false
});
// Create gradient fill
circle.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(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将flipX属性作为键传递,值为“true”
在此示例中,我们有一个半径为 80px,填充为水平线性渐变的圆形对象。当我们将flipX属性应用于圆形对象时,它会水平翻转,因此我们看到渐变也翻转了。
<!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 a Circle horizontally using FabricJS?</h2>
<p>Here, notice that the circle has flipped horizontally (see the gradient) because we have set <b>flipX</b> as True. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 215,
top: 50,
fill: "green",
radius: 80,
stroke: "#228b22",
strokeWidth: 2,
flipX: true
});
// Create gradient fill
circle.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(circle);
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