如何使用 FabricJS 停用三角形的中心旋轉?
在本教程中,我們將學習如何使用 FabricJS 停用三角形的中心旋轉。三角形是 FabricJS 提供的各種形狀之一。為了建立三角形,我們必須建立一個 fabric.Triangle 類的例項並將其新增到畫布中。
預設情況下,FabricJS 中的所有物件都使用其中心作為旋轉點。但是,我們可以使用 centeredRotation 屬性更改此行為。
語法
new fabric.Triangle({ centeredRotation: Boolean }: Object)引數
選項(可選) - 此引數是一個 物件,它為我們的三角形提供額外的自定義。使用此引數,可以更改與物件的 centeredRotation 屬性相關的顏色、游標、筆劃寬度以及許多其他屬性。
選項鍵
centeredRotation - 此屬性接受一個 布林值,並允許我們控制物件在透過控制元件旋轉時是否使用中心點作為其變換原點。其預設值為 true。
示例 1
FabricJS 中三角形旋轉的預設行為
讓我們看一個程式碼示例,它描述了三角形物件的預設行為。由於 centeredRotation 屬性預設設定為 true,因此三角形物件使用其中心作為旋轉點。
<!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>Default behaviour of rotation of Triangle in FabricJS</h2>
<p>Rotate the triangle to see the default behaviour of centeredRotation</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 60,
width: 100,
height: 70,
fill: "#deb887",
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>示例 2
將 centeredRotation 鍵的值傳遞為“false”
現在我們已經看到了預設行為,讓我們看一個程式碼示例來了解當 centeredRotation 屬性被賦予 False 值時會發生什麼。
<!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>Passing centeredRotation as key with the value "false"</h2>
<p>Rotate the triangle and notice that now its center of rotation has changed. The triangle rotates around one of its corners instead of its center.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 60,
width: 100,
height: 70,
fill: "#deb887",
centeredRotation: false,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP