如何使用 FabricJS 停用文字框的可選擇性?
在本教程中,我們將學習如何使用 FabricJS 停用文字框的可選擇性。我們可以自定義、拉伸或移動文字框中編寫的文字。為了建立文字框,我們必須建立一個 `fabric.Textbox` 類的例項並將其新增到畫布中。為了修改物件,我們必須在 FabricJS 中選擇它。但是,我們可以使用 `selectable` 屬性來改變這種行為。
語法
new fabric.Textbox(text: String, { selectable: Boolean }: Object)引數
text − 此引數接受一個字串,即我們想要在文字框內顯示的文字字串。
options (可選) − 此引數是一個物件,它為我們的文字框提供額外的自定義功能。使用此引數可以更改與物件的許多屬性相關的顏色、游標、筆劃寬度等等,其中 `selectable` 就是一個屬性。
選項鍵
selectable − 此屬性接受一個布林值。當為其賦值 "false" 時,就不能選擇該物件進行修改。其預設值為 true。
示例 1
預設行為或當selectable 屬性設定為 "true" 時
讓我們來看一個程式碼示例,瞭解當預設情況下selectable 屬性設定為 True 時,物件的行為方式。當 selectable 屬性設定為 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>Default behaviour or when selectable property is set to ‘true’</h2>
<p>You can try moving the textbox around the canvas or scaling it to provethat it's selectable.</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("Every noble work is at first impossible.", {
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
將 selectable 屬性作為鍵傳遞
在這個例子中,我們將 `selectable` 屬性的值設定為 false。這意味著我們不能再選擇文字框物件進行修改。
<!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 selectable property as key</h2>
<p>You can see that the textbox is no longer selectable</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("Every noble work is at first impossible.", {
width: 400,
left: 110,
top: 70,
fill: "orange",
strokeWidth: 2,
stroke: "green",
textAlign: "center",
selectable: 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