如何使用 FabricJS 設定圓形的最小允許縮放值?
在本教程中,我們將學習如何使用 FabricJS 設定圓形的最小允許縮放比例。圓形是 FabricJS 提供的各種形狀之一。為了建立圓形,我們必須建立一個 `fabric.Circle` 類的例項並將其新增到畫布上。我們可以透過新增填充顏色、消除邊框甚至更改其尺寸來自定義圓形物件。同樣,我們也可以使用 `minScaleLimit` 屬性設定其最小允許縮放比例。
語法
new fabric.Circle({ minScaleLimit : Number }: Object)引數
**`options` (可選)** − 此引數是一個 `Object`,它為我們的圓形提供了額外的自定義選項。使用此引數可以更改與物件相關的許多屬性,例如顏色、游標、邊框寬度等等,其中 `minScaleLimit` 就是一個屬性。
選項鍵
**`minScaleLimit`** − 此屬性接受一個 **`Number`** 作為值,允許我們控制圓形的最小允許縮放比例。
示例 1
圓形物件的預設外觀
讓我們來看一段程式碼,看看在不使用 `minScaleLimit` 屬性時圓形物件是什麼樣的。在這種情況下,我們將能夠自由地縮放物件,因為沒有設定最小限制。
<!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 minimum allowed scale value of circle using FabricJS</h2>
<p>Select the object and scale it down by dragging one of its controlling corners. Here you can scale down the object freely since there is no minimum limit set.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
top: 50,
radius: 50,
fill: "#ff1493"
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
將 `minScaleLimit` 屬性作為鍵並設定自定義值
在這個例子中,我們將看到為 `minScaleLimit` 屬性賦值如何改變畫布中圓形物件的最小允許縮放比例。這裡我們使用了 0.8 作為值,這意味著我們將無法將物件縮小到小於 64px 的半徑(計算方法為:**半徑 * 限制** (0.8 * 80 = 64px))。
<!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 minimum allowed scale value of circle using FabricJS</h2>
<p>Select the object and try to scale it down by dragging one of its controlling corners. You cannot scale down the object freely, as we have set <b>minScaleLimit</b> at 0.8. So, the minimum scale of the circle must be at least 80% of the original radius, beyond which you cannot scale it down any further. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
top: 50,
radius: 80,
fill: "#ff1493",
minScaleLimit: 0.8
});
// Adding it to the canvas
canvas.add(circle);
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