如何使用 FabricJS 獲取線條的 SVG 表示?
在本文中,我們將學習如何使用 FabricJS 獲取線條的 SVG 表示。線條元素是 FabricJS 提供的基本元素之一,用於建立直線。由於線條元素在幾何上是一維的,並且不包含內部,因此它們永遠不會填充。我們可以透過建立fabric.Line的例項,指定線條的 x 和 y 座標並將其新增到畫布上來建立線條物件。為了獲取線條物件的 SVG 表示,我們使用_toSVG方法。
語法
_toSVG(): Array
不使用_toSVG方法
示例
讓我們看一個程式碼示例,看看不使用_toSVG方法時記錄的輸出。_toSVG方法返回一個包含例項特定 svg 表示的字串陣列。但是,由於我們沒有使用_toSVG方法,因此我們將無法在控制檯中看到字串陣列。已記錄線條物件的預設值以進行說明。
<!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 _toSVG 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 Line object var line = new fabric.Line([70, 100, 150, 200], { stroke: "blue", }); // Add it to the canvas canvas.add(line); // Console logging the Line object console.log("The Line object is as follows: ", line); </script> </body> </html>
使用_toSVG方法
示例
在這個例子中,我們使用了_toSVG方法來獲取一個包含物件svg表示的字串陣列。
<!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 _toSVG method</h2> <p> You can open console from dev tools and see that the logged output contains an array of strings containing the svg representation of the Line object </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([70, 100, 150, 200], { stroke: "blue", }); // Add it to the canvas canvas.add(line); // Using the _toSVG method console.log( "The svg representation of the Line object is as follows: ", line._toSVG() ); </script> </body> </html>
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP