如何使用 FabricJS 設定文字框選中區域的背景顏色?
在本教程中,我們將學習如何使用 FabricJS 設定文字框選中區域的背景顏色。我們可以自定義、拉伸或移動文字框中的文字。為了建立一個文字框,我們必須建立一個 fabric.Textbox 類的例項並將其新增到畫布中。當物件被選中時,我們可以更改其尺寸、旋轉或操作它。我們可以使用 selectionBackgroundColor 屬性更改文字框選中區域的背景顏色。
語法
new fabric.Textbox(text: String, { selectionBackgroundColor : String}: Object)
引數
text − 此引數接受一個字串,即我們希望在文字框中顯示的文字字串。
options (可選) − 此引數是一個物件,它為我們的文字框提供額外的自定義選項。使用此引數,可以更改許多與物件相關的屬性,其中 selectionBackgroundColor 就是一個屬性,例如顏色、游標、筆劃寬度等等。
選項鍵
selectionBackgroundColor − 此屬性接受一個字串值。分配的值將決定選中區域的背景顏色。
示例 1
不使用selectionBackgroundColor 屬性時的預設顏色
讓我們來看一個程式碼示例,瞭解當不使用selectionBackgroundColor 屬性時選擇區域的外觀。從這個例子中我們可以看到,選擇區域或物件後面的區域沒有顏色。
<!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 colour when selectionBackgroundColor property is not used</h2> <p>You can select the textbox to see that the selection area has no colour</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("Everyone smiles in the same language.", { width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
示例 2
將selectionBackgroundColor 屬性作為鍵傳遞
在這個例子中,我們為selectionBackgroundColor 屬性賦值。在這裡,我們傳遞了十六進位制值“#e0ffff”,這是一種淺青色,因此選擇區域顯示為這種顏色。
<!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 selectionBackgroundColor property as key</h2> <p>You can select the textbox to see that the selection area now has a light cyan colour</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("Everyone smiles in the same language.", { width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", selectionBackgroundColor: "#e0ffff", }); // 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