如何使用FabricJS以程式設計方式實現複製貼上?


我們可以透過建立fabric.Polygon的例項來建立一個多邊形物件。多邊形物件可以由任何由一組連線的直線段組成的封閉形狀來表示。由於它是FabricJS的基本元素之一,我們也可以透過應用角度、不透明度等屬性來輕鬆自定義它。為了以程式設計方式實現複製貼上,我們需要使用clone方法。

語法

clone( callback: Object, propertiesToInclude: Array)

引數

  • 回撥函式 (可選)− 此引數是一個回撥函式,它使用克隆物件進行呼叫。

  • 要包含的屬性 (可選) − 此引數包含我們希望包含在克隆畫布例項中的任何其他屬性。這必須採用陣列的形式。

示例 1:在多邊形上以程式設計方式實現複製貼上

讓我們來看一個程式碼示例,瞭解如何在一個多邊形上以程式設計方式實現複製貼上。我們需要克隆我們要複製的內容並將其新增到剪貼簿中,以便稍後貼上。為此,我們在Copy()函式中使用了clone方法,該方法將克隆當前選定的物件並將其儲存到剪貼簿中。此外,我們建立了一個Paste()函式,它將使用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 copy paste programmatically on Polygon</h2>
   <p>
      You can select the object, click on copy and then click on paste button to see object duplication in action
   </p>
   <button onclick="Copy()" style="padding: 3px">copy</button>
   <button onclick="Paste()" style="padding: 3px">paste</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 Copy function which copies
      
      // the object to clipboard
      function Copy() {
         canvas.getActiveObject().clone(function (cloned) {
            _clipboard = cloned;
         });
      }
      
      // Initiating Paste function which pastes
      // the object to canvas, adds offset and 
      // makes the object active
      function Paste() {
         _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,
      });
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body>
</html> 

示例 2:在圓形上以程式設計方式實現複製貼上

讓我們來看一個程式碼示例,瞭解如何使用FabricJS在圓形上以程式設計方式實現複製貼上。在本例中,我們初始化了一個半徑為40的圓形而不是多邊形物件,並將其新增到畫布中。同樣,Copy()Paste()函式可以用於多邊形以外的各種物件。

<!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 copy paste programmatically on Circle</h2>
   <p>
      You can select the object, click on copy and then click on paste button to see object duplication in action
   </p>
   <button onclick="Copy()" style="padding: 3px">copy</button>
   <button onclick="Paste()" style="padding: 3px">paste</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 Copy function which copies
      
      // the object to clipboard
      function Copy() {
         canvas.getActiveObject().clone(function (cloned) {
            _clipboard = cloned;
         });
      }
      
      // Initiating Paste function which pastes
      // the object to canvas, adds offset and
      // makes the object active
      function Paste() {
         _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,
      });
      
      // Adding it to the canvas
      canvas.add(circle);
   </script>
</body>
</html>

結論

在本教程中,我們使用了兩個示例來演示如何使用FabricJS以程式設計方式實現複製貼上。

更新於:2023年1月2日

瀏覽量:703

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告