Home  >  Article  >  Web Front-end  >  How to set the quality level of an IText object's URL string using FabricJS?

How to set the quality level of an IText object's URL string using FabricJS?

WBOY
WBOYforward
2023-08-24 11:33:161292browse

如何使用 FabricJS 设置 IText 对象的 URL 字符串的质量级别?

In this tutorial, we will learn how to set the quality level in the URL string of an IText object using FabricJS. The IText class was introduced in FabricJS version 1.4, which extends Fabric.Text and is used to create IText instances. IText instances give us the freedom to select, cut, paste or add new text without additional configuration. There are also various supported key combinations and mouse/touch combinations to make text interactive that are not available in Text.

However, IText-based Textbox allows us to resize the text rectangle and wrap it automatically. This is not the case for IText, as the height does not adjust based on line breaks. We can manipulate IText objects by using various properties. Likewise, we can set the quality level in the URL string of the IText object using the quality attribute.

grammar

toDataURL({ quality: Number }: Object): String

parameter

  • options (optional) - This parameter is an object that provides additional customization for the URL representation of the IText object. Use this parameter to change height, mass, format, and many other properties, of which mass is one.

Option key

  • quality - This property accepts a Number value that represents the quality level of the final output image. Acceptable values ​​are between 0 and 1, excluding 0. 0.1 represents the worst quality, and 1 represents the best quality. This property is only available for jpeg format. The default value is 1.

Example 1

Do not use quality attributes

Let's look at a code example to see the output image when not using the quality attribute. Once we open the console from the development tools, we can see the URL representation of the IText object. We can copy the URL and paste it into the address bar of a new tab to see the final output. Since we are not using the quality attribute, the default value, which is 1, will be used.

<!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>Without using the quality property</h2>
   <p>You can open console from dev tools and see the output URL. You can copy that and paste it in the address bar of a new tab to see the final image </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 shadow object
      var shadow = new fabric.Shadow({
         blur: 25,
         color: "grey",
         offsetX: 12,
         offsetY: 15,
      });

      // Initiate an itext object
      var itext = new fabric.IText(
         "Add sample text here.Lorem ipsum dolor sit amet consectetur adipiscing.",{
            width: 300,
            left: 310,
            top: 70,
            fill: "#c70039",
            backgroundColor: "#c1dfed",
            stroke: "#c70039",
            originX: "center",
            shadow: shadow,
         }
      );

      // Add it to the canvas
      canvas.add(itext);

      // Using the toDataURL method
      console.log(itext.toDataURL({ format: "jpeg" }));
   </script>
</body>
</html>

Example 2

Use quality attributes

Let's look at a code example to see what the final output image of an IText object looks like when using the quality attribute. In this case, we pass it the value 0.1. Therefore the quality of the final image will become the worst.

<!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 quality property</h2>
   <p>You can open console from dev tools and see the output URL. You can copy that and paste it in the address bar of a new tab to see the final image </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 shadow object
      var shadow = new fabric.Shadow({
         blur: 25,
         color: "grey",
         offsetX: 12,
         offsetY: 15,
      });

      // Initiate an itext object
      var itext = new fabric.IText(
         "Add sample text here.Lorem ipsum dolor sit amet consectetur adipiscing.",{
            width: 300,
            left: 310,
            top: 70,
            fill: "#c70039",
            backgroundColor: "#c1dfed",
            stroke: "#c70039",
            originX: "center",
            shadow: shadow,
         }
      );

      // Add it to the canvas
      canvas.add(itext);

      // Using the toDataURL method
      console.log(itext.toDataURL({ format: "jpeg", quality: 0.1 }));
   </script>
</body>
</html>

The above is the detailed content of How to set the quality level of an IText object's URL string using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete