如何在FabricJS中設定自定義按鍵來啟用/停用畫布的均勻縮放?
本文將學習如何在FabricJS中設定自定義按鍵來啟用/停用均勻縮放。在FabricJS中,從物件的角拖動時,物件會按比例變換。這稱為均勻縮放。但是,我們可以使用uniScaleKey屬性來啟用/停用此行為。
語法
new fabric.Canvas(element: HTMLElement|String, { uniScaleKey: String }: Object)引數
element − 此引數是<canvas> 元素本身,可以使用Document.getElementById()或<canvas> 元素的ID來獲取。FabricJS畫布將在此元素上初始化。
options (可選) − 此引數是一個物件,它為我們的畫布提供額外的自定義選項。使用此引數,可以更改畫布相關的許多屬性,例如顏色、游標、邊框寬度以及uniScaleKey屬性。它接受一個字串值,用於確定哪個鍵切換均勻縮放。其預設值為shiftKey。
示例1
將uniScaleKey屬性的值設定為'altKey'
讓我們看一個使用自定義按鍵在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
將uniScaleKey屬性的值設定為'ctrlKey'
我們也可以將'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>
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP