Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 99视频在线,精品手机在线视频,韩日一区二区三区

          整合營銷服務(wù)商

          電腦端+手機端+微信端=數(shù)據(jù)同步管理

          免費咨詢熱線:

          Fabric.js IText設(shè)置指定字符顏色和背景色

          文簡介

          點贊 + 關(guān)注 + 收藏 = 學(xué)會了


          IText 是 Fabric.js 提供的一個 可編輯文本 的元素。



          要設(shè)置文字顏色,可以設(shè)置 fill 。

          但 fill 會設(shè)置所有文字的顏色,如果你只想修改指定文字的顏色,只用 fill 就不是那么容易實現(xiàn)了。

          本文要講的就是 設(shè)置指定文字的顏色和背景色


          設(shè)置文字顏色或背景色,需要分情況討論的:

          1. 全文設(shè)置
          2. 設(shè)置指定文字顏色(單行)
          3. 設(shè)置指定文字顏色(多行)

          接下來就將上述情況逐一講解。



          起步

          <canvas id="c" width="600" height="400" style="border: 1px solid #ccc;"></canvas>
          
          <!-- 引入 Fabric.js -->
          <script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/521/fabric.js"></script>
          
          <script>
            // 初始化畫布
            const canvas = new fabric.Canvas('c')
          
            // 創(chuàng)建文本
            const iText = new fabric.IText('hello world')
          
            // 將文本添加到畫布里
            canvas.add(iText)
          </script>
          復(fù)制代碼

          首先把 Fabric.js 引入,然后初始化畫布。如果對這個概念不太熟的話,可以看看 Fabric.js 從入門到膨脹。

          最后通過 new fabric.IText 創(chuàng)建一段文字添加到畫布中。


          全文設(shè)置

          // 省略部分代碼
          
          const iText = new fabric.IText('hello world', {
            fill: 'pink'
          })
          復(fù)制代碼

          fill 可以設(shè)置文字的填充顏色。在 Fabric.js 里是使用這個屬性設(shè)置顏色的,和 css 設(shè)置文字顏色使用 color 不一樣~



          單行:設(shè)置指定文字顏色

          const iText = new fabric.IText('hello world', {
            styles: {
              0: {
                1: {
                  fill: '#f00' // 文字顏色,#f00是紅色
                }
              }
            }
          })
          復(fù)制代碼

          第一次看到上面的代碼時我也覺得有點奇怪,后來仔細(xì)看了下才發(fā)現(xiàn)這樣設(shè)計的用意。

          styles 是一個對象。

          styles: { // 設(shè)置樣式
            0: { // 第1行
              1: { // 第2個字符
                // 要設(shè)置的樣式
              }
            }
          }
          復(fù)制代碼

          上面這段代碼是這個意思。行號和字符位置都是從0開始算起,有點像數(shù)組下標(biāo)的意思。

          我們這個例子只有1行,所以行號是0。

          e 的下標(biāo)是 1 。所以上面的代碼就把 e 設(shè)置成紅色了。其他字符還是默認(rèn)的顏色。



          多行:設(shè)置指定文字顏色

          const iText = new fabric.IText('hello\nworld', {
            styles: { // 設(shè)置樣式
              0: { // 第1行
                1: {
                  fill: '#f00' // 文字顏色
                }
              },
              1: { // 第2行
                2: {
                  fill: 'hotpink'
                }
              }
            }
          })
          復(fù)制代碼

          IText 的換行是用 \n 來表達(dá)的。

          這個例子要 修改第1行第2個字符的文字顏色為紅色,第2行第3個字符為亮粉色

          從代碼里的注釋應(yīng)該可以看得懂本次操作。



          設(shè)置文字背景色

          const iText = new fabric.IText('hello world', {
            styles: {
            0: {
              1: {
                textBackgroundColor: 'yellowgreen', // 背景色
              }
            },
          })
          復(fù)制代碼

          和設(shè)置文字顏色的原理一樣,只是把關(guān)鍵字改一改就行。

          textBackgroundColor 翻譯成中文就是文本背景色。



          代碼倉庫

          ?Fabric 設(shè)置IText指定字符顏色和背景色



          推薦閱讀

          《Fabric.js 筆刷到底怎么用?》

          《Fabric.js 圓形筆刷》

          《純CSS 紅磚背景墻》

          《Fabric.js 自由繪制橢圓》

          頁中添加滾動字幕效果

          <!DOCTYPE html>

          <html>

          <head>

          <meta charset="utf-8">

          <title>滾動字體的設(shè)置</title>

          </head>

          <body>

          <canvas id="canvas1" width="600" height="600" style="border:1px solid #000000"></canvas>

          <script type="text/javascript">

          var canvas1 = document.querySelector("#canvas1") // 1.找到畫布對象

          var ctx = canvas1.getContext("2d") // 2.上下文對象(畫筆)


          ctx.shadowBlur = 10; // 陰影距離

          ctx.shadowColor = "red" // 陰影顏色

          ctx.shadowOffsetX = 30 // 陰影偏移

          ctx.shadowOffsetY = 30 // 陰影偏移


          ctx.font = "150px 楷體"


          ctx.fillText("你好!", 20,150)


          ctx.fillText("你好!", 20,350)


          ctx.strokeText('你好!',23, 153)


          ctx.strokeText('你好',23, 553)


          canvas繪制文字



          var x = 600

          setInterval(function(){

          if(x > -350){

          //清空畫布

          ctx.clearRect(0,0,600,600)

          ctx.strokeText('你好!',x, 153)

          ctx.fillText("你好!", x,350)


          ctx.font = "50px 宋體"

          ctx.strokeText('每天學(xué)習(xí)一點點',x, 553)


          x -= 3

          }else{x=590}



          }, 16)


          </script>


          </body>

          </html>

          者:IT智云編程

          鏈接:https://www.jianshu.com/p/4fa116fc4653

          在web前端開發(fā)過程中,UI設(shè)計師經(jīng)常會設(shè)計一些帶漸變文字的設(shè)計圖,在以前我們只能用png的圖片來代替文字,今天可以實現(xiàn)使用純CSS實現(xiàn)漸變文字了。下面就介紹3中實現(xiàn)方式供大家參考!

          基礎(chǔ)樣式:

          .gradient-text{text-align: left;text-indent:30px;line-height: 50px;font-size:40px;font-weight:bolder; position: relative; }
          

          第一種方法,使用 background-cli、 text-fill-color:

          .gradient-text-one{ 
           background-image:-webkit-linear-gradient(bottom,red,#fd8403,yellow); 
           -webkit-background-clip:text; 
           -webkit-text-fill-color:transparent; 
          }
          

          說明 :

          background: -webkit-linear-gradient(...) 為文本元素提供漸變背景。

          webkit-text-fill-color: transparent 使用透明顏色填充文本。

          webkit-background-clip: text 用文本剪輯背景,用漸變背景作為顏色填充文本。

          第二種方法,使用 mask-image:

          .gradient-text-two{
           color:red;
          }
          .gradient-text-two[data-content]::after{
           content:attr(data-content);
           display: block;
           position:absolute;
           color:yellow;
           left:0;
           top:0;
           z-index:2;
           -webkit-mask-image:-webkit-gradient(linear, 0 0, 0 bottom, from(yellow), to(rgba(0, 0, 255, 0)));
          }
          

          說明:

          mask-image 和 background-image 一樣,不僅可以取值是 圖片路徑,也可以是漸變色。

          第三種方法,使用 linearGradient、fill:

          .gradient-text-three{
           fill:url(#SVGID_1_);
           font-size:40px;
           font-weight:bolder;
          }
          <svg viewBoxs="0 0 500 300" class="svgBox">
           <defs>
           <linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="0" y1="10" x2="0" y2="50">
           <stop offset="0" style="stop-color:yellow"/>
           <stop offset="0.5" style="stop-color:#fd8403"/>
           <stop offset="1" style="stop-color:red"/>
           </linearGradient>
           </defs>
           <text text-anchor="middle" class="gradient-text-three" x="110px" y="30%">花信年華</text>
          </svg>
          

          說明:

          在SVG中,有兩種主要的漸變類型:

          線性漸變(linearGradient)

          放射性漸變(radialGradient)

          SVG中的漸變不僅可以用于填充圖形元素,還可以填充文本元素

          dom示例:

          <!DOCTYPE html>
          <html>
          <head>
           <meta charset="utf-8">
           <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
           <title>CSS3漸變字體</title>
           <link rel="stylesheet" >
           <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
           <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
           <style type="text/css">
           *{margin:0;padding:0;}
           body,html{width:100%;height:100%;}
           .wrapper{width:80%;margin:0 auto;margin-top:30px;}
           .gradient-text{text-align: left;text-indent:30px;line-height: 50px;font-size:40px;font-weight:bolder; position: relative; }
           .gradient-text-one{ 
           background-image:-webkit-linear-gradient(bottom,red,#fd8403,yellow); 
           -webkit-background-clip:text; 
           -webkit-text-fill-color:transparent; 
           }
           .gradient-text-two{
           color:red;
           }
           .gradient-text-two[data-content]::after{
           content:attr(data-content);
           display: block;
           position:absolute;
           color:yellow;
           left:0;
           top:0;
           z-index:2;
           -webkit-mask-image:-webkit-gradient(linear, 0 0, 0 bottom, from(yellow), to(rgba(0, 0, 255, 0)));
           }
           .gradient-text-three{
           fill:url(#SVGID_1_);
           font-size:40px;
           font-weight:bolder;
           }
           </style>
          </head>
          <body>
           <section class="wrapper">
           <div class="panel panel-info">
           <div class="panel-heading">
           <h3 class="panel-title">方法1. background-clip + text-fill-color</h3>
           </div>
           <div class="panel-body">
           <h3 class="gradient-text gradient-text-one">花樣年華</h3>
           </div>
           </div>
           <div class="panel panel-warning">
           <div class="panel-heading">
           <h3 class="panel-title">方法2. mask-image</h3>
           </div>
           <div class="panel-body">
           <h3 class="gradient-text gradient-text-two" data-content="豆蔻年華">豆蔻年華</h3>
           </div>
           </div>
           <div class="panel panel-danger">
           
           <div class="panel-heading">
           <h3 class="panel-title">方法3. svg linearGradient</h3>
           </div>
           
           <div class="panel-body">
           <svg viewBoxs="0 0 500 300" class="svgBox">
           <defs>
           <linearGradient id="SVGID_1_" gradientUnits="userSpaceOnUse" x1="0" y1="10" x2="0" y2="50">
           <stop offset="0" style="stop-color:yellow"/>
           <stop offset="0.5" style="stop-color:#fd8403"/>
           <stop offset="1" style="stop-color:red"/>
           </linearGradient>
           </defs>
           <text text-anchor="middle" class="gradient-text-three" x="110px" y="30%">花信年華</text>
           </svg>
           </div>
           
           </div>
           </section>
          </body>
          </html>
          

          效果:

          這里推薦一下我的前端技術(shù)分享群:731771211,里面都是學(xué)習(xí)前端的,如果你想制作酷炫的網(wǎng)頁,想學(xué)習(xí)編程。自己整理了一份2018最全面前端學(xué)習(xí)資料,從最基礎(chǔ)的HTML+CSS+JS【炫酷特效,游戲,插件封裝,設(shè)計模式】到移動端HTML5的項目實戰(zhàn)的學(xué)習(xí)資料都有整理,送給每一位前端小伙伴,有想學(xué)習(xí)web前端的,或是轉(zhuǎn)行,或是大學(xué)生,還有工作中想提升自己能力的,正在學(xué)習(xí)的小伙伴歡迎加入學(xué)習(xí)。


          主站蜘蛛池模板: 91视频一区二区三区| 国产精品视频一区二区三区| 日韩精品一区二区三区色欲AV| 理论亚洲区美一区二区三区| 国产成人AV一区二区三区无码 | 久久精品国产第一区二区三区| 韩国一区二区三区| 中文字幕无线码一区二区| 精品视频一区二区三区免费| 国产伦一区二区三区高清| 亚洲AV网一区二区三区| 国产短视频精品一区二区三区| 日本免费一区二区久久人人澡| 国产在线精品一区二区高清不卡| 精品视频一区在线观看| 久热国产精品视频一区二区三区 | 奇米精品视频一区二区三区| 亚洲人成网站18禁止一区| 国产在线观看一区二区三区四区| 无码人妻一区二区三区免费| 99精品国产一区二区三区2021| 亚洲制服丝袜一区二区三区| 亚洲一区二区三区日本久久九| 无码人妻AⅤ一区二区三区| 另类一区二区三区| 久久91精品国产一区二区| 日本免费一区二区三区| 精品欧洲av无码一区二区三区| 日韩一区二区在线播放| 波多野结衣一区二区三区 | 无码人妻精品一区二区三区夜夜嗨 | 精品成人av一区二区三区| 538国产精品一区二区在线| 国产在线视频一区| 亚洲日本va午夜中文字幕一区| 亚洲一区二区三区高清不卡| 美日韩一区二区三区| 色噜噜一区二区三区| 精品视频一区二区三区免费| 99精品国产高清一区二区麻豆| 国产日韩精品一区二区在线观看播放|