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

FabricJS – 如何更改 Line 对象的 URL 字符串的格式?

王林
发布: 2023-08-30 10:13:05
转载
661 人浏览过

FabricJS – 如何更改 Line 对象的 URL 字符串的格式?

在本教程中,我们将学习如何使用 FabricJS 更改 Line 对象的 URL 字符串的格式。 Line 元素是 FabricJS 中提供的基本元素之一。它用于创建直线。由于线元素在几何上是一维的并且不包含内部,因此它们永远不会被填充。我们可以通过创建 Fabric.Line 的实例、指定线的 x 和 y 坐标并将其添加到画布来创建线对象。为了更改 Line 对象的 URL 字符串的格式,我们使用 format 属性。

语法

toDataURL({ format: String }: Object): String
登录后复制

参数

  • 选项(可选) - 此参数是一个对象,它为 Line 对象的 URL 表示形式提供额外的自定义。使用此参数可以更改高度、质量、乘数和许多其他属性,其中 format 是一个属性。

选项键

  • format - 该属性接受一个String值,它允许我们定义输出图像的格式。接受的值为“jpeg”或“png”。默认值为“png”。

不使用format属性的默认值

示例

让我们看一个代码示例,以查看使用 toDataURL 方法而不使用 format 属性时记录的输出。一旦我们从开发工具打开控制台,我们就可以看到 Line 对象的 URL 表示。我们可以复制该 URL 并将其粘贴到新选项卡的地址栏中以查看最终输出。由于我们没有指定图像的格式,因此它将根据设置的默认值“png”。

<!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 value without using the format property</h2>
   <p>
   You can open console from dev tools and see that the URL representation of the Line object has a "png" format by 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 Line object
      var line = new fabric.Line([200, 100, 100, 40], {
         stroke: "blue",
         strokeWidth: 20,
         angle: 70,
      });
      
      // Add it to the canvas
      canvas.add(line);
      
      // Using the toDataURL method
      console.log(line.toDataURL());
   </script>
</body>
</html>
登录后复制

使用 toDataURL 方法和 format 属性

示例

让我们看一个代码示例,看看当 toDataURL 方法与 format 属性一起使用时 Line 对象是什么样子。在这种情况下,我们将能够指定最终图像的格式。由于此处分配的值为“jpeg”,因此最终图像将为“jpeg”格式。

<!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>Using the toDataURL method along with format property</h2>
   <p>
   You can open console from dev tools and see that the URL representation of the Line object has a "jpeg" format 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 Line object
      var line = new fabric.Line([200, 100, 100, 40], {
         stroke: "blue",
         strokeWidth: 20,
         angle: 70,
      });
      
      // Add it to the canvas
      canvas.add(line);
      
      // Using the toDataURL method
      console.log(line.toDataURL({format: "jpeg"}));
   </script>
</body>
</html>
登录后复制

以上是FabricJS – 如何更改 Line 对象的 URL 字符串的格式?的详细内容。更多信息请关注PHP中文网其他相关文章!

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