如何使用 FabricJS 设置椭圆控制角的样式?
在本教程中,我们将学习如何使用 FabricJS 设置椭圆控制角的样式。椭圆是 FabricJS 提供的各种形状之一。为了创建椭圆,我们将创建一个 fabric.Ellipse 类的实例并将其添加到画布上。对象的控制角允许我们缩放、拉伸或更改其位置。我们可以通过多种方式自定义我们的控制角,例如为其添加特定的颜色、更改其大小等。我们可以使用 cornerStyle 属性更改样式。
语法
new fabric.Ellipse({ cornerStyle: String }: Object)参数
options (可选) − 此参数是一个 对象,它为我们的椭圆提供了额外的自定义选项。使用此参数,可以更改与对象相关的颜色、光标、描边宽度和许多其他属性,其中 cornerStyle 是一个属性。
选项键
cornerStyle − 此属性接受一个 字符串,它允许我们指定所需的控制角样式。
示例 1
控制角的默认样式
以下示例显示了控制角的默认样式。
<!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>Setting the style of controlling corners of an Ellipse using FabricJS</h2>
<p>Select the object and observe its controlling corners. This is the default appearance as we have not used the <b>cornerStyle</b> property. </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: "white",
rx: 90,
ry: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#daa520",
cornerColor: "rgb(255,20,147)",
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将 cornerStyle 作为键,值为 "circle"
我们可以通过将值指定为 "circle" 或 "rect" 来指定活动选择对象的控制角的样式或外观。将值指定为 "circle" 将使控制角显示为圆形,如下面的示例所示:
<!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>Setting the style of controlling corners of Ellipse using FabricJS</h2>
<p>Select the object and observe the shape of its controlling corners. We have set the <b>cornerStyle</b> as circle.</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: "white",
rx: 90,
ry: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#daa520",
cornerColor: "rgb(255,20,147)",
cornerStyle: "circle",
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP