如何使用 FabricJS 設定文字框的垂直縮放比例?
在本教程中,我們將學習如何使用 FabricJS 設定文字框的垂直縮放比例。我們可以自定義、拉伸或移動文字框中的文字。為了建立一個文字框,我們必須建立一個fabric.Textbox類的例項並將其新增到畫布上。就像我們可以指定畫布中文字框物件的 位置、顏色、不透明度和尺寸一樣,我們也可以設定文字框物件的垂直縮放比例。這可以透過使用scaleY屬性來實現。
語法
new fabric.Textbox(text: String, { scaleY : Number }: Object)
引數
text − 此引數接受一個字串,即我們希望在文字框內顯示的文字字串。
options (可選) − 此引數是一個物件,它為我們的文字框提供了額外的自定義選項。使用此引數,可以更改與物件相關的許多屬性,例如顏色、游標、描邊寬度等等,其中scaleY也是一個屬性。
選項鍵
scaleY − 此屬性接受一個數字值。分配的值決定了垂直物件縮放比例。其預設值為 1。
示例 1
不使用scaleY時的預設外觀
讓我們來看一個程式碼示例,該示例顯示在不使用scaleY屬性時文字框物件的外觀。預設情況下,文字框物件的垂直縮放比例為 1。scaleY確定沿 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 appearance when "scaleY" is not used</h2> <p>You can see that there is no resizing along the Y-axis</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("Politicians and diapers must be changed often, and for the same reason.", { backgroundColor: "#fffff0", width: 400, left: 50, top: 20, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
示例 2
將scaleY屬性作為鍵傳遞
在這個例子中,我們將scaleY屬性作為鍵傳遞,其值為 2。這意味著文字框物件在垂直方向上的縮放比例加倍。
<!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 scaleY property as key</h2> <p>You can see that the object's vertical width has doubled</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(450); // Initiate a textbox object var textbox = new fabric.Textbox("Politicians and diapers must be changed often, and for the same reason.", { backgroundColor: "#fffff0", width: 400, left: 50, top: 20, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", scaleY: 2, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
廣告