在本文中,我們將學習如何使用 FabricJS 僅當物件完全包含在選擇區域中時才啟用物件的選擇。我們可以使用 SelectionFullyContained 屬性來實現此目的。
new fabric.Canvas(element: HTMLElement|String, { selectionFullyContained: Boolean }: Object)
# - 此參數是
選項(可選) - 此參數是一個對象,它提供對我們的畫布進行額外的自訂。使用此參數,可以更改與畫布相關的顏色、遊標、邊框寬度和許多其他屬性,其中selectionFullyContained 是一個屬性。它接受一個布林值,該值確定是否僅當物件完全包含在選擇區域中時才應選擇該物件。預設值為 False。
傳遞值為False 的SelectionFullyContained 鍵
##讓我們看一下FabricJS 中預設行為的程式碼範例,即儘管物件未完全包含在選擇區域中,但物件仍被選取。在此範例中,我們將 SelectionFullyContained 的值傳遞為 False。<!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>Enabling selection of an object only when it is fully contained in a selection area</h2> <p>Select a partial area around the object. The entire object would be selected even if you select a partial area containing the object.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { selectionFullyContained: false }); // Creating an instance of the fabric.Rect class var circle = new fabric.Circle({ left: 215, top: 100, radius: 50, fill: "orange", }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>範例2
將selectionFullyContained鍵傳遞給值為True的類別
讓我們來看一個程式碼範例,其中selectionFullyContained屬性的值具有已設定為True。正如我們所看到的,只有當物件完全包含在選擇區域中時才會選擇該物件。<!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>Enabling selection of an object only when it is fully contained in a selection area</h2> <p>Here you cannot select the object by selecting a partial area around the object. The object must be fully contained inside the selection area.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { selectionFullyContained: true }); // Creating an instance of the fabric.Rect class var circle = new fabric.Circle({ left: 215, top: 100, radius: 50, fill: "orange" }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
以上是如何僅當物件完全包含在 FabricJS 中的選擇區域中時才啟用物件選擇?的詳細內容。更多資訊請關注PHP中文網其他相關文章!