- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to move an object to the top of the stack of drawn objects in IText using FabricJS?
In this tutorial, we are going to learn about how to move an object to the top of the stack of drawn objects in IText using FabricJS. The IText class was introduced in FabricJS version 1.4, extends fabric.Text and is used to create IText instances. An IText instance gives us the freedom to select, cut, paste or add new text without additional configurations. There are also various supported key combinations and mouse/touch combinations which make text interactive which are not provided in Text.
Textbox, however, which is based on IText allows us to resize the text rectangle and wraps lines automatically. This is not true for IText as height is not adjusted based on the wrapping of lines. We can manipulate our IText object by using various properties. Likewise, we can move an object to the top of the stack of drawn objects by using the bringToFront method.
Syntax
bringToFront(): fabric.Object
Example 1
Without using the bringToFront method
Let’s see a code example to see how the IText objects behave when the bringToFront method is not used. In this case, we have initialized two IText objects namely itext1 and itext2. You can notice that the second text is covering the first text, hiding it behind.
<!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 bringToFront method</h2> <p> You can see that the first itext object is hidden behind the second itext object </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 an itext object var itext1 = new fabric.IText("First itext object.", { width: 300, left: 110, top: 70, fill: "#b666d2", fontStyle: "bold", backgroundColor: "#f8f4ff", }); // Initiate another itext object var itext2 = new fabric.IText("Second itext object.", { width: 300, left: 130, top: 90, fill: "white", backgroundColor: "#c8a2c8", }); // Add it to the canvas canvas.add(itext1); canvas.add(itext2); </script> </body> </html>
Example 2
Using the bringToFront method
Let’s see a code example to see how the IText objects behave when the bringToFront method is used. In this case, our itext1 object will now be at the top of the stack and hence it will no longer be hidden behind itext2.
<!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 bringToFront method</h2> <p> You can see that the first itext object is no longer hidden behind the second itext object </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 an itext object var itext1 = new fabric.IText("First itext object.", { width: 300, left: 110, top: 70, fill: "#b666d2", fontStyle: "bold", backgroundColor: "#f8f4ff", }); // Initiate another itext object var itext2 = new fabric.IText("Second itext object.", { width: 300, left: 130, top: 90, fill: "white", backgroundColor: "#c8a2c8", }); // Add it to the canvas canvas.add(itext1); canvas.add(itext2); // Using the bringToFront method itext1.bringToFront(); </script> </body> </html>
- Related Articles
- FabricJS – How to move a Line object to the top of the stack of drawn objects?
- FabricJS – How to move a Line object to the bottom of the stack of drawn objects?
- FabricJS – How to move a Line object one step down in the stack of drawn objects?
- FabricJS – How to move a Line object one step up in the stack of drawn objects?
- FabricJS – How to move a Line object to a specific index in the stack of drawn objects?
- How to straighten an IText object using FabricJS?
- How to change the format of the URL string of IText object using FabricJS?
- How to remove current object transform in the URL string of IText object using FabricJS?
- How to remove current object shadow in the URL string of IText object using FabricJS?
- How to set the quality level in the URL string of IText object using FabricJS?
- How to check if the IText object has fill using FabricJS?
- How to check if the IText object has stroke using FabricJS?
- How to disable the selectability of IText using FabricJS?
- How to center an object horizontally with respect to current viewport of canvas in IText using FabricJS?
- How to center an object vertically with respect to current viewport of canvas in IText using FabricJS?
