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

How to make a circle invisible using FabricJS?

WBOY
Release: 2023-08-26 22:49:11
forward
729 people have browsed it

How to make a circle invisible using FabricJS?

In this tutorial, we will learn how to make a circle invisible using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we must create an instance of the fabric.Circle class and add it to the canvas. Our circle object can be customized in many ways, such as changing its size, adding a background color, or making it visible or invisible. We can achieve this by using the visible attribute.

Syntax

new fabric.Circle( { visible: Boolean }: Object)
Copy after login

Parameters

  • options (optional) - This parameter is a Object, provides the option for additional customization of our circle. Using this parameter, you can change the properties related to this object, such as color, cursor, stroke width and many other properties, where visible is a property.

Option Key

  • visible - This property accepts a boolean value, allowing us to Objects are rendered onto the canvas. Its default value is True.

Example 1

Pass visible attribute as key and give it a 'true' value

Let's see one code to understand what happens when we pass the visible property a True value. By specifying the value as 'true' we ensure that our circle object is rendered to the canvas. This is also the default behavior of FabricJS.

<!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>Making a circle invisible using FabricJS</h2>
      <p>Here, the circle is visible because we have set <b>visible</b> to True. This is the default behavior. So, even if we don&#39;t apply the <b>visible</b> property, it will be set to True, by defalt. </p>
      <canvas id="canvas"></canvas>

      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "#adff2f",
            radius: 50,
            stroke: "#228b22",
            visible: true
         });

         // Adding it to the canvas
         canvas.add(cir);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>
Copy after login

Example 2

Pass the visible attribute as key and set to 'false' value

In this example we will pass the visible attribute Passed as key and set to False value. Setting this to a false value will ensure that our circle object is not rendered to the canvas. It doesn't just make the object 'invisible', it gets rid of it entirely. It can be used to remove an object from the canvas without deleting its code.

<!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>Making a circle invisible using FabricJS</h2>
      <p>Notice that the circle is invisible here, as we have set <b>visible</b> to False. </p>
      <canvas id="canvas"></canvas>
   
      <script>
         // Initiate a canvas instance
         var canvas = new fabric.Canvas("canvas");
         var cir = new fabric.Circle({
            left: 215,
            top: 100,
            fill: "#adff2f",
            radius: 50,
            stroke: "#228b22",
            visible: false
         });
   
         // Adding it to the canvas
         canvas.add(cir);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
      </script>
   </body>
</html>
Copy after login

The above is the detailed content of How to make a circle invisible 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