在本文中,我们将学习如何使用 fabricjs 设置画布上选择区域的颜色。我们可以使用selectioncolor属性调整颜色。
语法new fabric.canvas(element: htmlelement|string, { selectioncolor: string }: object)
参数元素 - 此参数是 元素本身,可以使用 document.getelementbyid() 或 元素本身的 id 派生。 fabricjs 画布将在此元素上初始化。
选项(可选) - 此参数是一个对象,它提供对我们的画布进行额外的定制。使用此参数,可以更改与画布相关的颜色、光标、边框宽度等属性以及许多其他属性,其中selectioncolor是我们可以指示选区颜色的属性。 selectioncolor的默认值为rgba(100,100,255,0.3)。
示例1将selectioncolor键传递给类
selectioncolor 属性接受一个确定所选内容颜色的字符串。我们可以使用 rgba 值,它代表:红、蓝、绿和 alpha。 alpha 参数指定颜色的不透明度。让我们看一个代码示例,了解如何使用 fabricjs 设置画布中选择区域的颜色。
<!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 color of a selection area using fabricjs</h2> <p>select an area around the object to see the color of the selection area.</p> <canvas id="canvas"></canvas> <script> // initiate a canvas instance var canvas = new fabric.canvas("canvas", { selectioncolor: "rgba(0,0,0,0.4)", }); // creating an instance of the fabric.rect class var rect = new fabric.rect({ left: 170, top: 90, width: 60, height: 80, fill: "#006400", angle: 90, }); // adding it to the canvas canvas.add(rect); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); </script></body></html>
示例 2使用颜色名称代替 rgba 值
我们还可以使用特定颜色代替 rgba 值。在此示例中,selectioncolor 属性已设置为颜色“红色”。
<!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 color of a selection area using fabricjs</h2> <p>select an area around the object to see the color of the selection area.</p> <canvas id="canvas"></canvas> <script> // initiate a canvas instance var canvas = new fabric.canvas("canvas", { selectioncolor: "red", }); // creating an instance of the fabric.rect class var rect = new fabric.rect({ left: 170, top: 90, width: 60, height: 80, fill: "#006400", angle: 90, }); // adding it to the canvas canvas.add(rect); canvas.setwidth(document.body.scrollwidth); canvas.setheight(250); </script></body></html>
以上就是如何使用 fabricjs 设置画布上选择区域的颜色?的详细内容。