在本教程中,我們將學習如何使用 FabricJS 獲取文本的不透明度。我們可以通過添加 Fabric.Text 的實例在畫布上顯示文本。它不僅允許我們移動、縮放和更改文本的尺寸,而且還提供了附加功能,例如文本對齊、文本裝飾、行高,這些功能可以分別通過屬性 textAlign、underline 和 lineHeight 獲得。我們還可以使用 getObjectOpacity 方法找到對象的不透明度。
語法
getObjectOpacity()
登錄后復制
示例 1
使用 getObjectOpacity 方法
讓我們看一個代碼示例,看看使用 getObjectOpacity 方法時記錄的輸出。在這種情況下,控制臺中將顯示默認的不透明度,即 1。
<!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 getObjectOpacity method</h2>
<p>You can open console from dev tools and see that the opacity 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 a text object
var text = new fabric.Text("Add sampletext here", {
width: 300,
fill: "green",
fontWeight: "bold",
});
// Add it to the canvas
canvas.add(text);
// Using getObjectOpacity method
console.log("The opacity is", text.getObjectOpacity());
</script>
</body>
</html>
登錄后復制
示例 2
使用 getObjectOpacity 方法并傳遞 opacity 屬性
讓我們看一個代碼示例,以查看 getObjectOpacity 方法與 opacity 屬性結合使用時記錄的輸出。在本例中,文本對象的不透明度被指定為 0.7,因此記錄的輸出將為 0.7。
<!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 getObjectOpacity method and passing the opacity property</h2>
<p>You can open console from dev tools and see that the opacity 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 a text object
var text = new fabric.Text("Add sampletext here", {
width: 300,
fill: "green",
fontWeight: "bold",
opacity: 0.7,
});
// Add it to the canvas
canvas.add(text);
// Using getObjectOpacity method
console.log("The opacity is", text.getObjectOpacity());
</script>
</body>
</html>
登錄后復制
以上就是如何使用 FabricJS 獲取 Text 對象的不透明度?的詳細內容,更多請關注www.92cms.cn其它相關文章!






