如何使用 FabricJS 垂直翻轉三角形?
在本教程中,我們將學習如何使用 FabricJS 垂直翻轉三角形物件。三角形是 FabricJS 提供的各種形狀之一。為了建立一個三角形,我們將必須建立一個 fabric.Triangle 類的例項並將其新增到畫布中。我們可以使用 flipY 屬性垂直翻轉三角形物件。
語法
new fabric.Triangle({ flipY: Boolean }: Object)
引數
選項 (可選) − 此引數是一個物件,它為我們的三角形提供了額外的自定義。使用此引數,可以更改與物件的 flipY 屬性相關的屬性,例如顏色、游標、筆劃寬度以及許多其他屬性。
選項鍵
flipY − 此屬性接受一個布林值,允許我們垂直翻轉物件。
示例 1
將 flipY 作為鍵並傳遞 'false' 值
讓我們看一個程式碼示例,它向我們展示了 FabricJS 中三角形物件的預設方向。由於我們將 flipY 屬性設定為 False 值,因此三角形物件不會垂直翻轉。
<!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>Passing flipY as key with a 'false' value</h2> <p>You can see that the triangle object has not been flipped vertically.</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 triangle object var triangle = new fabric.Triangle({ left: 75, top: 45, width: 180, height: 109, stroke: "#e3f988", strokeWidth: 5, flipY: false, }); // Create gradient fill triangle.set( "fill", new fabric.Gradient({ type: "linear", coords: { x1: 0, y1: 0, x2: 0, y2: 100 }, colorStops: [ { offset: 0, color: "#545a2c" }, { offset: 1, color: "#6495ed" }, ], }) ); // Add it to the canvas canvas.add(triangle); </script> </body> </html>
示例 2
將 flipY 屬性作為鍵並傳遞 'true' 值
在此示例中,我們有一個寬度為 180px,高度為 109px,並填充垂直線性漸變的三角形物件。當我們將 flipY 屬性應用於三角形物件時,它會垂直翻轉,因此我們看到三角形也翻轉了。
<!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>Passing the flipY property as key with a 'true' value</h2> <p>You can see that the triangle object has been flipped vertically</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 triangle object var triangle = new fabric.Triangle({ left: 75, top: 45, width: 180, height: 109, stroke: "#e3f988", strokeWidth: 5, flipY: true, }); // Create gradient fill triangle.set( "fill", new fabric.Gradient({ type: "linear", coords: { x1: 0, y1: 0, x2: 0, y2: 100 }, colorStops: [ { offset: 0, color:"#545a2c" }, { offset: 1, color: "#6495ed" }, ], }) ); // Add it to the canvas canvas.add(triangle); </script> </body> </html>
廣告