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
clone( callback: Object, propertiesToInclude: Array)
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.
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>
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>
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!