在本教程中,我們將學(xué)習(xí)如何使用 FabricJS 獲取 IText 中字符的完整樣式聲明。 IText 類是在 FabricJS 版本 1.4 中引入的,它擴(kuò)展了 Fabric.Text 并用于創(chuàng)建 IText 實(shí)例。 IText 實(shí)例使我們可以自由選擇、剪切、粘貼或添加新文本,而無需額外配置。還有各種支持的按鍵組合和鼠標(biāo)/觸摸組合使文本具有交互性,而 Text 中未提供這些組合。
然而,基于 IText 的 Textbox 允許我們調(diào)整文本矩形的大小并自動(dòng)換行。對于 IText 來說情況并非如此,因?yàn)楦叨炔粫?huì)根據(jù)換行進(jìn)行調(diào)整。我們可以通過使用各種屬性來操作 IText 對象。同樣,我們可以使用 getCompleteStyleDeclaration 方法獲取字符的完整樣式聲明。
語法
getCompleteStyleDeclaraction(lineIndex: Number, charIndex: Number): Object
登錄后復(fù)制
參數(shù)
lineIndex – 此參數(shù)接受一個(gè) Number ,它指定所需字符的行號。
charIndex – 此參數(shù)接受一個(gè)數(shù)字,表示該行上字符的位置。
示例 1
使用 getCompleteStyleDeclaration 方法
讓我們看一個(gè)代碼示例,看看使用 getCompleteStyleDeclaration 方法時(shí) IText 對象是什么樣子。在這種情況下,我們將返回第 0 行第二個(gè)字符的完整樣式聲明。該字符已被分配淺黃色文本背景色。
<!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>Using the getCompleteStyleDeclaration method</h2>
<p>You can open console from dev tools and see the style declaration for 2nd character of the first line</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 an itext object
var itext = new fabric.IText(
"Add sample text here.Lorem ipsum dolor sit ametconsectetur adipiscing elit.",{
width: 300,
left: 60,
top: 70,
fill: "red",
styles: {
0: {
1: {
textBackgroundColor: "rgba(253,255,214,0.9)",
},
},
},
}
);
// Add it to the canvas
canvas.add(itext);
// Using getCompleteStyleDeclaration method
console.log(
"The style object is as follows: ",
itext.getCompleteStyleDeclaration(0, 1)
);
</script>
</body>
</html>
登錄后復(fù)制
示例 2
使用getCompleteStyleDeclaration方法進(jìn)行比較
讓我們看一個(gè)代碼示例,用于比較兩個(gè)不同行中同一索引處的兩個(gè)字符的樣式聲明。在本例中,我們選擇了第 1 行和第 2 行中的第二個(gè)字符,因此它們已使用不同的文本背景顏色突出顯示。由于我們?yōu)檫@兩個(gè)字符指定了不同的填充顏色、textBackgroundColor 和 fontSize,這些值將反映在我們的控制臺(tái)中,我們將能夠比較這些更改。
<!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>Using the getCompleteStyleDeclaration method for comparison</h2>
<p>You can open console from dev tools and see the style declaration for both lines</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 an itext object
var itext = new fabric.IText(
"Add sample text here.Lorem ipsum dolor sit amet",{
width: 300,
left: 60,
top: 70,
fill: "red",
styles: {
0: {
1: {
textBackgroundColor: "rgba(130,111,201,0.6)",
fontSize: 30,
fill: "black",
},
},
1: {
1: {
textBackgroundColor: "rgba(52,235,189,0.5)",
fontSize: 90,
fill: "green",
},
},
},
}
);
// Add it to the canvas
canvas.add(itext);
// Using getCompleteStyleDeclaration method
console.log(
"The style object for 2nd character of 1st line is as follows: ",
itext.getCompleteStyleDeclaration(0, 1)
);
console.log(
"The style object for 2nd character of 2nd line is as follows: ",
itext.getCompleteStyleDeclaration(1, 1)
);
</script>
</body>
</html>
登錄后復(fù)制
以上就是如何使用FabricJS獲取IText中字符的完整樣式聲明?的詳細(xì)內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!






