如何使用 FabricJS 停用文字框的居中旋轉?
在本教程中,我們將學習如何使用 FabricJS 停用文字框的居中旋轉。我們可以自定義、拉伸或移動文字框中編寫的文字。為了建立文字框,我們必須建立一個 fabric.Textbox 類的例項並將其新增到畫布中。預設情況下,FabricJS 中的所有物件都使用其中心作為旋轉點。但是,我們可以使用 centeredRotation 屬性更改此行為。
語法
new fabric.Textbox(text: String, { centeredRotation: Boolean }: Object)引數
text − 此引數接受一個字串,即我們希望在文字框內顯示的文字字串。
options(可選)− 此引數是一個物件,它為我們的文字框提供了額外的自定義選項。使用此引數,可以更改與物件相關的屬性,例如顏色、游標、筆劃寬度以及許多其他屬性,其中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 Textbox in FabricJS</h2>
<p>Rotate the textbox to see the default value 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 textbox object
var textbox = new fabric.Textbox("The past does not equal the future.", {
backgroundColor: "#ffe5b4",
width: 400,
top: 70,
left: 110,
});
// Add it to the canvas
canvas.add(textbox);
</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 key with the value as "false"</h2>
<p>Rotate the textbox to see the changed center of rotation</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 textbox object
var textbox = new fabric.Textbox("The past does not equal the future.", {
backgroundColor: "#ffe5b4",
width: 400,
top: 70,
left: 110,
centeredRotation: false,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP