如何使用FabricJS設定文字框的編輯模式?
在本教程中,我們將學習如何使用FabricJS啟用文字框的編輯模式。就像我們可以在畫布上指定文字框物件的位 置、顏色、不透明度和尺寸一樣,我們也可以編輯文字框中的文字。這可以透過使用`editable`屬性來啟用或停用。
語法
new fabric.Textbox(text: String, { editable : Boolean }: Object)
引數
text − 此引數接受一個字串,即我們想要在文字框內顯示的文字字串。
options (可選) − 此引數是一個物件,它為我們的文字框提供額外的自定義選項。使用此引數,可以更改與物件相關的許多屬性,其中`editable`就是一個屬性,例如顏色、游標、描邊寬度等等。
選項鍵
editable: 此屬性接受一個布林值。如果我們將其賦值為“true”,則允許我們編輯文字框內的文字。其預設值為true。
示例1
畫布中文字框物件的預設行為
讓我們看一個程式碼示例,瞭解當不使用`editable`屬性時文字框物件的預設行為。預設情況下,允許我們編輯文字框內的文字。
<!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>You can double click on the Textbox and edit the text</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("It doesn't matter how slow you go, as long as you don't stop.", { width: 400, left: 110, top: 70, fill: "orange", stroke: "green", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
示例2
將editable作為鍵,值設定為“false”
在這個例子中,我們將看到如何使用`editable`屬性使文字框內的文字不可編輯。正如我們所看到的,雖然我們可以選擇文字框物件,但我們無法再編輯其中的文字。
<!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 editable as key with "false" value</h2> <p>You can double-click on the textbox to see that the text is not editable</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("It doesn't matter how slow you go, as long as you don't stop.", { width: 400, left: 110, top: 70, fill: "orange", stroke: "green", textAlign: "center", editable: false, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP