Home > Web Front-end > JS Tutorial > FabricJS - Programmatically implement object copying on polygons?

FabricJS - Programmatically implement object copying on polygons?

WBOY
Release: 2023-08-24 11:05:07
forward
1362 people have browsed it

FabricJS – 在多边形上以编程方式实现对象复制?

We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized as any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties such as angle, opacity, etc. In order to implement object copying programmatically, we need to use the clone method to implement duplication control

grammar

clone( callback: Object, propertiesToInclude: Array)
Copy after login

parameter

  • Callback (optional) - This parameter is the callback function called through the clone.

  • propertiesToInclude (optional) - This parameter includes any additional properties we wish to include in the cloned canvas instance. This must be in the form of an array.

Example 1: Programmatic object copying on Polygon

Let's look at a code example to understand how to implement object copying on polygons. First, we start a polygon object and its respective point array and add it to the clipboard. We also have a button that when clicked triggers the duplicate() function. duplicate() will get the object from the clipboard and clone the copied object using the offset and add it to the canvas using canvas.add.

<!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>Implementing object duplication programmatically on Polygon</h2>
   <p>
      You can click on Duplicate button to see object duplication in action
   </p>
   <button onclick="duplicate()" style="padding: 3px">Duplicate</button>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating duplicate() function which pastes
      
      // the object to canvas, adds offset and
      
      // makes the object active
      function duplicate() {
         _clipboard.clone(function (clonedObj) {
            canvas.discardActiveObject();
            clonedObj.set({
               left: clonedObj.left + 10,
               top: clonedObj.top + 10,
               evented: true,
            });
            canvas.add(clonedObj);
            _clipboard.top += 10;
            _clipboard.left += 10;
            canvas.setActiveObject(clonedObj);
            canvas.requestRenderAll();
         });
      }
      
      // Initiating a points array
      var points = [
         { x: 30, y: 50 },
         { x: 70, y: 50 },
         { x: 0, y: 0 },
         { x: 70, y: 0 },
      ];
      
      // Initiating a polygon object
      var polygon = new fabric.Polygon(points, {
         left: 100,
         top: 40,
         fill: "#1e90ff",
         strokeWidth: 4,
         stroke: "green",
         scaleX: 2,
         scaleY: 2,
      });
      
      // Clone the object to clipboard
      polygon.clone(function (cloned) {
         _clipboard = cloned;
      });
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body>
</html>
Copy after login

Example 2: Programmatic object copying on a Circle

Let’s look at a code example to see how to programmatically implement object copying on a circle using FabricJS. In this case we started with a circle instead of a polygon object and followed the same process. Therefore, a copy of the circle will be created.

<!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>Implementing object duplication programmatically on Circle</h2>
   <p>
      You can click on Duplicate button to see object duplication in action
   </p>
   <button onclick="duplicate()" style="padding: 3px">Duplicate</button>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating duplicate() function which pastes
      // the object to canvas, adds offset and
      // makes the object active
      function duplicate() {
         _clipboard.clone(function (clonedObj) {
            canvas.discardActiveObject();
            clonedObj.set({
               left: clonedObj.left + 10,
               top: clonedObj.top + 10,
               evented: true,
            });
            canvas.add(clonedObj);
            _clipboard.top += 10;
            _clipboard.left += 10;
            canvas.setActiveObject(clonedObj);
            canvas.requestRenderAll();
         });
      }
      
      // Initiating a circle object
      var circle = new fabric.Circle({
         left: 100,
         top: 40, 
         fill: "#1e90ff",
         radius: 40,
         strokeWidth: 4,
         stroke: "green",
         scaleX: 2,
         scaleY: 2,
      });
      
      // Clone the object to clipboard
      circle.clone(function (cloned) {
         _clipboard = cloned;
      });
      
      // Adding it to the canvas
      canvas.add(circle);
   </script>
</body>
</html> 
Copy after login

in conclusion

In this tutorial, we use two simple examples to demonstrate how to programmatically implement object copying using FabricJS.

The above is the detailed content of FabricJS - Programmatically implement object copying on polygons?. 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