如何使用 FabricJS 设置圆形描边的宽度?
在本教程中,我们将学习如何使用 FabricJS 为圆形添加虚线描边。圆形是 FabricJS 提供的各种形状之一。为了创建一个圆形,我们将必须创建一个fabric.Circle类的实例并将其添加到画布中。strokeWidth属性允许我们指定对象描边的宽度。
语法
new fabric.Circle( { strokeWidth: Number }: Object)参数
options(可选) - 此参数是一个Object,它为我们的圆形提供额外的自定义选项。使用此参数,可以更改与对象相关的属性,例如颜色、光标、描边宽度以及许多其他属性,其中strokeWidth就是一个属性。
选项键
strokeWidth - 此属性接受一个Number值,允许我们指定对象的描边宽度。其默认值为 1。
示例 1
对象的描边默认外观
让我们来看一段代码,它描述了圆形对象的描边默认外观。由于我们没有使用strokeWidth属性,因此渲染的是默认宽度。
<!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 width of stroke of circle using FabricJS</h2>
<p>Notice the outline border of the circle. This is the default thickness of outline. Here we have not used the <b>strokeWidth</b> property, but by default, it is set to 1. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 40,
fill: "#adff2f",
radius: 100,
stroke: "#228b22",
//strokeWidth: 1
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将strokeWidth属性作为键传递
在此示例中,我们传递了值为 5 的strokeWidth属性。这将确保我们的圆形对象以宽度为 5 像素的描边进行渲染。
<!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 width of stroke of circle using FabricJS</h2>
<p>Notice the outline border of the circle. Here we have used the <b>strokeWidth</b> property and assigned it a value of 5.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 40,
fill: "#adff2f",
radius: 100,
stroke: "#228b22",
strokeWidth: 5
});
// 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