如何使用 FabricJS 建立具有背景顏色的圓形?
在本教程中,我們將使用 FabricJS 建立一個具有**背景顏色**的圓形。圓形是 FabricJS 提供的各種形狀之一。要建立圓形,我們必須建立一個fabric.Circle類的例項並將其新增到畫布中。backgroundColor屬性允許我們將顏色賦予物件的背景。它是容器(圓形所在的區域)的顏色,形狀為正方形。
語法
new fabric.Circle({ backgroundColor: String }: Object)引數
options (可選) − 此引數是一個物件,它為我們的物件提供額外的自定義選項。使用此引數,可以更改與圓形相關的屬性,例如顏色、游標、筆劃寬度以及許多其他屬性,其中backgroundColor就是一個屬性。
選項鍵
backgroundColor − 此屬性接受一個字串,用於確定物件背景的顏色。該值可以是十六進位制值、rgba 值或我們想要的背景顏色的名稱。
示例 1
使用十六進位制值作為backgroundColor屬性的鍵
讓我們來看一個使用十六進位制顏色值將背景顏色賦予圓形物件的示例。在此示例中,我們使用了十六進位制顏色程式碼#d0db61,這是一種深卡其色。
<!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>Creating a circle with a background colour using FabricJS</h2>
<p>Notice the dark-khaki background around the circle. It appears as we have applied the <b>backgroundColor</b> property and assigned it a Hex color code. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
radius: 50,
fill: "#74c365",
stroke: "#00b7eb",
strokeWidth: 2,
backgroundColor: "#d0db61",
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
使用 rgba 值作為backgroundColor屬性的鍵
我們可以使用RGBA(紅色、藍色、綠色和 alpha)值代替十六進位制顏色程式碼。alpha 引數指定顏色的不透明度。在此示例中,我們使用了rgba 值 (255,0,0,0.7),這是不透明度為 0.7 的紅色。
<!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>Creating a circle with a background colour using FabricJS</h2>
<p>Notice the orange-red background around the circle. Here we have used the <b>backgroundColor</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 cir = new fabric.Circle({
left: 215,
top: 100,
radius: 50,
fill: "green",
stroke: "blue",
strokeWidth: 2,
backgroundColor: "rgba(255,0,0,0.7)",
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP