如何使用 FabricJS 在懸停物件時建立帶有輔助游標的橢圓?
在本教程中,我們將學習如何使用 FabricJS 建立一個橢圓,並在懸停物件時顯示輔助游標。“help”是可用於 FabricJS 畫布的原生游標樣式之一。FabricJS 提供各種型別的游標,例如 default、all-scroll、crosshair、col-resize、row-resize 等,這些游標在底層重用了原生游標。hoverCursor 屬性設定懸停在畫布物件上時游標的樣式。
語法
new fabric.Ellipse({ hoverCursor: String }: Object)
引數
options (可選) − 此引數是一個物件,它為我們的橢圓提供額外的自定義選項。使用此引數可以更改與物件相關的許多屬性,例如顏色、游標、筆劃寬度以及hoverCursor屬性。
選項鍵
hoverCursor − 此屬性接受一個字串,該字串確定在懸停在畫布物件上時要使用的游標名稱。使用此屬性,我們可以設定在懸停畫布上的橢圓物件時預設的游標值。
示例 1
將hoverCursor鍵傳遞給類
預設情況下,當我們懸停在畫布中的橢圓物件上時,游標型別為移動。讓我們來看一個程式碼示例,該示例演示如何在 FabricJS 中建立一個畫布,並在懸停在橢圓物件上時顯示輔助游標。
<!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 Ellipse with help cursor on hover over objects using FabricJS</h2> <p>Hover the mouse over the ellipse to see the <b>help</b> cursor.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Initiate an ellipse instance var ellipse = new fabric.Ellipse({ left: 115, top: 100, fill: "#b22222", rx: 80, ry: 50, stroke: "black", strokeWidth: 5, hoverCursor: "help", }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
示例 2
演示這隻影響例項
在此示例中,我們將hoverCursor鍵傳遞給橢圓類,這意味著不會更改畫布中每個物件的hoverCursor屬性。更改只會發生在該單個物件上。
<!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 Ellipse with help cursor on hover over objects using FabricJS</h2> <p>Hover the mouse over the objects. On the left ellipse, you would get to see <b>help</b> cursor. We haven't applied the <b>hoverCursor</b> property to the right ellipse. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Initiate an ellipse instance var ellipseOne = new fabric.Ellipse({ left: 115, top: 100, fill: "#b22222", rx: 80, ry: 50, stroke: "black", strokeWidth: 5, hoverCursor: "help", }); // Initiate another ellipse instance var ellipseTwo = new fabric.Ellipse({ rx: 80, ry: 50, left: 335, top: 100, fill: "black", stroke: "#8b0000", strokeWidth: 5, }); // Add them to the canvas canvas.add(ellipseOne); canvas.add(ellipseTwo); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
廣告