如何使用 FabricJS 將 IText 物件轉換為類似資料的 URL 字串?


在本教程中,我們將學習如何使用 FabricJS 將 IText 物件轉換為類似資料的 URL 字串。IText 類是在 FabricJS 1.4 版本中引入的,它擴充套件了 fabric.Text,用於建立 IText 例項。IText 例項使我們能夠在無需額外配置的情況下選擇、剪下、貼上或新增新文字。它還支援各種鍵盤組合和滑鼠/觸控組合,使文字具有互動性,而 Text 類則不提供這些功能。

但是,基於 IText 的文字框允許我們調整文字矩形的大小並自動換行。這對於 IText 來說並不適用,因為高度不會根據換行進行調整。我們可以使用各種屬性來操作 IText 物件。同樣,我們可以使用 toDataURL 方法將 IText 物件轉換為類似資料的 URL 字串。

語法

toDataURL(options: Object): String

引數

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

示例 1

不使用 toDataURL 方法時的預設值

讓我們看一個程式碼示例,瞭解在不使用 toDataURL 方法時 IText 物件的外觀。使用 toDataURL 方法時,會返回 IText 物件的 URL 表示。在此示例中,我們建立了一個 itext 物件併為其分配了各種屬性,例如 stroke、fill、shadow 等。但是,由於我們沒有使用 toDataURL 方法,因此我們無法在控制檯中看到物件的 URL 表示,而是會記錄 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>Default value without using toDataURL method</h2> <p>You can open console from dev tools and see the logged output</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: 50, top: 70, fill: "#c70039", backgroundColor: "#c1dfed", stroke: "#c70039", originX: "center", shadow: shadow, } ); // Add it to the canvas canvas.add(itext); // Console logging the itext object console.log("The itext object is as follows: ", itext); </script> </body> </html>

示例 2

使用 toDataURL 方法

讓我們看一個程式碼示例,瞭解使用 toDataURL 方法時的日誌輸出。一旦我們從開發者工具中開啟控制檯,我們就可以看到 IText 物件的 URL 表示。我們可以複製該 URL 並將其貼上到新標籤頁的位址列中以檢視最終輸出。

<!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 toDataURL method</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 image </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: 50, top: 70, fill: "#c70039", backgroundColor: "#c1dfed", stroke: "#c70039", originX: "center", shadow: shadow, } ); // Add it to the canvas canvas.add(itext); // Using the toDataURL method console.log(itext.toDataURL()); </script> </body> </html>

更新於: 2022年9月12日

262 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.