如何使用 FabricJS 建立一個在移動物體時顯示等待游標的文字框?
在本文中,我們將學習如何使用 FabricJS 建立一個在移動物體時顯示等待游標的文字框。“等待”是可用於 FabricJS 畫布的原生游標樣式之一。FabricJS 提供各種型別的游標,例如預設、全部滾動、十字準星、列調整大小、行調整大小等,這些游標都在底層重用了原生游標。`moveCursor` 屬性設定在畫布中移動物件時游標的樣式。
語法
new fabric.Textbox(text: String, { moveCursor: String }: Object)引數
text − 此引數接受一個字串,即我們希望在文字框中顯示的文字字串。
options (可選) − 此引數是一個物件,它為我們的文字框提供額外的自定義選項。使用此引數可以更改與物件相關的許多屬性,包括`moveCursor`屬性,例如顏色、游標、描邊寬度等。
選項鍵
moveCursor − 此屬性接受一個字串,允許我們設定在畫布上移動此文字框物件時的預設游標值。該值決定了在移動畫布物件時使用的游標型別。
示例 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>Default cursor value when object is moved around the canvas</h2>
<p>You can move around the textbox object around to see that the default cursor style is “move”</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("Add Sample Text Here", {
backgroundColor: "#fffff0",
width: 300,
left: 110,
top: 70,
fill: "#e1a95f",
strokeWidth: 2,
stroke: "#a40000",
textAlign: "center",
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>示例 2
將 moveCursor 屬性作為鍵值對傳入
在這個例子中,我們將`moveCursor`鍵作為值為“wait”傳遞給文字框類。這將確保當我們在畫布中移動物件時,游標值為“等待”。這在下面的程式碼示例中進行了說明:
<!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 the moveCursor property as key with a value</h2>
<p>You can move around the textbox object around to see that the cursor style is “wait”</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("Add Sample Text Here", {
backgroundColor: "#fffff0",
width: 300,
left: 110,
top: 70,
fill: "#e1a95f",
strokeWidth: 2,
stroke: "#a40000",
textAlign: "center",
moveCursor: "wait",
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP