
在本教程中,我们将学习如何使用 FabricJS 向矩形添加虚线描边。矩形是 FabricJS 提供的各种形状之一。为了创建一个矩形,我们必须创建一个 Fabric.Rect 类的实例并将其添加到画布中。 StrokeDashArray 属性允许我们为对象的笔画指定破折号图案。
语法
new fabric.Rect( { strokeDashArray: Array }: Object)参数
选项(可选) - 此参数是一个提供额外自定义的对象到我们的矩形。使用此参数,可以更改与 StrokeDashArray 为属性的对象相关的颜色、光标、描边宽度和许多其他属性。
Options Keys
StrokeDashArray - 此属性允许我们为对象的笔画指定破折号图案。我们可以通过向其传递一个数组来实现这一点。例如,如果我们传递一个值为 [2,3] 的数组,则表示 2px 破折号和 3px 间隙的破折号图案,并且无限重复此图案。
示例 1
对象描边的默认外观
让我们看一个描述矩形对象描边默认外观的代码示例。由于我们没有使用 StrokeDashArray 属性,因此没有显示破折号图案。
Default appearance of an object’s stroke
You can see that there is no dashed stroke on the rectangle
示例 2
将 StrokeDashArray 属性作为键传递
在此示例中,我们向 StrokeDashArray 属性传递一个值[9,2]的。这意味着将创建一个虚线图案,其中有一条 9 像素长的线,后面有一个 2 像素的间隙,然后再次绘制一条 9 像素长的线,依此类推。
Passing strokeDashArray property as key
You can see now a dashed stroke has been added around the rectangle










