如何使用 FabricJS 禁用文本框的可选择性?
在本教程中,我们将学习如何使用 FabricJS 禁用文本框的可选择性。我们可以自定义、拉伸或移动文本框中编写的文本。为了创建文本框,我们必须创建一个 `fabric.Textbox` 类的实例并将其添加到画布中。为了修改对象,我们必须在 FabricJS 中选择它。但是,我们可以使用 `selectable` 属性来改变这种行为。
语法
new fabric.Textbox(text: String, { selectable: Boolean }: Object)参数
text − 此参数接受一个字符串,即我们想要在文本框内显示的文本字符串。
options (可选) − 此参数是一个对象,它为我们的文本框提供额外的自定义功能。使用此参数可以更改与对象的许多属性相关的颜色、光标、笔划宽度等等,其中 `selectable` 就是一个属性。
选项键
selectable − 此属性接受一个布尔值。当为其赋值 "false" 时,就不能选择该对象进行修改。其默认值为 true。
示例 1
默认行为或当selectable 属性设置为 "true" 时
让我们来看一个代码示例,了解当默认情况下selectable 属性设置为 True 时,对象的行为方式。当 selectable 属性设置为 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 or when selectable property is set to ‘true’</h2>
<p>You can try moving the textbox around the canvas or scaling it to provethat it's selectable.</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("Every noble work is at first impossible.", {
width: 400,
left: 110,
top: 70,
fill: "orange",
strokeWidth: 2,
stroke: "green",
textAlign: "center",
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>示例 2
将 selectable 属性作为键传递
在这个例子中,我们将 `selectable` 属性的值设置为 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 selectable property as key</h2>
<p>You can see that the textbox is no longer selectable</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("Every noble work is at first impossible.", {
width: 400,
left: 110,
top: 70,
fill: "orange",
strokeWidth: 2,
stroke: "green",
textAlign: "center",
selectable: false,
});
// 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