如何使用 FabricJS 從左側設定圓形的位置?
在本教程中,我們將學習如何使用 FabricJS 從左側設定圓形的位置。圓形是 FabricJS 提供的各種形狀之一。為了建立圓形,我們必須建立 fabric.Circle 類的例項並將其新增到畫布中。我們可以透過更改其位置、不透明度、描邊以及尺寸來操作圓形物件。可以透過使用 left 屬性更改其左側的位置。
語法
new fabric.Circle( { left: Number }: Object)引數
options (可選) - 此引數是一個物件,它為我們的圓形提供了額外的自定義選項。使用此引數,可以更改與物件相關的屬性,例如顏色、游標、描邊寬度以及許多其他屬性,其中 left 是一個屬性。
選項鍵
left - 此屬性接受一個數字,用於設定物件左側的位置。該值決定了物件將放置在左側多遠。
示例 1
圓形物件的預設放置
讓我們看一個程式碼示例來了解當圓形物件的位置未更改時,其在畫布中的預設放置。
<!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 position of Circle from left using FabricJS</h2>
<p>This is the default placement of the circle. Here we have not used the <b>left</b> property. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
fill: "white",
radius: 100,
stroke: "yellow",
strokeWidth: 3
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
將 left 屬性作為鍵傳遞
在此示例中,我們使用自定義值分配了 left 屬性。由於它接受數字,因此必須為其分配一個數值,該數值將表示其左側的位置。
<!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 position of Circle from left using FabricJS</h2>
<p>Here we have used the <b>left</b> property and assigned it a custom value to set the position of the circle from left. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
fill: "white",
radius: 100,
stroke: "yellow",
strokeWidth: 3,
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP