如何使用 FabricJS 移动文本中单个字符的基线?

如何使用 FabricJS 移动文本中单个字符的基线?

在本教程中,我们将学习如何使用 FabricJS 移动文本中单个字符的基线。我们可以通过添加 Fabric.Text 的实例在画布上显示文本。它不仅允许我们移动、缩放和更改文本的尺寸,而且还提供了附加功能,例如文本对齐、文本装饰、行高,这些功能可以分别通过属性 textAlign、underline 和 lineHeight 获得。同样,我们也可以使用 deltaY 属性来移动单个字符的基线。

语法

new fabric.Text(text: String , { styles: { deltaY: Number}:Object }: Object)

参数

  • text - 此参数接受一个 String,这是我们要显示的文本字符串。

  • 选项(可选) - 此参数是一个对象,它为我们的文本提供额外的自定义。使用此参数可以更改与样式为属性的对象相关的颜色、光标、边框宽度和许多其他属性。

选项键

  • styles - 该属性接受一个Object值,它允许我们为单个字符添加样式。

  • deltaY - 此属性接受一个 Number 值,该值允许我们仅移动样式的基线。

示例 1

仅传递 styles 属性作为键

在此示例中,我们可以看到如何使用 styles 属性向字符添加单独的样式。正如我们在这个例子中看到的,只有第 0 个字符的 fontSize 为 55,fontWeight 为粗体,fontStyle 为“oblique”。第一级属性是行号,第二级属性是字符号。这里我们使用 0 来表示第一行和第一个字符。

<!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 styles property as key</h2>
   <p>You can see that the first character looks different now</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 text object
      var text = new fabric.Text("Add sample text here.", {
         width: 300,
         left: 50,
         top: 70,
         fill: "green",
         styles: {
            0: {
               0: {
                  fontSize: 55,
                  fontWeight: "bold",
                  fontStyle: "oblique",
               }
            }
         }
      });

      // Add it to the canvas
      canvas.add(text);
   </script>
</body>
</html>

示例 2

将 styles 属性作为键与 deltaY 属性一起传递

在此示例中,我们将了解如何使用 deltaY 属性为字符添加不同的基线。在这种情况下,由于指定了 deltaY,第一行中的第二个数字(第一个索引)与其相邻字符具有不同的基线。

<!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 styles property as key along with deltaY property</h2>
   <p> You can see that the second number in the first line has a different baseline </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 text object
      var text = new fabric.Text("Add sample text here. H2O", {
         width: 300,
         left: 50,
         top: 70,
         fill: "green",
         styles: {
            1: {
               0: {
                  fontSize: 55,
                  fontWeight: "bold",
                  fill: "red",
               },
               1: {
                  deltaY: 15,
                  fill: "blue",
               },
               2: {
                  fontSize: 55,
                  fontWeight: "bold",
                  fill: "red",
               },
            },
         },
      });

      // Add it to the canvas
      canvas.add(text);
   </script>
</body>
</html>

以上就是如何使用 FabricJS 移动文本中单个字符的基线?的详细内容,更多请关注编程之家其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


kindeditor4.x代码高亮功能默认使用的是prettify插件,prettify是Google提供的一款源代码语法高亮着色器,它提供一种简单的形式来着色HTML页面上的程序代码,实现方式如下: 首先在编辑器里面插入javascript代码: 确定后会在编辑器插入这样的代码: <pre
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代码高亮,因为SyntaxHighlighter的应用非常广泛,所以将kindeditor默认的prettify替换为SyntaxHighlighter代码高亮插件 上一篇“让kindeditor显示高亮代码”中已经
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小
JS怎么获取当前时间戳
JS如何判断对象是否为数组
JS怎么获取图片当前宽高
JS对象如何转为json格式字符串
JS怎么获取图片原始宽高
怎么在click事件中调用多个js函数
js如何往数组中添加新元素
js如何拆分字符串
JS怎么对数组内元素进行求和
JS如何判断屏幕大小
js怎么解析json数据
js如何实时获取浏览器窗口大小
原生JS实现别踩白块小游戏(五)
原生JS实现别踩白块小游戏(一)