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.
new fabric.Circle( { visible: Boolean }: Object)
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.
visible - This property accepts a boolean value, allowing us to Objects are rendered onto the canvas. Its default value is True.
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'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>
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>
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!