如何使用 FabricJS 设置圆形控制角的样式?
在本教程中,我们将学习如何使用 FabricJS 设置圆形控制角的样式。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆形,我们将必须创建一个fabric.Circle类的实例并将其添加到画布。
对象的控制角允许我们缩放、拉伸或改变其位置。我们可以通过多种方式自定义我们的控制角,例如向其添加特定颜色、更改其大小等。我们可以使用 cornerStyle 属性更改样式。
语法
new fabric.Circle({ 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 circle using FabricJS</h2>
<p>Select the object and notice the shape and size of its controlling corners. This is the default style of the controlling corners.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
fill: "white",
radius: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#daa520",
cornerColor: "rgb(255,20,147)"
});
// Adding it to the canvas
canvas.add(cir);
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 circle using FabricJS</h2>
<p>Select the object and notice the shape of its controlling corners. Here we have used the <b>cornerStyle</b> property and assigned it the value "circle". </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
fill: "white",
radius: 50,
stroke: "#c154c1",
strokeWidth: 5,
borderColor: "#daa520",
cornerColor: "rgb(255,20,147)",
cornerStyle: "circle"
});
// Adding it to the canvas
canvas.add(cir);
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