使用FabricJS在移動文字框時如何設定邊框不透明度?


在本教程中,我們將學習如何使用FabricJS在移動文字框時設定其邊框的不透明度。我們可以自定義、拉伸或移動文字框中的文字。為了建立一個文字框,我們需要建立一個`fabric.Textbox`類的例項並將其新增到畫布上。我們可以使用`borderOpacityWhenMoving`屬性來更改移動文字框時邊框的不透明度。

語法

new fabric.Textbox(text: String, { borderOpacityWhenMoving: Number }: Object)

引數

  • text − 此引數接受一個字串,即我們想要在文字框中顯示的文字字串。

  • options (可選) − 此引數是一個物件,它為我們的文字框提供額外的自定義選項。使用此引數,可以更改許多與物件相關的屬性,其中`borderOpacityWhenMoving`也是一個屬性,例如顏色、游標、描邊寬度等等。

選項鍵

  • borderOpacityWhenMoving − 此屬性接受一個數字,指定我們在移動文字框時希望邊框的不透明度。它允許我們控制移動文字框物件時邊框的不透明度。預設值為0.4

示例1

顯示`borderOpacityWhenMoving`屬性的預設行為

讓我們看一個程式碼示例,它展示了`borderOpacityWhenMoving`屬性的預設行為。當我們選擇文字框物件並在畫布上移動它時,選中邊框的不透明度會從1(完全不透明)變為0.4,使其看起來有點半透明。

<!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>Displaying the default behaviour of borderOpacityWhenMoving property</h2> <p>You can select the textbox and drag it around to see that the border opacity changes from being fully opaque (1) to being translucent (0.4)</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 textbox object var textbox = new fabric.Textbox("Small steps motivate. Big steps overwhelm.", { backgroundColor: "#ffe5b4", width: 400, top: 70, left: 110, borderColor: "red", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

示例2

將`borderOpacityWhenMoving`作為鍵傳遞

讓我們看一個程式碼示例,為`borderOpacityWhenMoving`屬性賦值。在本例中,我們將其值設定為0。這意味著當我們移動文字框時,邊框的不透明度將變為0,並且將不可見。

<!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 borderOpacityWhenMoving as key</h2> <p>You can select the textbox and drag it around to see that the borders are no longer visible when being moved</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 textbox object var textbox = new fabric.Textbox("Small steps motivate. Big steps overwhelm.", { backgroundColor: "#ffe5b4", width: 400, top: 70, left: 110, borderColor: "red", borderOpacityWhenMoving: 0, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

更新於:2022年8月2日

瀏覽量:116

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告