如何使用 FabricJS 在縮放文字框時鎖定翻轉?
在本教程中,我們將學習如何使用 FabricJS 在縮放文字框時鎖定翻轉。就像我們可以在畫布上指定文字框物件的
語法
new fabric.Textbox(text: String, { lockScalingFlip : Boolean }: Object)
引數
text − 此引數接受一個字串,即我們希望在文字框內顯示的文字字串。
options(可選) − 此引數是一個物件,它為我們的文字框提供了額外的自定義選項。使用此引數,可以更改與物件的
選項鍵
lockScalingFlip − 此屬性接受一個布林值。如果我們為其分配“true”值,則在縮放期間將不允許物件翻轉。
示例 1
畫布中文字框物件的預設行為
讓我們看一個程式碼示例來了解當未使用 lockScalingFlip 屬性時文字框物件的預設行為。
<!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>Select a corner and drag diagonally to scale it and then flip it following the same path </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("The wisest mind has something yet to learn.", { width: 400, left: 110, top: 70, fill: "orange", strokeWidth: 2, stroke: "green", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
示例 2
將 lockScalingFlip 作為鍵傳遞,值為“true”
在此示例中,我們將看到如何透過使用 lockScalingFlip 屬性來停止文字框物件在縮放時的翻轉能力。如我們所見,
<!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 lockScalingFlip as key with "true" value</h2> <p>You will no longer be able to flip the textbox while scaling it</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("The wisest mind has something yet to learn.", { width: 400, left: 110, top: 70, fill: "orange", strokeWidth: 2, stroke: "green", textAlign: "center", lockScalingFlip: true, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
廣告