首頁 > web前端 > js教程 > 主體

如何使用 FabricJS 建立帶有背景顏色的文字方塊?

WBOY
發布: 2023-09-06 11:57:02
轉載
1513 人瀏覽過

如何使用 FabricJS 创建带有背景颜色的文本框?

在本教程中,我們將使用 FabricJs 建立一個具有背景顏色的文字方塊。我們可以自訂、拉伸或移動文字方塊中寫入的文字。我們也可以使用諸如fontSize、fontFamily、fontStyle、fontWeight 等屬性來自訂文字本身。為了建立文字框,我們必須建立fabric.Textbox 的實例em> 類別並將其新增到畫布中。 backgroundColor 屬性允許我們為物件的背景分配顏色,並且文字框的形狀為矩形。

語法

new fabric.Textbox(text: String, { backgroundColor: String }: Object)
登入後複製

參數

  • text - 此參數接受一個String ,這是我們要使用的文字字串。想要在我們的文字方塊中顯示。

  • 選項(可選) - 此參數是一個物件,它提供了額外的自訂我們的文字方塊。使用此參數,可以變更與 backgroundColor 為屬性的文字方塊相關的顏色、遊標、描邊寬度等屬性以及許多其他屬性。

  • < /ul>

    Options Keys

    • #backgroundColor - 此屬性接受一個String 值,該值決定背景顏色。

    範例1

    將backgroundColor屬性作為帶有十六進位值的鍵傳遞

    讓我們來看一個程式碼範例,使用十六進位顏色值將背景顏色指派給我們的文字方塊物件。在此範例中,我們使用了十六進位顏色代碼“#ffe4e1”,它是非常淺的紅色陰影。

    <!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 backgroundColor property as key with a hexadecimal value</h2>
       <p>You can see that the background colour is a very light shade of red.</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("Details matter, it&#39;s worth waiting to get it right.", {
             backgroundColor: "#ffe4e1",
             width: 400,
             top: 70,
             left: 110,
          });
    
          // Add it to the canvas
          canvas.add(textbox);
       </script>
    </body>
    </html>
    登入後複製

    範例2

    將backgroundColor屬性作為帶有rgba值的鍵傳遞

    我們可以使用RGBA值,而不是十六進制顏色代碼,這代表- 紅色、綠色、藍色和阿爾法。 alpha 參數指定顏色的不透明度。在此範例中,我們使用了 rgba 值 (0,206,209,0.4),它是不透明度為 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>Passing backgroundColor property as key with an RGBA value</h2>
       <p>You can see that the background colour is a dark turquoise colour with 0.4 opacity.</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("Details matter, it&#39;s worth waiting to get it right.", {
             backgroundColor: "rgba(0,206,209, 0.4)",
             width: 400,
             top: 70,
             left: 110,
          });
    
          // Add it to the canvas
          canvas.add(textbox);
       </script>
    </body>
    </html>
    登入後複製

    以上是如何使用 FabricJS 建立帶有背景顏色的文字方塊?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!