在本文中,我們將看到如何使用FabricJS設置畫布文本框的文本對齊方式。畫布意味著編寫的文本框是可移動的,可旋轉的,可調整大小的,並且可以拉伸。在本文中,我們將設置文本對齊方式。
方法:為了實現這一點,我們將使用一個名為FabricJS的JavaScript庫。使用CDN導入庫後,我們將在body標簽中創建一個canvas塊,其中將包含我們的文本框。之後,我們將初始化FabricJS提供的canvas和textbox的實例,並使用textAlign屬性更改文本的對齊方式,並在文本框上呈現畫布,如下麵的示例所示。
用法:
fabric.Textbox('text', { textAlign:string });
參數:該函數接受上麵提到和下麵描述的一個參數。
- textAlign:它指定文本對齊方式的值。
例:本示例使用FabricJS設置畫布文本框的文本對齊,如下所示。
HTML
<!DOCTYPE html>
<html>
<head>
<!-- Adding the FabricJS library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</script>
</head>
<body>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h3>
Fabric.js | Textbox textAlign Property
</h3>
<canvas id="canvas" width="600" height="300"
style="border:1px solid #000000">
</canvas>
<script>
// Initiate a Canvas instance
var canvas = new fabric.Canvas("canvas");
// Create a new Textbox instance
var text = new fabric.Textbox(
'A Computer Science Portal', {
width:500,
textAlign:"center"
});
// Render the Textbox in canvas
canvas.add(text);
</script>
</body>
</html>
輸出:
相關用法
- HTML canvas textAlign用法及代碼示例
- HTML Style textAlign用法及代碼示例
- p5.js textAlign()用法及代碼示例
- Fabric.js Textbox borderOpacityWhenMoving屬性用法及代碼示例
- Fabric.js Textbox borderScaleFactor屬性用法及代碼示例
- Fabric.js Textbox centeredRotation屬性用法及代碼示例
- Fabric.js Textbox centeredScaling屬性用法及代碼示例
- Fabric.js Textbox charSpacing屬性用法及代碼示例
- Fabric.js Textbox cornerColor屬性用法及代碼示例
- Fabric.js Textbox cornerSize屬性用法及代碼示例
- Fabric.js Textbox fontFamily屬性用法及代碼示例
- Fabric.js Textbox fontSize屬性用法及代碼示例
- Fabric.js Textbox fontStyle屬性用法及代碼示例
- Fabric.js Textbox fontWeight屬性用法及代碼示例
- Fabric.js Textbox hasBorders屬性用法及代碼示例
- Fabric.js Textbox hasControls屬性用法及代碼示例
- Fabric.js Textbox hoverCursor屬性用法及代碼示例
- Fabric.js Textbox height屬性用法及代碼示例
- Fabric.js Textbox includeDefaultValues屬性用法及代碼示例
- Fabric.js Textbox inverted屬性用法及代碼示例
- Fabric.js Textbox isEditing屬性用法及代碼示例
- Fabric.js Textbox editingBorderColor屬性用法及代碼示例
- Fabric.js Textbox fill屬性用法及代碼示例
- Fabric.js Textbox flipX屬性用法及代碼示例
注:本文由純淨天空篩選整理自blalverma92大神的英文原創作品 Fabric.js Textbox textAlign Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。