在本教程中,我們將學習如何使用 FabricJS 檢查 IText 對象是否已填充。 IText 類是在 FabricJS 版本 1.4 中引入的,它擴展了 Fabric.Text 并用于創建 IText 實例。 IText 實例使我們可以自由選擇、剪切、粘貼或添加新文本,而無需額外配置。還有各種支持的按鍵組合和鼠標/觸摸組合使文本具有交互性,而 Text 中未提供這些組合。
然而,基于 IText 的 Textbox 允許我們調整文本矩形的大小并自動換行。對于 IText 來說情況并非如此,因為高度不會根據換行進行調整。我們可以通過使用各種屬性來操作 IText 對象。同樣,我們可以使用 hasFill 方法檢查 IText 對象是否已填充。
語法
hasFill()
登錄后復制
示例 1
使用透明填充時使用hasFill方法
讓我們看一個代碼示例,以查看當 hasFill 方法與透明填充一起使用時記錄的輸出。如果對象具有填充顏色,則 hasFill 方法返回真值。在本例中,我們將填充顏色設置為“透明”。因此,記錄的輸出將為 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>Using hasFill method when transparent fill is used</h2>
<p>You can open console from dev tools and see that the logged output is false</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: "transparent",
fontStyle: "bold",
backgroundColor: "#f8f4ff",
stroke: "black",
}
);
// Add it to the canvas
canvas.add(itext);
// Using the hasFill method
console.log("The value is: ", itext.hasFill());
</script>
</body>
</html>
登錄后復制
示例 2
使用 hasFill 方法
讓我們看一個代碼示例,以查看使用 hasFill 方法時記錄的輸出。在這種情況下,由于 IText 對象具有填充顏色,因此將返回真值。
<!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 hasFill method</h2>
<p>You can open console from dev tools and see that the value is being displayed in the console</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 HereLorem ipsum dolor sit amet",{
width: 300,
left: 60,
top: 70,
fill: "#b666d2",
fontStyle: "bold",
backgroundColor: "#f8f4ff",
stroke: "black",
}
);
// Using the hasFill method
console.log("The value is: ", itext.hasFill());
// Add it to the canvas
canvas.add(itext);
</script>
</body>
</html>
登錄后復制
以上就是如何使用 FabricJS 檢查 IText 對象是否已填充?的詳細內容,更多請關注www.92cms.cn其它相關文章!






