如何使用 FabricJS 為圓形新增描邊?
在本教程中,我們將學習如何使用 FabricJS 為圓形新增**描邊**。圓形是 FabricJS 提供的各種形狀之一。為了建立圓形,我們將建立一個fabric.Circle類的例項並將其新增到畫布上。我們的圓形物件可以透過多種方式進行自定義,例如更改其尺寸、新增背景顏色或更改圍繞物件繪製的線條的顏色。我們可以使用stroke屬性來實現這一點。
語法
new fabric.Circle({ stroke : String }: Object)
引數
options (可選) − 此引數是一個物件,它為我們的圓形提供額外的自定義。使用此引數,可以更改與物件相關的屬性,例如顏色、游標、描邊寬度以及許多其他屬性,其中stroke是一個屬性。
選項鍵
stroke − 此屬性接受一個字串,並確定該物件邊框的顏色。
示例 1
使用十六進位制值作為stroke鍵傳遞
讓我們看一個示例,瞭解當使用stroke屬性時我們的圓形物件是如何顯示的。十六進位制顏色程式碼以“#”開頭,後面跟著一個六位數字,表示一種顏色。在本例中,我們使用了“#ff4500”,它是一種橙紅色。
<!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 stroke to a circle using FabricJS</h2> <p>Notice the orange-red outline around the circle. It appears as we have applied the <b>stroke</b> property and assigned it a hexadecimal color code. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); var circle = new fabric.Circle({ left: 50, top: 90, radius: 50, fill: "#4169e1", stroke: "#ff4500", strokeWidth: 5 }); canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
示例 2
將'rgba'值傳遞給stroke屬性
在本例中,我們將瞭解如何將 rgba 值賦給 stroke 屬性。我們可以使用RGBA值(而不是十六進位制顏色程式碼),它代表:紅色、藍色、綠色和 alpha。alpha 引數指定顏色的不透明度。在本例中,我們使用了rgba 值 (255,69,0,0.5),它是一種不透明度為 0.5 的橙紅色。
<!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 stroke to a circle using FabricJS</h2> <p>Notice the outline around the circle. Here we have applied the <b>stroke</b> property and assigned it an 'rgba' value. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); var circle = new fabric.Circle({ left: 50, top: 90, radius: 50, fill: "#4169e1", stroke: "rgba(255,69,0,0.5)", strokeWidth: 5 }); canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
廣告