在本教程中,我們將學習如何使用 FabricJS 設置橢圓選區的背景顏色。橢圓形是 FabricJS 提供的各種形狀之一。為了創建一個橢圓,我們必須創建一個 Fabric.Ellipse 類的實例并將其添加到畫布中。當主動選擇對象時,我們可以更改對象的尺寸、旋轉它或操縱它。我們可以使用 selectionBackgroundColor 屬性更改橢圓選區的背景顏色。
語法
new fabric.Ellipse({ selectionBackgroundColor : String }: Object)
登錄后復制
參數
選項(可選)- 此參數是一個對象 為我們的橢圓提供額外的定制。使用此參數,可以更改與 selectionBackgroundColor 為屬性的對象相關的顏色、光標、描邊寬度和許多其他屬性。
選項鍵
selectionBackgroundColor – 此屬性接受字符串 確定選區的背景顏色。
示例 1
selectionBackgroundColor 屬性未使用
讓我們舉個例子來了解當 selectionBackgroundColor 屬性未使用時選擇內容的顯示方式。從這個例子中我們可以看到,選擇區域或對象后面的區域沒有顏色。
<!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>How to set the background color of selection of Ellipse using FabricJS?</h2> <p>Select the object and you will observe that the selection background has no color. Here we have not applied the <b>selectionBackgroundColor</b> property. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Initiate an ellipse instance var ellipse = new fabric.Ellipse({ left: 115, top: 50, rx: 80, ry: 50, fill: "#ff1493", }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
登錄后復制
示例 2
將 selectionBackgroundColor 屬性作為鍵傳遞
在此示例中,我們將一個值分配給 selectionBackgroundColor 屬性。在本例中,我們將“darkBlue”顏色傳遞給它,因此選擇區域看起來是深藍色的。
<!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>How to set the background color of selection of Ellipse using FabricJS?</h2> <p>Select the object and you will observe that the background of the selection appears dark blue. This is because we have set the <b>selectionBackgroundColor</b> as dark blue. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Initiate an ellipse instance var ellipse = new fabric.Ellipse({ left: 115, top: 50, rx: 80, ry: 50, fill: "#ff1493", selectionBackgroundColor: "darkBlue", }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
登錄后復制
以上就是如何使用FabricJS設置橢圓選區的背景顏色?的詳細內容,更多請關注www.92cms.cn其它相關文章!