在本教程中,我們將使用 FabricJS 創(chuàng)建一個(gè)具有邊框顏色的矩形。矩形是 FabricJS 提供的各種形狀之一。為了創(chuàng)建一個(gè)矩形,我們必須創(chuàng)建一個(gè)fabric.Rect類的實(shí)例并將其添加到畫布中。
由于FabricJS非常靈活,我們可以以我們喜歡的任何方式自定義我們的矩形對(duì)象。 FabricJS 提供的屬性之一是 borderColor,它允許我們?cè)趯?duì)象處于活動(dòng)狀態(tài)時(shí)操縱邊框的顏色。
語法
new fabric.Rect({ borderColor: String }: Object)
登錄后復(fù)制
參數(shù)
選項(xiàng)(可選) – 此參數(shù)是一個(gè)提供額外自定義的對(duì)象到我們的矩形。使用此參數(shù),可以更改與 borderColor 為屬性的對(duì)象相關(guān)的顏色、光標(biāo)、描邊寬度和許多其他屬性等屬性。
選項(xiàng)鍵
borderColor – 此屬性接受指定顏色的字符串選擇矩形對(duì)象時(shí)的邊框。其默認(rèn)值為 rgb(178,204,255)。
示例 1
使用字符串值傳遞 borderColour 鍵
讓我們看一個(gè)代碼示例,了解如何為 borderColor 屬性賦值。我們已將值“blue”分配給 borderColor 鍵,這有助于在選擇矩形對(duì)象時(shí)創(chuàng)建藍(lán)色邊框。
<!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>Passing borderColour key with a String value</h2> <p>Select the rectangle to see the border colour</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a rectangle object var rect = new fabric.Rect({ left: 125, top: 90, width: 170, height: 70, fill: "#cf1020", borderColor: "blue", padding: 9, }); // Add it to the canvas canvas.add(rect); </script> </body> </html>
登錄后復(fù)制
示例 2
將 RGBA 值傳遞給 borderColor 鍵
而不是將簡單的顏色名稱作為字符串傳遞,我們還可以使用 RGBA 值,其分量指定紅色、綠色、藍(lán)色和 Alpha 的數(shù)量,其中 alpha 表示不透明度。在此示例中,我們使用了 rgb(128,0,128),它是紫色的 rgb 值。
<!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>Passing an RGBA value to the borderColor key</h2> <p>Select the rectangle to see the border colour</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a rectangle object var rect = new fabric.Rect({ left: 125, top: 90, width: 170, height: 70, fill: "#cf1020", borderColor: "rgb(128,0,128)", padding: 9, }); // Add it to the canvas canvas.add(rect); </script> </body> </html>
登錄后復(fù)制
以上就是如何使用 FabricJS 創(chuàng)建帶有邊框顏色的矩形?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!