如何使用 FabricJS 建立帶有背景色的橢圓形?
在本教程中,我們將學習如何使用 FabricJs 建立帶有背景色的橢圓形。橢圓形是 FabricJS 提供的各種形狀之一。為了建立橢圓形,我們必須建立一個 fabric.Ellipse 類的例項並將其新增到畫布上。backgroundColor 屬性允許我們為物件的背景分配顏色。它是橢圓形所在的容器的顏色,對於橢圓形來說,形狀是矩形。
語法
new fabric.Ellipse({ 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>How to create an Ellipse with a background color using FabricJS?</h2>
<p>Observe the dark khaki color which we have applied as the background color.</p>
<canvas id="canvas" width="700" height="300" style="margin-left: 2px"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an Ellipse instance
var ellipse = new fabric.Ellipse({
left: 180,
top: 100,
rx: 90,
ry: 50,
fill: "#74c365",
stroke: "#00b7eb",
strokeWidth: 2,
backgroundColor: "#d0db61"
});
// Adding it to the canvas
canvas.add(ellipse);
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>How to create an Ellipse with a background color using FabricJS?</h2>
<p>Observe the background color which we have applied using an "rgba" value.</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: 180,
top: 100,
rx: 90,
ry: 50,
fill: "green",
stroke: "blue",
strokeWidth: 2,
backgroundColor: "rgba(255,0,0,0.7)",
});
// Adding it to the canvas
canvas.add(ellipse);
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