最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • Canvas-字体

    正文概述 掘金(辰酱czklove)   2021-06-11   655

    Canvas - 字体

    浏览器支持

    Internet Explorer 9、Firefox、Opera、Chrome 以及 Safari 支持 fillStyle 属性。

    注释:Internet Explorer 8 以及更早的版本不支持 元素。

    Canvas - 描边字体

          // 定义描边颜色
          this.ctx.strokeStyle = 'red'
          this.ctx.font = 'bold 32px serif'
          this.ctx.strokeText('hello word', 120, 130)
    

    Canvas-字体

    Canvas - 填充字体

          // 定义填充颜色
          this.ctx.strokeStyle = 'red'
                this.ctx.font = 'bold 64px serif'
                this.ctx.fillText('hello word', 120, 130)
    

    Canvas - 字体样式,类型,大小,粗细

          // 定义填充颜色
          this.ctx.strokeStyle = 'red'
                this.ctx.font = 'bold 64px serif'
                this.ctx.fillText('hello word', 120, 130)
    

    Canvas - 字体水平对齐方式

    Canvas - 字体垂直对齐方式

    this.ctx.fillStyle = 'red'
                this.ctx.font = 'bold 32px serif'
                this.ctx.textAlign = 'left'
                this.ctx.fillText('hello', 120, 130)
                this.ctx.fillStyle = 'blue'
                this.ctx.textAlign = 'right'
                this.ctx.fillText('hello', 120, 130)
                this.ctx.fillStyle = 'green'
                this.ctx.textAlign = 'center'
                this.ctx.fillText('hello', 120, 130)
                this.ctx.font = 'bold 32px serif'
                this.ctx.fillStyle = 'red'
                this.ctx.textBaseline = 'alphabetic'
                this.ctx.fillText('alphabetic', 320, 130)
                this.ctx.fillStyle = 'blue'
                this.ctx.textBaseline = 'top'
                this.ctx.fillText('top', 350, 130)
                this.ctx.fillStyle = 'green'
                this.ctx.textBaseline = 'hanging'
                this.ctx.fillText('hanging', 380, 130)
                this.ctx.fillStyle = 'red'
                this.ctx.textBaseline = 'middle'
                this.ctx.fillText('middle', 410, 130)
                this.ctx.fillStyle = 'blue'
                this.ctx.textBaseline = 'ideographic'
                this.ctx.fillText('ideographic', 440, 130)
                this.ctx.fillStyle = 'green'
                this.ctx.textBaseline = 'bottom'
                this.ctx.fillText('bottomq', 570, 130)
    

    Canvas-字体

    Canvas - 字体透明

     // 每点击一次透明度都会减一
                this.ctx.clearRect(0, 0, 800, 800)
                this.globalAlphaValue -= 0.1
                this.ctx.globalAlpha = this.globalAlphaValue
                this.ctx.fillStyle = 'red'
                this.ctx.font = 'bold 64px serif'
                this.ctx.fillText('hello word', 120, 130)
    

    Canvas-字体

    Canvas - 字体阴影

    this.ctx.shadowColor = 'green'
                this.ctx.shadowOffsetX = '4'
                this.ctx.shadowOffsetY = '4'
                this.ctx.shadowBlur = '4'
                this.ctx.fillStyle = 'red'
                this.ctx.font = 'bold 64px serif'
                this.ctx.fillText('hello word', 120, 130)
    

    Canvas-字体

    Canvas - 文本渐变

    // 水平颜色渐变
    let gr = this.ctx.createLinearGradient(60, 0, 430, 0) //初始化渐变
                gr.addColorStop(0, 'rgb(255, 0, 0)') // 初始位置
                gr.addColorStop(0.5, 'rgb(0, 255, 0)') // 中间位置
                gr.addColorStop(1, 'rgb(255, 0, 0)') // 结束位置
                this.ctx.fillStyle = gr
                this.ctx.font = 'bold 64px serif'
                this.ctx.fillText('hello word', 60, 130)
    // 垂直颜色渐变
                gr = this.ctx.createLinearGradient(0, 100, 0, 130) //初始化渐变
                gr.addColorStop(0, 'rgb(255, 0, 0)') // 初始位置
                gr.addColorStop(0.5, 'rgb(0, 255, 0)') // 中间位置
                gr.addColorStop(1, 'rgb(255, 0, 0)') // 结束位置
                this.ctx.fillStyle = gr
                this.ctx.font = 'bold 64px serif'
                this.ctx.fillText('hello word', 420, 130)
    
    

    Canvas-字体

    Canvas - 动态文字动画

    // 画动画
    darwScreen() {
                // 设置画布背景
                this.ctx.fillStyle = '#333'
                this.ctx.fillRect(0, 0, 800, 600)
                this.ctx.font = '90px impact'
                this.ctx.textAlign = 'center'
                this.ctx.textBaseline = 'middle'
                let metrics = this.ctx.measureText('hello word')
                let textWidth = metrics.width
                let xposition = 400
                let yposition = 300
                // 设置渐变
                let gr = this.ctx.createLinearGradient(400, 0, 400, 600) //初始化渐变
                for (let i = 0; i < this.colorStops.length; i++) {
                    let tempColorStop = this.colorStops[i]
                    let tempColor = tempColorStop.color
                    let tempStopPercent = tempColorStop.stopPercent
                    gr.addColorStop(tempStopPercent, tempColor)
                    tempStopPercent += 0.015
                    if (tempStopPercent > 1) {
                        tempStopPercent = 0
                    }
                    tempColorStop.stopPercent = tempStopPercent
                    this.colorStops[i] = tempColorStop
                }
                this.ctx.fillStyle = gr
                this.ctx.fillText('hello word', xposition, yposition)
            }
            // 循环动画
    filltextdh() {
                let self = this
                ;(function frame() {
                    window.requestAnimationFrame(frame)
                    self.darwScreen()
                })()
            },
    

    Canvas-字体


    起源地下载网 » Canvas-字体

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元