在本教程中,我們將使用 FabricJS 創建一個帶有等待光標移動對象的三角形。 wait 是可用的本機光標樣式之一,也可以在 FabricJS 畫布中使用。 FabricJS 提供了各種類型的光標,例如默認、全滾動、十字準線、列調整大小、行調整大小等,它們在幕后重用了本機光標。
moveCursor 屬性設置當對象在畫布中移動時光標的樣式。
語法
new fabric.Triangle({ moveCursor: String }: Object)
登錄后復制
參數
選項(可選) – 此參數是一個對象 為我們的三角形提供額外的定制。使用此參數,可以更改與 moveCursor 為屬性的對象相關的屬性,例如顏色、光標、描邊寬度和許多其他屬性。
選項鍵
moveCursor – 該屬性接受一個字符串,它允許我們設置在畫布上移動此三角形對象時的默認光標值。該值決定移動畫布對象時使用的光標類型。
示例 1
對象移動時的默認光標值在畫布上移動
默認情況下,當我們在畫布中圍繞三角形對象移動時,光標類型是移動。讓我們看一個代碼示例來理解這一點。
<!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>Default cursor value when object is moved around the canvas</h2> <p>You can move around the triangle to see that the default cursor type is "move"</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: 105, top: 75, width: 90, height: 80, fill: "#eedc82", stroke: "#bcb88a", strokeWidth: 5, }); // Add it to the canvas canvas.add(triangle); </script> </body> </html>
登錄后復制
示例 2
將 moveCursor 屬性作為鍵傳遞給值
在此示例中,我們將 moveCursor 鍵傳遞給三角形類值為“等待”。這將確保當我們在畫布中的對象周圍移動時,光標值處于等待狀態。下面的代碼示例對此進行了說明。
<!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 moveCursor property as key with a value</h2> <p>You can move around the triangle to see that the cursor type is "wait"</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: 105, top: 75, width: 90, height: 80, fill: "#eedc82", stroke: "#bcb88a", strokeWidth: 5, moveCursor: "wait", }); // Add it to the canvas canvas.add(triangle); </script> </body> </html>
登錄后復制
以上就是如何使用 FabricJS 在移動對象上創建帶有等待光標的三角形?的詳細內容,更多請關注www.92cms.cn其它相關文章!