我們可以透過建立一個 fabric.Polygon 的實例來建立一個多邊形物件。多邊形物件可以由一組相連的直線段組成的任何封閉形狀來描述。由於它是FabricJS的基本元素之一,我們也可以透過套用屬性如角度、不透明度等來輕鬆地自訂它。為了讓多邊形添加圖案和顏色,我們可以使用FabricJS中的Pattern類別。
語法
new fabric.Pattern( options: Object, callback: function )
參數
options (optional) − This parameter is an which provides additional customizations to our object. Using this parameter offsetX, cross-origin and a lot of other properties can be changed related to the Pattern.
callback − 此參數是一個在回呼初始化後呼叫的函數。此參數是可選的。
範例1:建立fabric.Pattern()的實例並將其加入到我們的多邊形物件中
讓我們看一個程式碼範例,看看如何建立一個 fabric.Pattern 的實例並將其新增到畫布中。在這裡,我們建立了一個多邊形對象,它的形狀是一個矩形(一個不規則的多邊形)。我們初始化了 createPattern 函數,該函數將把圖案加到我們的矩形中。最後,我們呼叫了 createPattern 函數,並將所需的 URL 傳遞給它。
<!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> Creating an instance of fabric.Pattern() and adding it to our Polygon object </h2> <p>You can see that a pattern has been created</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiating the createPattern function which loads image // and adds the image as pattern to the rect object function createPattern(url) { fabric.util.loadImage(url, function (img) { rect.set( "fill", new fabric.Pattern({ source: img, }) ); canvas.renderAll(); }); } // Initiating a Polygon object var rect = new fabric.Polygon( [ { x: 0, y: 0 }, { x: 500, y: 0 }, { x: 500, y: 200 }, { x: 0, y: 200 }, ], { left: 50, top: 20, stroke: "black", } ); // Adding it to the canvas canvas.add(rect); // Calling the createPattern function createPattern("https://www.tutorialspoint.com/images/logo.png"); </script> </body> </html>
範例2:在我們的多邊形中新增圖像和顏色的模式
讓我們來看一個程式碼範例,看看如何為我們的多邊形物件建立一個帶有圖像和顏色的動態模式。在這種情況下,我們使用了fromURL方法來載入圖像,並初始化了一個fabric.StaticCanvas()對象,這是FabricJS的主要渲染表面之一,對於創建動態模式至關重要。
我們使用了 setBackgroundColor 方法來為多邊形設定背景顏色。最後,我們將多邊形物件加入畫布。
<!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 id="Adding-a-pattern-with-Image-and-Colour-to-our-Polygon">Adding a pattern with Image and Colour to our Polygon</h2> <p>You can see that a pattern with image and colour has been created and further use the number field to change the pattern density</p> <label>Add a width value(50-500): </label> <input type="number" id="changeWidth" value="50"/> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiating the colour that we want to fill the pattern with var imgColor = "rgba(216,228,188,0.5)"; // Using fromURL method to load image and add to canvas // further setting the dimensions and background colour fabric.Image.fromURL( "https://www.tutorialspoint.com/images/logo.png", function (img) { img.scaleToWidth(100); img.scaleToHeight(90); var patternSourceCanvas = new fabric.StaticCanvas(); patternSourceCanvas.add(img); patternSourceCanvas.renderAll(); patternSourceCanvas.setBackgroundColor( imgColor, patternSourceCanvas.renderAll.bind(patternSourceCanvas) ); // Initiating a Pattern object var pattern = new fabric.Pattern({ source: patternSourceCanvas.getElement(), repeat: "repeat", }); // Adding a polygon object to the canvas canvas.add( // Initiate a polygon object new fabric.Polygon( [ { x: -100, y: -175 }, { x: 100, y: -175 }, { x: 200, y: 0 }, { x: 100, y: 175 }, { x: -100, y: 175 }, { x: -200, y: 0 }, ], { top: 30, left: 10, strokeWidth: 3, stroke: "#96c8a2", fill: pattern, objectCaching: false, scaleX: 0.5, scaleY: 0.5, } ) ); // Using getElementById and targeting the input tag with the id as "changeWidth" document.getElementById("changeWidth").oninput = function () { img.scaleToWidth(parseInt(this.value, 10)); patternSourceCanvas.setDimensions({ width: img.getScaledWidth(), height: img.getScaledHeight(), }); canvas.requestRenderAll(); }; } ); </script> </body> </html>
結論
在本教學中,我們使用了兩個簡單的範例來示範如何使用FabricJS為多邊形新增影像和顏色的模式。
以上是使用FabricJS向多邊形添加帶有圖像和顏色的圖案的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript是現代Web開發的核心語言,因其多樣性和靈活性而廣泛應用。 1)前端開發:通過DOM操作和現代框架(如React、Vue.js、Angular)構建動態網頁和單頁面應用。 2)服務器端開發:Node.js利用非阻塞I/O模型處理高並發和實時應用。 3)移動和桌面應用開發:通過ReactNative和Electron實現跨平台開發,提高開發效率。

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python更适合数据科学和机器学习,JavaScript更适合前端和全栈开发。1.Python以简洁语法和丰富库生态著称,适用于数据分析和Web开发。2.JavaScript是前端开发核心,Node.js支持服务器端编程,适用于全栈开发。

JavaScript不需要安裝,因為它已內置於現代瀏覽器中。你只需文本編輯器和瀏覽器即可開始使用。 1)在瀏覽器環境中,通過標籤嵌入HTML文件中運行。 2)在Node.js環境中,下載並安裝Node.js後,通過命令行運行JavaScript文件。

如何在Quartz中提前發送任務通知在使用Quartz定時器進行任務調度時,任務的執行時間是由cron表達式設定的。現�...

在JavaScript中如何獲取原型鏈上函數的參數在JavaScript編程中,理解和操作原型鏈上的函數參數是常見且重要的任�...

在微信小程序web-view中使用Vue.js動態style位移失效的原因分析在使用Vue.js...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3漢化版
中文版,非常好用