如何使用 FabricJS 为椭圆添加虚线描边?
在本教程中,我们将学习如何使用 FabricJS 为椭圆添加**虚线描边**。椭圆是 FabricJS 提供的各种形状之一。为了创建椭圆,我们将创建一个fabric.Ellipse类的实例并将其添加到画布上。 strokeDashArray 属性允许我们为对象的描边指定虚线样式。
语法
new fabric.Ellipse( { strokeDashArray: Array }: Object)参数
options (可选) - 此参数是一个对象,它为我们的椭圆提供了额外的自定义选项。使用此参数,可以更改与对象的许多属性相关的颜色、光标、描边宽度等,其中strokeDashArray是一个属性。
选项键
strokeDashArray - 此选项是一个数组,用于定义虚线的样式。例如,如果我们传递一个值为 [2,3] 的数组,则表示 2px 的虚线和 3px 的间隙,并无限重复此样式。
示例 1
对象的描边默认外观
让我们来看一个示例,该示例描述了椭圆对象描边的默认外观。由于我们没有使用strokeDashArray属性,因此没有显示任何虚线样式。
<!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>How to add dashed stroke to an Ellipse using FabricJS?</h2>
<p>This is the default appearance. No dashed strokes here, as we have not used the <b>strokeDashArray</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: 115,
top: 50,
rx: 80,
ry: 50,
fill: "#ffdead",
stroke: "#cd853f",
strokeWidth: 7,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
将strokeDashArray 属性作为键传递
在此示例中,我们传递了strokeDashArray属性,其值为 [9,2]。这意味着将创建一条虚线样式,其中将有一条 9px 长的线,然后是一个 2px 的间隙,然后再次绘制一条 9px 长的线,依此类推。
<!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>How to add dashed stroke to an Ellipse using FabricJS?</h2>
<p>Observe the dashed strokes, 9px long lines followed by 2px gaps.</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: 115,
top: 50,
rx: 80,
ry: 50,
fill: "#ffdead",
stroke: "#cd853f",
strokeWidth: 7,
strokeDashArray: [9, 2],
});
// 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