在本教程中,我們將學習如何使用 FabricJS 鎖定橢圓的水平傾斜。正如我們可以指定畫布中橢圓對象的位置、顏色、不透明度和尺寸一樣,我們也可以指定是否要停止水平傾斜對象。這可以通過使用lockSkewingX屬性來完成。
語法
new fabric.Ellipse({ lockSkewingX : Boolean }: Object)
登錄后復制
參數
選項(可選)- 此參數是一個提供額外自定義的對象到我們的橢圓。使用此參數,可以更改與 lockSkewingX 為屬性的對象相關的顏色、光標、描邊寬度和許多其他屬性。
選項鍵
lockSkewingX – 此屬性接受布爾值值。如果我們為其指定“true”值,則對象的水平傾斜將被鎖定。
示例 1
默認行為畫布中的 Ellipse 對象
讓我們通過一個示例來了解未使用 lockSkewingX 屬性時 Ellipse 對象的默認行為。通過按住 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>Locking the horizontal skewing of Ellipse using FabricJS</h2>
<p>Select the object. Hold the "shift" and try to stretch the object (not diagonally). You can skew the object both horizontally and vertically. This is the default behavior.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 115,
top: 50,
fill: "white",
rx: 80,
ry: 50,
stroke: "black",
strokeWidth: 5,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
登錄后復制
示例 2
將 lockSkewingX 作為具有“true”值的鍵傳遞
在此示例中,我們將了解如何可以使用lockSkewingX屬性來停止Ellipse對象水平傾斜的能力。雖然我們可以垂直傾斜橢圓對象,但我們不允許水平執行相同的操作。
<!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>How to lock the horizontal skewing of Ellipse using FabricJS?</h2>
<p>Select the object; hold the "shift" key and try to stretch the object horizontally. You cannot skew the object horizontally because we have set <b>lockSkewingX</b> to True. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an ellipse instance
var ellipse = new fabric.Ellipse({
left: 115,
top: 50,
fill: "white",
rx: 80,
ry: 50,
stroke: "black",
strokeWidth: 5,
lockSkewingX: true,
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
登錄后復制
以上就是如何使用 FabricJS 鎖定 Ellipse 的水平傾斜?的詳細內容,更多請關注www.92cms.cn其它相關文章!






