如何使用 FabricJS 垂直翻转三角形?
在本教程中,我们将学习如何使用 FabricJS 垂直翻转三角形对象。三角形是 FabricJS 提供的各种形状之一。为了创建一个三角形,我们将必须创建一个 fabric.Triangle 类的实例并将其添加到画布中。我们可以使用 flipY 属性垂直翻转三角形对象。
语法
new fabric.Triangle({ flipY: Boolean }: Object)参数
选项 (可选) − 此参数是一个对象,它为我们的三角形提供了额外的自定义。使用此参数,可以更改与对象的 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>Passing flipY as key with a 'false' value</h2>
<p>You can see that the triangle object has not been flipped vertically.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a triangle object
var triangle = new fabric.Triangle({
left: 75,
top: 45,
width: 180,
height: 109,
stroke: "#e3f988",
strokeWidth: 5,
flipY: false,
});
// Create gradient fill
triangle.set(
"fill",
new fabric.Gradient({
type: "linear",
coords: { x1: 0, y1: 0, x2: 0, y2: 100 },
colorStops: [
{ offset: 0, color: "#545a2c" },
{ offset: 1, color: "#6495ed" },
],
})
);
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>示例 2
将 flipY 属性作为键并传递 'true' 值
在此示例中,我们有一个宽度为 180px,高度为 109px,并填充垂直线性渐变的三角形对象。当我们将 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>Passing the flipY property as key with a 'true' value</h2>
<p>You can see that the triangle object has been flipped vertically</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a triangle object
var triangle = new fabric.Triangle({
left: 75,
top: 45,
width: 180,
height: 109,
stroke: "#e3f988",
strokeWidth: 5,
flipY: true,
});
// Create gradient fill
triangle.set(
"fill",
new fabric.Gradient({
type: "linear",
coords: { x1: 0, y1: 0, x2: 0, y2: 100 },
colorStops: [
{ offset: 0, color:"#545a2c" },
{ offset: 1, color: "#6495ed" },
],
})
);
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP