如何使用 FabricJS 禁用 Textbox 的居中缩放?

在本教程中,我们将学习如何使用 FabricJS 禁用 Textbox 的居中缩放。我们可以自定义、拉伸或移动文本框中写入的文本。为了创建文本框,我们必须创建 Fabric.Textbox 类的实例并将其添加到画布中。通过控件进行缩放时,为 centeredScaling 属性分配一个真值,使用中心作为对象的变换原点。
语法
new fabric.Textbox(text: String, { centeredScaling: Boolean }: Object)参数
text − 此参数接受一个String,它是我们要使用的文本字符串。想要在我们的文本框中显示。
选项(可选) - 此参数是一个对象,它提供了额外的自定义我们的文本框。使用此参数,可以更改与 centeredScaling 属性相关的对象的颜色、光标、描边宽度等属性。
< /ul>centeredScaling - 此属性接受布尔值并允许我们控制对象是否应该是否使用其中心作为变换原点。
Options Keys
示例 1
传递 centeredScaling 作为键,并且为其分配“true”值
让我们看一个代码示例,了解启用 centeredScaling 属性时文本框对象的行为。当我们放大对象时,变换的原点是文本框的中心。
<!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 centeredScaling as key and assigning a "true" value to it</h2>
<p>Try scaling the textbox to see that centered scaling has been enabled</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("Success is the child of audacity.", {
backgroundColor: "#ffe5b4",
width: 400,
top: 70,
left: 110,
centeredScaling: true,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>示例 2
禁用 centeredScaling 属性
我们可以通过为其指定“false”值来禁用 centeredScaling 属性。这将不再使用文本框的中心作为变换的中心。这是一个代码示例来演示 -
<!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>Disabling centeredScaling property</h2>
<p>Try scaling the textbox to see that centered scaling has been disabled</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("Success is the child of audacity.", {
backgroundColor: "#ffe5b4",
width: 400,
top: 70,
left: 110,
centeredScaling: false,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
javascript