如何使用 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