Home > Web Front-end > JS Tutorial > body text

How to get the opacity of a Text object using FabricJS?

WBOY
Release: 2023-09-21 15:37:09
forward
600 people have browsed it

如何使用 FabricJS 获取 Text 对象的不透明度?

In this tutorial, we will learn how to get the opacity of text using FabricJS. We can display text on the canvas by adding an instance of Fabric.Text. Not only does it allow us to move, scale and change the dimensions of text, but it also provides additional features such as text alignment, text decoration, line height, which are available through the properties textAlign, underline and lineHeight respectively. We can also find the opacity of an object using the getObjectOpacity method.

grammar

getObjectOpacity()
Copy after login

Example 1

Use getObjectOpacity method

Let's look at a code example to see the output logged when using the getObjectOpacity method. In this case, the default opacity of 1 will be displayed in the console.

<!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 getObjectOpacity method</h2>
   <p>You can open console from dev tools and see that the opacity value is being displayed in the console</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 sampletext here", {
         width: 300,
         fill: "green",
         fontWeight: "bold",
      });

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

      // Using getObjectOpacity method
      console.log("The opacity is", text.getObjectOpacity());
   </script>
</body>
</html>
Copy after login

Example 2

Use getObjectOpacity method and pass opacity property

Let's look at a code example to see the output logged when the getObjectOpacity method is used in conjunction with the opacity property. In this example, the text object's opacity is assigned 0.7, so the recorded output will be 0.7.

<!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 getObjectOpacity method and passing the opacity property</h2>
   <p>You can open console from dev tools and see that the opacity value is being displayed in the console</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 sampletext here", {
         width: 300,
         fill: "green",
         fontWeight: "bold",
         opacity: 0.7,
      });
      
      // Add it to the canvas
      canvas.add(text);

      // Using getObjectOpacity method
      console.log("The opacity is", text.getObjectOpacity());
   </script>
</body>
</html>
Copy after login

The above is the detailed content of How to get the opacity of a Text object using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!