在本文中,我們將學習如何設置自定義鍵以在 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 畫布上的統一縮放?的詳細內容,更多請關注www.92cms.cn其它相關文章!






