在本教程中,我們將學習如何使用 FabricJS 刪除 IText 對象的 URL 字符串中的當前對象變換(縮放、角度、翻轉、傾斜)。 IText 類是在 FabricJS 版本 1.4 中引入的,它擴展了 Fabric.Text 并用于創建 IText 實例。 IText 實例使我們可以自由選擇、剪切、粘貼或添加新文本,而無需額外配置。還有各種支持的按鍵組合和鼠標/觸摸組合使文本具有交互性,而 Text 中未提供這些組合。
然而,基于 IText 的 Textbox 允許我們調整文本矩形的大小并自動換行。對于 IText 來說情況并非如此,因為高度不會根據換行進行調整。我們可以通過使用各種屬性來操作 IText 對象。同樣,我們可以使用 withoutTransform 屬性來刪除 IText 對象的 URL 字符串中當前對象的變換。
語法
toDataURL({ withoutTransform: Boolean }: Object): String
登錄后復制
參數
選項(可選) – 此參數是一個對象,它為 IText 對象的 URL 表示形式提供額外的自定義。使用此參數可以更改高度、質量、格式和許多其他屬性,其中 withoutTransform 是一個屬性。
選項鍵
withoutTransform – 該屬性接受一個布爾值,它允許我們擺脫當前的對象變換。向其傳遞真值后,最終輸出圖像中將不再存在比例、角度、翻轉或傾斜。
示例 1
使用 withoutTransform 屬性并向其傳遞一個 false 值
讓我們看一個代碼示例,看看向 withoutTransform 屬性傳遞 false 值時的輸出圖像。一旦我們從開發工具打開控制臺,我們就可以看到 IText 對象的 URL 表示。我們可以復制該 URL 并將其粘貼到新選項卡的地址欄中以查看最終輸出。在此示例中,我們向 IText 對象傳遞了指定垂直比例因子的scaleY 屬性。因此我們的輸出將垂直縮放。但是,由于我們還向 withoutTransform 屬性傳遞了一個 false 值,因此我們的最終輸出圖像仍將包含 scaleY 屬性。
<!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 withoutTransform property and passing it a false value</h2>
<p>You can open console from dev tools and see the output URL. You can copy that and paste it in the address bar of a new tab to see that the final image contains vertical scaling </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 shadow object
var shadow = new fabric.Shadow({
blur: 25,
color: "grey",
offsetX: 12,
offsetY: 15,
});
// Initiate an itext object
var itext = new fabric.IText(
"Add sample text here.Lorem ipsum dolor sit amet consectetur adipiscing.",{
width: 300,
left: 60,
top: 70,
fill: "#c70039",
backgroundColor: "#c1dfed",
stroke: "#c70039",
originX: "center",
shadow: shadow,
scaleY: 2,
}
);
// Add it to the canvas
canvas.add(itext);
// Using the toDataURL method
console.log(
itext.toDataURL({ withoutTransform: false })
);
</script>
</body>
</html>
登錄后復制
示例 2
使用 withoutTransform 屬性并向其傳遞一個真值
讓我們看一個代碼示例,看看當使用 withoutTransform 屬性并向其傳遞 true 值時,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 withoutTransform property and passing it a true value</h2>
<p>You can open console from dev tools and see the output URL. You can copy that and paste it in the address bar of a new tab to see that the final image does not contain vertical scaling </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 shadow object
var shadow = new fabric.Shadow({
blur: 25,
color: "grey",
offsetX: 12,
offsetY: 15,
});
// Initiate an itext object
var itext = new fabric.IText(
"Add sample text here.Lorem ipsum dolor sit amet consectetur adipiscing.",{
width: 300,
left: 60,
top: 70,
fill: "#c70039",
backgroundColor: "#c1dfed",
stroke: "#c70039",
originX: "center",
shadow: shadow,
scaleY: 2,
}
);
// Add it to the canvas
canvas.add(itext);
// Using the toDataURL method with withoutTransform
console.log(
itext.toDataURL({ withoutTransform: true })
);
</script>
</body>
</html>
登錄后復制
以上就是如何使用 FabricJS 刪除 IText 對象的 URL 字符串中的當前對象轉換?的詳細內容,更多請關注www.92cms.cn其它相關文章!






