在本教程中,我們將學(xué)習(xí)如何使用 FabricJS 水平翻轉(zhuǎn) Triangle 對(duì)象。三角形是 FabricJS 提供的各種形狀之一。為了創(chuàng)建一個(gè)三角形,我們必須創(chuàng)建一個(gè) Fabric.Triangle 類的實(shí)例并將其添加到畫布中。我們可以使用 flipX 屬性水平翻轉(zhuǎn)三角形對(duì)象。
語(yǔ)法
new fabric.Triangle({ flipX: Boolean }: Object)
登錄后復(fù)制
參數(shù)
選項(xiàng)(可選) – 此參數(shù)是一個(gè)對(duì)象 為我們的三角形提供額外的定制。使用此參數(shù),可以更改與 flipX 為屬性的對(duì)象相關(guān)的顏色、光標(biāo)、描邊寬度等屬性以及許多其他屬性。
選項(xiàng)鍵
flipX?? 此屬性接受布爾值 允許我們水平翻轉(zhuǎn)對(duì)象的值。
示例 1
傳遞flipX作為帶有“false”值的鍵
讓我們看一個(gè)代碼示例,它向我們展示了 FabricJS 中三角形對(duì)象的默認(rèn)方向。由于我們向 flipX 屬性傳遞了 False 值,因此三角形對(duì)象不會(huì)水平翻轉(zhuǎn)。
<!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 flipX as key with a 'False' value</h2> <p>You can see that the triangle object has not been flipped horizontally</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, flipX: false, }); // Create gradient fill triangle.set( "fill", new fabric.Gradient({ type: "linear", coords: { x1: 0, y1: 0, x2: 100, y2: 0 }, colorStops: [ { offset: 0, color: "#545a2c" }, { offset: 1, color: "#6495ed" }, ], }) ); // Add it to the canvas canvas.add(triangle); </script> </body> </html>
登錄后復(fù)制
示例 2
將 flipX 屬性作為具有“True”值的鍵傳遞
在此示例中,我們有一個(gè)寬度為 180px、高度為 109px 的三角形對(duì)象,具有水平線性漸變填充。當(dāng)我們將 flipX 屬性應(yīng)用于三角形對(duì)象時(shí),它會(huì)水平翻轉(zhuǎn),因此我們看到漸變也翻轉(zhuǎn)了。
<!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 flipX property as key with a 'True' value</h2> <p>You can see that the triangle has been flipped horizontally</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, flipX: true, }); // Create gradient fill triangle.set( "fill", new fabric.Gradient({ type: "linear", coords: { x1: 0, y1: 0, x2: 100, y2: 0 }, colorStops: [{ offset: 0, color: "#545a2c" },{ offset: 1, color: "#6495ed" },],}) ); // Add it to the canvas canvas.add(triangle); </script> </body> </html>
登錄后復(fù)制
以上就是如何使用 FabricJS 水平翻轉(zhuǎn)三角形?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!