Home  >  Article  >  Web Front-end  >  FabricJS - How to move a Line object one step down in the draw object stack?

FabricJS - How to move a Line object one step down in the draw object stack?

WBOY
WBOYforward
2023-09-10 14:01:111265browse

FabricJS – 如何将 Line 对象在绘制对象堆栈中向下移动一步?

In this tutorial, we will learn how to move a Line object one step down the draw object stack using FabricJS. Line element is one of the basic elements provided in FabricJS. It is used to create straight lines. Since line elements are geometrically one-dimensional and contain no interiors, they are never filled. We can create a line object by creating a fabric.Line instance, specifying the x and y coordinates of the line and adding it to the canvas. To move the Line object one step down the draw object stack, we use the sendBackwards method.

Syntax

 sendBackwards(intersecting: Boolean): fabric.Object 

Parameters

  • Intersect - This parameter accepts Boolean valuesWhen specified as a "true" value, the object will be sent behind the next lower intersecting object. If the value is "false", it normally sends the object after the next object on the stack. This parameter is optional.

Using the sendBackwards method

Example

Let’s look at a code example to see how to use the sendBackwards method time output. sendBackwards method moves the object one step down in the draw object stack. In this example, line2 is sent after line1 using the sendBackwards 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 sendBackwards method</h2>
   <p>
      You can see that line2 (red) has been moved down 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 sendBackwards method
      line2.sendBackwards();
   </script>
</body>
</html>

Use the sendBackwards method and enable three objects and enable the intersection key

Example

In this example, we use three line objects, line1, line2 and line3. Although they have been added to the canvas in numerical order, line3 is clearly behind line1. This is because we are using the sendBackwards method with intersection key enabled, which sends line3 to its next lower intersection object (i.e. line1) later. p>

<!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 sendBackwards method with three objects and intersection key enabled</h2>
   <p>
      You can see that the green line now lies behind the blue line which is line number 1
   </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([500, 70, 400, 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 sendBackwards method
      line3.sendBackwards(true);
   </script>
</body>
</html>

The above is the detailed content of FabricJS - How to move a Line object one step down in the draw object stack?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete