在本文中,我們將學習如何使用 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 設置畫布上選擇區域的顏色?的詳細內容,更多請關注www.92cms.cn其它相關文章!






