如何使用 FabricJS 鎖定圓形的水平移動?
在本教程中,我們將學習如何使用 FabricJS 鎖定圓形的水平移動。就像我們可以指定畫布中圓形物件的 位置、顏色、不透明度和尺寸一樣,我們也可以指定是否只想讓它在 Y 軸上移動。這可以透過使用 `lockMovementX` 屬性來實現。
語法
new fabric.Circle({ lockMovementX: Boolean }: Object)引數
options (可選) − 此引數是一個物件,它為我們的圓形提供了額外的自定義選項。使用此引數,可以更改與 `lockMovementX` 屬性相關的物件的許多屬性,例如顏色、游標、描邊寬度等。
選項鍵
lockMovementX − 此屬性接受一個布林值。如果我們將其賦值為“true”,則該物件將無法再在水平方向上移動。
示例 1
畫布中圓形物件的預設行為
讓我們來看一段程式碼,瞭解當 `lockMovementX` 屬性未賦值為“true”時,如何自由地在 X 軸或 Y 軸上移動圓形物件。
<!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 movement of a circle using FabricJS?</h2>
<p>Here you can select the circle and move it freely, as we have not applied the <b>lockMovementX</b> property. This is the default behavior. By default, <b>lockMovementX</b> is set to False. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
top: 50,
fill: "white",
radius: 50,
stroke: "black",
strokeWidth: 5
});
// Adding it to the canvas
canvas.add(circle);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>示例 2
將 lockMovementX 作為鍵傳遞,值為“true”
在此示例中,我們將看到如何鎖定圓形物件的水平移動。透過將 `lockMovementX` 屬性賦值為“true”,我們實際上就停止了水平方向的移動。
<!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 movement of circle using FabricJS?</h2>
<p>Select the object and try to move it horizontally. You can't do that as we have restricted the horizontal movement by setting <b>lockMovementX</b> to True. You can however move the circle in vertical direction. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var circle = new fabric.Circle({
left: 115,
top: 50,
fill: "white",
radius: 50,
stroke: "black",
strokeWidth: 5,
lockMovementX: true
});
// 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