FabricJS – 如何在 Line 物件的 URL 字串中移除當前物件的變換?
在本教程中,我們將學習如何使用 FabricJS 在 Line 物件的 URL 字串中移除當前物件的變換(縮放、角度、翻轉、傾斜)。Line 元素是 FabricJS 提供的基本元素之一,用於建立直線。由於線元素在幾何上是一維的,並且不包含內部,因此它們永遠不會被填充。
我們可以透過建立fabric.Line的例項,指定線的 x 和 y 座標並將其新增到畫布上來建立線物件。為了移除 Line 物件 URL 字串中的當前物件變換,我們使用withoutTransform屬性。
語法
toDataURL({ withoutTransform: Boolean }: Object): String
引數
options (可選) − 此引數是一個Object,它為 Line 物件的 URL 表示提供額外的自定義選項。使用此引數可以更改高度、質量、格式以及許多其他屬性,其中withoutTransform是一個屬性。
選項鍵
withoutTransform − 此屬性接受一個布林值,允許我們去除當前物件的變換。如果傳遞 true 值,最終輸出影像中將不再包含縮放、角度、翻轉或傾斜。
使用withoutTransform屬性並傳遞 false 值
示例
讓我們來看一個程式碼示例,看看當withoutTransform屬性傳遞 false 值時輸出影像的樣子。開啟開發者工具中的控制檯後,我們可以看到 Line 物件的 URL 表示。我們可以複製該 URL 並將其貼上到新標籤頁的位址列中以檢視最終輸出。
在這個例子中,我們為 Line 物件傳遞了 scaleY 和 angle 屬性,分別賦值垂直縮放因子和角度。因此,我們的輸出將垂直縮放,旋轉角度為 70 度。但是,由於我們也為withoutTransform屬性傳遞了 false 值,因此最終輸出影像仍然包含 scaleY 和 angle 屬性。
<!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 the 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 and has an angle of 70 degrees </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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, angle: 70, scaleY: 2, }); // Add it to the canvas canvas.add(line); // Using the toDataURL method console.log(line.toDataURL({ withoutTransform: false })); </script> </body> </html>
使用withoutTransform屬性並傳遞 true 值
示例
讓我們來看一個程式碼示例,看看當使用withoutTransform屬性並向其傳遞 true 值時,Line 物件的最終輸出影像是什麼樣子。在這種情況下,我們的最終輸出影像將不包含任何物件變換。
<!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 the 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 or an angle of 70 degrees </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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, angle: 70, scaleY: 2, }); // Add it to the canvas canvas.add(line); // Using the toDataURL method console.log(line.toDataURL({ withoutTransform: true })); </script> </body> </html>
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP