首页 > web前端 > js教程 > 正文

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

PHPz
发布: 2023-08-23 18:01:02
转载
1211 人浏览过

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

在本教程中,我们将学习如何使用 FabricJS 禁用 Textbox 的居中缩放。我们可以自定义、拉伸或移动文本框中写入的文本。为了创建文本框,我们必须创建 Fabric.Textbox 类的实例并将其添加到画布中。通过控件进行缩放时,为 centeredScaling 属性分配一个真值,使用中心作为对象的变换原点。

语法

new fabric.Textbox(text: String, { centeredScaling: Boolean }: Object)
登录后复制

参数

  • text − 此参数接受一个String,它是我们要使用的文本字符串。想要在我们的文本框中显示。

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

  • < /ul>

    Options Keys

    • centeredScaling - 此属性接受布尔值并允许我们控制对象是否应该是否使用其中心作为变换原点。

    示例 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>
    登录后复制

    以上是如何使用 FabricJS 禁用 Textbox 的居中缩放?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!