在本教程中,我們將學(xué)習(xí)如何使用 FabricJS 鎖定 Line 的垂直移動(dòng)。 Line 元素是 FabricJS 中提供的基本元素之一。它用于創(chuàng)建直線。由于線元素在幾何上是一維的并且不包含內(nèi)部,因此它們永遠(yuǎn)不會(huì)被填充。我們可以通過(guò)創(chuàng)建 fabric.Line 實(shí)例來(lái)創(chuàng)建線條對(duì)象,指定線條的 x 和 y 坐標(biāo)并將其添加到畫(huà)布中。我們還可以指定是否希望線對(duì)象僅在 X 軸上移動(dòng)。這可以通過(guò)使用 lockMovementY 屬性來(lái)完成。
語(yǔ)法
new fabric.Line(points: Array, { lockMovementY: Boolean }: Object)
登錄后復(fù)制
參數(shù)
points – 此參數(shù)接受一個(gè)點(diǎn)的Array,它確定 (x1, y1) 和 (x2, y2)值,分別是線的起點(diǎn)和終點(diǎn)的 x 軸坐標(biāo)和 y 軸坐標(biāo)。
選項(xiàng)(可選) – 此參數(shù)是一個(gè)對(duì)象,它為我們的對(duì)象提供額外的自定義。使用此參數(shù),可以更改與 lockMovementY 為屬性的對(duì)象相關(guān)的原點(diǎn)、筆觸寬度和許多其他屬性。
選項(xiàng)鍵
lockMovementY – 此屬性接受布爾值。如果我們給它指定一個(gè)“true”值,那么該對(duì)象將不再能夠在垂直方向上移動(dòng)。
畫(huà)布中 Line 對(duì)象的默認(rèn)行為
示例
讓我們看一個(gè)代碼示例,了解當(dāng) lockMovementY 屬性未分配“true”值時(shí),如何在 X 軸或 Y 軸上自由移動(dòng)線條對(duì)象。
<!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 Line object in the canvas</h2> <p> Drag the line object across the x-axis and y-axis to see that movement is allowed 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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, }); // Add it to the canvas canvas.add(line); </script> </body> </html>
登錄后復(fù)制
將 lockMovementY 作為具有“true”值的鍵傳遞
示例
在此示例中,我們將了解如何鎖定線條對(duì)象的垂直移動(dòng)。通過(guò)為 lockMovementY 屬性分配“true”值,我們基本上停止了垂直方向的移動(dòng)。
<!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 lockMovementY as key with ‘true’ value</h2> <p> Drag the line object across the x-axis and y-axis to see that movement is no longer allowed in the vertical direction </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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, lockMovementY: true, }); // Add it to the canvas canvas.add(line); </script> </body> </html>
登錄后復(fù)制
以上就是如何使用FabricJS鎖定Line的垂直移動(dòng)?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!