
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Move Line Object to Specific Index in Fabric.js Stack
In this tutorial, we are going to learn about how to move a Line object to a specified index in the stack of drawn objects using FabricJS. A Line element is one of the basic elements provided in FabricJS. It is used for creating straight lines. Because line elements are geometrically one-dimensional and do not contain an interior, they are never filled. We can create a line object by creating an instance of fabric.Line, specifying the x and y coordinates of the line and adding it to the canvas. In order to move a Line object to a specified index in the stack of drawn objects, we use the moveTo method.
Syntax
moveTo(index: Number): fabric.Object
Parameters
index ? This parameter accepts a Number value which specifies the level where we want to move our object in the stack of drawn objects.
Using moveTo method
Example
Let's see a code example to see the output when the moveTo method is used. The moveTo method moves an object to a specified level in the stack of drawn objects. In this case line2 is sent to the 0th index, on using the moveTo method.
<!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 moveTo method</h2> <p> You can see that line2 (red) has been moved to the 0th index in the stack of drawn objects </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 Line object var line1 = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); // Initiate another Line object var line2 = new fabric.Line([200, 70, 70, 40], { stroke: "red", strokeWidth: 20, }); // Add both to the canvas canvas.add(line1); canvas.add(line2); // Using moveTo method line2.moveTo(0); </script> </body> </html>
Using moveTo method with three objects
Example
In this example, we have used three line objects namely line1, line2 and line3. Although they have been added to the canvas according to their numerical order, line3 clearly lies behind line2, in the 1st index. This is because we have used the moveTo method, which sends line3 to the 1st index while line1 and line2 occupy the 0th and 2nd index in the stack of drawn objects.
<!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 moveTo method with three objects</h2> <p> You can see that line3 (green) lies in the 1st index which is middle position in stack </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 Line object var line1 = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); // Initiate another Line object var line2 = new fabric.Line([200, 70, 70, 40], { stroke: "red", strokeWidth: 20, }); // Initiate another Line object var line3 = new fabric.Line([200, 30, 30, 90], { stroke: "green", strokeWidth: 20, }); // Add them all to the canvas canvas.add(line1); canvas.add(line2); canvas.add(line3); // Using moveTo method line3.moveTo(1); </script> </body> </html>