您好,欢迎访问一九零五行业门户网

如何使用 FabricJS 将破折号添加到画布上选择区域的边框?

在本文中,我们将学习如何使用 fabricjs 将破折号添加到画布上选择区域的边框。我们可以通过使用 selectiondasharray 属性来实现这一点。它允许我们将选择区域的边框设为虚线。
语法new fabric.canvas(element: htmlelement|string, { selectiondasharray: array }: object)
参数元素 - 此参数是 元素本身,可以使用 document.getelementbyid() 或 元素本身的 id 派生。 fabricjs 画布将在此元素上初始化。
选项(可选) - 此参数是一个对象,它提供对我们的画布进行额外的定制。使用这个参数可以改变画布相关的颜色、光标、边框宽度等很多属性,其中selectiondasharray就是一个属性。它接受一个数组,该数组确定我们想要的破折号图案。
示例 1将 selectiondasharray 作为键传递给类
selectiondasharray 允许我们将选择区域的边框设为虚线。定义破折号图案的方法是指定数组中破折号的长度。在下面的示例中,我们采用了 [7,6] 数组。这意味着,将会有一条 7px 长的线,后面跟着一个 6px 的间隙,依此类推。
<!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>adding dashes to the border of a selection area on a canvas</h2> <p>select an area around the object. the border of the selection area would have dashed lines.</p> <canvas id="canvas"></canvas> <script> // initiate a canvas instance var canvas = new fabric.canvas("canvas", { selectiondasharray: [7, 6], selectionbordercolor: "red" }); // creating an instance of the fabric.rect class var circle = new fabric.circle({ left: 200, top: 100, radius: 40, fill: "blue", }); // adding it to the canvas canvas.add(circle); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); </script></body></html>
示例2将selectiondasharray与selectionlinewidth和selectionbordercolor结合使用
selectiondasharray属性可以通过多种方式使用。一种方法是将其与selectionlinewidth和selectionbordercolor结合使用,它们分别指定选区边框的宽度和选区边框的颜色。
<!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>adding dashes to the border of a selection area on a canvas</h2> <p>select an area around the object and observe the outline of the selection area. </p> <canvas id="canvas"></canvas> <script> // initiate a canvas instance var canvas = new fabric.canvas("canvas", { selectiondasharray: [13, 16], selectionlinewidth: 5, selectionbordercolor: "green", }); // creating an instance of the fabric.rect class var circle = new fabric.circle({ left: 200, top: 100, radius: 40, fill: "blue", }); // adding it to the canvas canvas.add(circle); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); </script></body></html>
以上就是如何使用 fabricjs 将破折号添加到画布上选择区域的边框?的详细内容。
其它类似信息

推荐信息