如何使用 FabricJS 在 IText 物件的 URL 字串中移除當前物件的變換?


在本教程中,我們將學習如何在使用 FabricJS 的情況下,從 IText 物件的 URL 字串中移除當前物件的變換(縮放、角度、翻轉、傾斜)。IText 類是在 FabricJS 1.4 版本中引入的,它擴充套件了 fabric.Text,用於建立 IText 例項。IText 例項使我們能夠在無需額外配置的情況下選擇、剪下、貼上或新增新文字。它還支援各種按鍵組合和滑鼠/觸控組合,使文字具有互動性,而這些功能在 Text 中並未提供。

然而,基於 IText 的文字框允許我們調整文字矩形的大小並自動換行。這對於 IText 來說是不正確的,因為高度不會根據換行進行調整。我們可以使用各種屬性來操作我們的 IText 物件。同樣,我們可以使用 withoutTransform 屬性在 IText 物件的 URL 字串中移除當前物件的變換。

語法

toDataURL({ withoutTransform: Boolean }: Object): String

引數

  • options (可選) − 此引數是一個物件,它為 IText 物件的 URL 表示提供額外的自定義。使用此引數,可以更改高度、質量、格式以及許多其他屬性,其中 withoutTransform 是一個屬性。

選項鍵

  • withoutTransform − 此屬性接受一個布林值,允許我們去除當前物件的變換。如果傳遞給它一個 true 值,則最終輸出影像中將不再包含縮放、角度、翻轉或傾斜。

示例 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 屬性並傳遞一個 true 值

讓我們來看一個程式碼示例,看看在使用 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>

更新於: 2022-09-13

90 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.