在本教程中,我們將學(xué)習(xí)如何使用 FabricJS 設(shè)置橢圓選區(qū)的背景顏色。橢圓形是 FabricJS 提供的各種形狀之一。為了創(chuàng)建一個(gè)橢圓,我們必須創(chuàng)建一個(gè) Fabric.Ellipse 類的實(shí)例并將其添加到畫布中。當(dāng)主動(dòng)選擇對(duì)象時(shí),我們可以更改對(duì)象的尺寸、旋轉(zhuǎn)它或操縱它。我們可以使用 selectionBackgroundColor 屬性更改橢圓選區(qū)的背景顏色。
語(yǔ)法
new fabric.Ellipse({ selectionBackgroundColor : String }: Object)
登錄后復(fù)制
參數(shù)
選項(xiàng)(可選)- 此參數(shù)是一個(gè)對(duì)象 為我們的橢圓提供額外的定制。使用此參數(shù),可以更改與 selectionBackgroundColor 為屬性的對(duì)象相關(guān)的顏色、光標(biāo)、描邊寬度和許多其他屬性。
選項(xiàng)鍵
selectionBackgroundColor – 此屬性接受字符串 確定選區(qū)的背景顏色。
示例 1
selectionBackgroundColor 屬性未使用
讓我們舉個(gè)例子來(lái)了解當(dāng) selectionBackgroundColor 屬性未使用時(shí)選擇內(nèi)容的顯示方式。從這個(gè)例子中我們可以看到,選擇區(qū)域或?qū)ο蠛竺娴膮^(qū)域沒(méi)有顏色。
<!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>
登錄后復(fù)制
示例 2
將 selectionBackgroundColor 屬性作為鍵傳遞
在此示例中,我們將一個(gè)值分配給 selectionBackgroundColor 屬性。在本例中,我們將“darkBlue”顏色傳遞給它,因此選擇區(qū)域看起來(lái)是深藍(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>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>
登錄后復(fù)制
以上就是如何使用FabricJS設(shè)置橢圓選區(qū)的背景顏色?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!






