如何使用 FabricJS 设置圆形控制角的大小?
在本教程中,我们将学习如何使用 FabricJS 设置圆形控制角的大小。对象的控制角允许我们缩放、拉伸或更改其位置。我们可以通过多种方式自定义控制角,例如为其添加特定的颜色、更改其大小等。我们可以使用cornerSize 属性更改大小。
语法
new fabric.Circle({ cornerSize: Number }: Object)参数
options (可选) − 此参数是一个对象,它为我们的圆形提供额外的自定义。使用此参数,可以更改与cornerSize 属性相关的对象的许多属性,例如颜色、光标、笔触宽度等。
选项键
cornerSize − 此属性接受一个数字,允许我们操作所选对象的控制角的大小。其默认值为 13。
示例 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 size of the controlling corners of a circle using FabricJS</h2>
<p>Select the object and notice the size of its controlling corners. This is the default appearance. Here we haven't used the <b>cornerSize</b> property.</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
将 cornerSize 作为键传递自定义值
在此示例中,我们将cornerSize 属性作为键传递,其值为 7。我们可以看到当圆形对象被选中时,这如何更改控制角的大小。
<!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 size of the controlling corners of a circle using FabricJS</h2>
<p>Select the object and notice the size of its controlling corners. Here we have set the <b>cornerSize</b> at 7. </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)",
cornerSize: 7
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP