如何使用 FabricJS 在移動物件上建立帶有十字準線游標的三角形?
在本教程中,我們將使用 FabricJS 建立一個在移動物件上帶有十字準線游標的三角形。十字準線是可用的原生游標樣式之一,也可以在 FabricJS 畫布中使用。FabricJS 提供了各種型別的滑鼠游標,如預設、全部滾動、十字準線、列調整大小、行調整大小等,這些游標在後臺重用了原生游標。
moveCursor 屬性在物件在畫布中移動時設定游標的樣式。
語法
new fabric.Triangle({ moveCursor: String }: Object)
引數
選項(可選) - 此引數是一個物件,它為我們的三角形提供了額外的自定義。使用此引數,可以更改與moveCursor 屬性相關的物件的屬性,例如顏色、游標、描邊寬度以及許多其他屬性。
選項鍵
moveCursor - 此屬性接受一個字串,允許我們在畫布上移動此三角形物件時設定預設的游標值。該值決定了在移動畫布物件時使用的游標型別。
示例 1
物件在畫布中移動時的預設游標值
預設情況下,當我們在畫布中移動三角形物件時,游標型別為move。讓我們看一個程式碼示例來理解這一點。
<!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 鍵與值為“crosshair”一起傳遞給三角形類。這將確保當我們在畫布中移動物件時,游標值為十字準線。這在下方的程式碼示例中進行了說明
<!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 "crosshair"</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: "crosshair", }); // Add it to the canvas canvas.add(triangle); </script> </body> </html>
廣告