如何使用 FabricJS 鎖定文字框的垂直移動?
在本教程中,我們將學習如何使用 FabricJS 鎖定文字框的垂直移動。就像我們可以在畫布上指定文字框物件的的位置、顏色、不透明度和尺寸一樣,我們還可以指定是否希望它只在 X 軸上移動。這可以透過使用 lockMovementY 屬性來實現。
語法
new fabric.Textbox(text: String, { lockMovementY: Boolean }: Object)
引數
text − 此引數接受一個 字串,表示我們希望在文字框內顯示的文字字串。
options (可選) − 此引數是一個 物件,它為我們的文字框提供了額外的自定義選項。使用此引數,可以更改與物件相關的許多屬性,例如顏色、游標、描邊寬度等等,其中 lockMovementY 就是一個屬性。
選項鍵
lockMovementY − 此屬性接受一個 布林值。如果我們為它賦值為“true”,則物件將無法在垂直方向上移動。
示例 1
畫布中文字框物件的預設行為
讓我們來看一個程式碼示例,瞭解當 lockMovementY 屬性未賦值為“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>Default behaviour of a Textbox object in the canvas</h2> <p>Drag the Textbox across the X-axis or Y-axis and observe 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 textbox object var textbox = new fabric.Textbox("If you're not confused, you're not paying attention.", { backgroundColor: "#fffff0", width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "pink", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
示例 2
將 lockMovementY 作為鍵並賦值為“true”
在本例中,我們將看到如何鎖定文字框物件的垂直移動。透過將 lockMovementY 屬性賦值為“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>Passing lockMovementY as key with "true" value</h2> <p>Drag the Textbox across the X-axis or Y-axis and observer 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 textbox object var textbox = new fabric.Textbox("If you're not confused, you're not paying attention.", { backgroundColor: "#fffff0", width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "pink", textAlign: "center", lockMovementY: true, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP