首頁  >  文章  >  web前端  >  如何設定自訂鍵以啟用/停用 FabricJS 畫布上的統一縮放?

如何設定自訂鍵以啟用/停用 FabricJS 畫布上的統一縮放?

PHPz
PHPz轉載
2023-09-07 11:57:08916瀏覽

如何设置自定义键以启用/禁用 FabricJS 画布上的统一缩放?

在本文中,我們將學習如何設定自訂鍵以在 FabricJS 中啟用/停用統一縮放。在 FabricJS 中,當從角落拖曳物件時,物件會按比例變換。這稱為均勻縮放。但是,我們可以使用 uniScaleKey 啟用/停用此行為。

語法

new fabric.Canvas(element: HTMLElement|String, { uniScaleKey: String }: Object)

參數

element - 此參數是 元素本身,可以使用Document 派生。 getElementById() 或 元素本身的 id。 FabricJS 畫布將在此元素上初始化。

選項(可選) - 此參數是一個對象,它為我們的畫布提供額外的自訂。使用這個參數,可以改變與畫布相關的顏色、遊標、邊框寬度等許多屬性,其中uniScaleKey就是一個屬性。它接受一個字串值,該值決定哪個鍵切換統一縮放。它的預設值為shiftKey。

範例1

傳遞值為「altKey」的uniScaleKey屬性

讓我們看一下用於在FabricJS 中停用統一縮放的自訂鍵的程式碼範例。在這裡,我們將 uniScaleKey 設定為“altKey”。

<!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>Setting a custom key to enable/disable uniform scaling on a canvas</h2>
   <p>Hold the <b>alt</b> key and stretch the object diagonally. The object will scale non-uniformly. </p>
   <canvas id="canvas"></canvas>
   <script>
   // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas", {
         uniformScaling: true,
         uniScaleKey: "altKey"
      });
      // Creating an instance of the fabric.Rect class
      var circle = new fabric.Circle({
         left: 215,
         top: 100,
         radius: 50,
         fill: "orange",
      });
      // Adding it to the canvas
         canvas.add(circle);
         canvas.setWidth(document.body.scrollWidth);
         canvas.setHeight(250);
   </script>
</body>
</html>

範例2

傳遞值為「ctrlKey」的uniScaleKey 屬性

我們也可以傳遞「ctrlKey」'作為uniScaleKey 屬性的值,因為它也是修飾鍵。如果為 uniScaleKey 指定了 NULL 值或不是修飾鍵的鍵,則其功能將被停用。

在此範例中,uniformScaling 已被指定為 false 值,這表示該功能已停用。一旦我們按下ctrlKey,均勻縮放就會再次啟用。

<!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>Setting a custom key to enable/disable uniform scaling on a canvas </h2>
   <p>Hold the <b>ctrl</b> key and stretch the object diagonally. It will scale uniformly. </p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas", {
         uniformScaling: false,
         uniScaleKey: "ctrlKey"
      });
      // Creating an instance of the fabric.Rect class
      var circle = new fabric.Circle({
         left: 215,
         top: 100,
         radius: 50,
         fill: "red",
      });
      // Adding it to the canvas
      canvas.add(circle);
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
   </script>
</body>
</html>

以上是如何設定自訂鍵以啟用/停用 FabricJS 畫布上的統一縮放?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除