如何使用 FabricJS 在 IText 物件的 URL 字串中設定縮放倍數?
在本教程中,我們將學習如何使用 FabricJS 在 IText 物件的 URL 字串中設定縮放倍數。IText 類是在 FabricJS 1.4 版本中引入的,它擴充套件了 fabric.Text 並用於建立 IText 例項。IText 例項使我們能夠自由地選擇、剪下、貼上或新增新文字,而無需額外的配置。它還支援各種鍵盤組合和滑鼠/觸控組合,使文字具有互動性,而這些功能在 Text 中沒有提供。
然而,基於 IText 的文字框允許我們調整文字矩形的大小並自動換行。這對於 IText 來說並不適用,因為高度不會根據換行進行調整。我們可以使用各種屬性來操作 IText 物件。同樣,我們可以使用 multiplier 屬性在 IText 物件的 URL 字串中設定縮放倍數。
語法
toDataURL({ multiplier: Number }: Object): String
引數
options (可選) − 此引數是一個物件,它為 IText 物件的 URL 表示提供額外的自定義。使用此引數,可以更改高度、質量、格式和許多其他屬性,其中 multiplier 是一個屬性。
選項鍵
multiplier − 此屬性接受一個數字值,表示最終 IText 輸出影像的縮放倍數。預設值為 1。
示例 1
不使用 multiplier 屬性
讓我們看一個程式碼示例,以檢視不使用 multiplier 屬性時輸出影像的情況。一旦我們從開發者工具中開啟控制檯,我們就可以看到 IText 物件的 URL 表示。我們可以複製該 URL 並將其貼上到新標籤頁的位址列中以檢視最終輸出。由於我們沒有使用 multiplier 屬性,因此將使用預設的 multiplier 值,即 1。
<!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>Without using the multiplier property</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: 310, 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>
示例
使用 multiplier 屬性
讓我們看一個程式碼示例,以檢視使用 multiplier 屬性時 IText 物件的最終輸出影像是什麼樣的。在這種情況下,我們傳遞了一個值為 2 的值。因此,最終影像將在 x 和 y 方向上分別縮放兩倍。
<!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 multiplier property</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 the final 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: 310, 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({ multiplier: 2 })); </script> </body> </html>
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP