在本文中,我們將看到如何使用FabricJS向文本畫布添加筆觸偏移。畫布意味著書寫的文本是可移動的,可旋轉的,可調整大小的,並且可以拉伸。但是在本文中,我們將添加筆觸偏移量。此外,文本本身不能像文本框一樣進行編輯。
為了使其成為可能,我們將使用一個名為FabricJS的JavaScript庫。使用CDN導入庫後,我們將在body標簽中創建一個canvas塊,其中將包含我們的文本。此後,我們將初始化FabricJS提供的Canvas和Text實例,並使用stroke屬性創建一個筆觸,並進一步使用strokeDashOffset屬性添加筆觸偏移並在文本上渲染Canvas,如以下示例所示。
用法:
fabric.text( text:string, strokeDashOffset:number, stroke:string );
參數:此函數接受上述和以下描述的三個參數:
- text:此參數指定文本。
- strokeDashOffset:此參數定義筆劃的偏移量。
- stroke:此參數定義筆觸顏色。
例:本示例使用FabricJS將筆畫偏移添加到文本畫布,如下所示。
<!DOCTYPE html>
<html>
<head>
<title>
Fabric.js | Text strokeDashOffset Property
</title>
<!-- Loading the FabricJS library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</script>
</head>
<body>
<center>
<h1 style="color:green;">GeeksforGeeks</h1>
<b>
Add stroke offset to canvas-like
text with JavaScript
</b>
<canvas id="canvas" width="600" height="200"
style="border:1px solid #000000;">
</canvas>
<script>
// Create a new instace of Canvas
var canvas = new fabric.Canvas("canvas");
// Create a new Text instance
var text = new fabric.Text('GeeksforGeeks', {
strokeDashOffset:10,
stroke:'red'
});
// Render the text on Canvas
canvas.add(text);
</script>
</center>
</body>
</html>
輸出:
相關用法
- Fabric.js Triangle strokeDashOffset屬性用法及代碼示例
- CSS text-align用法及代碼示例
- CSS text-decoration用法及代碼示例
- CSS text-decoration-color用法及代碼示例
- CSS text-decoration-line用法及代碼示例
- CSS text-justify用法及代碼示例
- CSS text-align-last用法及代碼示例
- CSS text-transform屬性用法及代碼示例
- CSS text-shadow用法及代碼示例
- CSS text-indent用法及代碼示例
- CSS text-decoration-style用法及代碼示例
- CSS text-overflow用法及代碼示例
- HTML Option text用法及代碼示例
- HTML Input Text value用法及代碼示例
- HTML Input Text type用法及代碼示例
- HTML Input Text size用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 Fabric.js | Text strokeDashOffset Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。