如何使用 FabricJS 鎖定矩形的水平傾斜?
在本教程中,我們將學習如何使用 FabricJS 鎖定矩形的水平傾斜。就像我們可以在畫布上指定矩形物件的位置、顏色、不透明度和尺寸一樣,我們還可以指定是否要停止物件的水平傾斜。這可以透過使用lockSkewingX屬性來完成。
語法
new fabric.Rect({ lockSkewingX : Boolean }: Object)引數
選項(可選) - 此引數是一個物件,它為我們的矩形提供了額外的自定義。使用此引數,可以更改與物件的許多屬性相關的屬性,例如顏色、游標、筆觸寬度等,其中lockSkewingX是其中一個屬性。
選項鍵
lockSkewingX - 此屬性接受布林值。如果我們將其賦值為True,則物件的水平傾斜將被鎖定。
示例 1
畫布中矩形物件的預設行為
讓我們看一個程式碼示例來了解在不使用lockSkewingX屬性時矩形物件的預設行為。透過按住Shift鍵然後沿水平或垂直方向拖動,可以在水平和垂直兩個方向上傾斜物件。
<!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>Default behaviour of a Rectangle object in the canvas</h2>
<p>Press the shift-key and drag the object along the X or Y-axis to see that
skewing is possible in both directions.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a rectangle object
var rect = new fabric.Rect({
left: 155,
top: 90,
width: 170,
height: 70,
fill: "#6f2da8",
padding: 9,
stroke: "#b666d2",
strokeWidth: 5,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>示例 2
將lockSkewingX作為鍵並傳遞True值
在此示例中,我們將看到如何使用lockSkewingX屬性來停止矩形物件水平傾斜的能力。正如我們所看到的,儘管我們可以垂直傾斜矩形物件,但我們不允許在水平方向上執行相同的操作。
<!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>Passing lockSkewingX as key with True value</h2>
<p>Press the shift-key and try to drag the object in the horizontal direction. You will notice that skewing along the X-axis is no longer feasible.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiate a rectangle object
var rect = new fabric.Rect({
left: 155,
top: 90,
width: 170,
height: 70,
fill: "#6f2da8",
padding: 9,
stroke: "#b666d2",
strokeWidth: 5,
lockSkewingX: true,
});
// Add it to the canvas
canvas.add(rect);
</script>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP