如何使用 FabricJS 更改文本框的字体大小?
在本教程中,我们将了解如何使用 FabricJS 更改文本框的字体大小。我们可以自定义、拉伸或移动文本框中的文本。为了创建文本框,我们必须创建 fabric.Textbox 类的实例并将其添加到画布中。字体大小指定文本框中显示的字符的大小。我们可以使用 fontSize 属性更改字体大小。
语法
new fabric.Textbox(text: String, { fontSize: Number }: Object)参数
text − 此参数接受一个字符串,它是我们想要在文本框内显示的文本字符串。
options(可选)− 此参数是一个对象,它为我们的文本框提供额外的自定义。使用此参数可以更改与对象相关的颜色、光标、笔划宽度和许多其他属性,其中fontSize 是一个属性。
选项键
fontSize − 此属性接受一个数字,它允许我们设置文本框内文本的大小。其默认值为 40。
示例 1
文本框对象的默认外观
让我们来看一个代码示例,以了解当不使用fontSize 属性时,我们的文本框对象将如何显示。
<!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 appearance of the Textbox object</h2>
<p>You can see the default value of text in a textbox which is 40</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("Impossible is for the unwilling.", {
backgroundColor: "#e6e8fa",
width: 400,
left: 100,
top: 70,
fill: "#000060",
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>示例 2
将fontSize 属性作为键传递,并使用自定义值
在此示例中,我们将fontSize 属性作为键传递,其值为 30。这意味着我们的文本框对象现在将呈现字体大小为 30px 的文本。
<!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 fontSize property as key with a custom value</h2>
<p>You can see the font size is 30 now which is slightly smaller than the default</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("Impossible is for the unwilling.", {
backgroundColor: "#e6e8fa",
width: 400,
left: 100,
top: 70,
fill: "#000060",
fontSize: 30,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP