其他分享
首页 > 其他分享> > canvas文字与阴影

canvas文字与阴影

作者:互联网

基本属性

 var oC = document.getElementById("canvas")
    var oGC =oC.getContext("2d") 
    oGC.font ='30px impact'
    oGC.textBaseline = 'top';
    oGC.fillText('text', 20, 20);
    oGC.strokeText('text',0,200)

文字居中

var w =measureText('text').width() 获取文字的宽度

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      body {
        background: #000000;
      }
      canvas {
        background: #ffffff;
        transition: translate 0.5s;
      }
    </style>
  </head>
  <body>
    <canvas  id="canvas" width="400" height="400"></canvas>
  </body>
  <script>
    var oC = document.getElementById("canvas")
    var oGC =oC.getContext("2d") 
   oGC.font ="50px impact"
   oGC.textBaseline ="top"
   var w =oGC.measureText('text').width/2
   oGC.fillText('text',(200-w), (200-30));
   
  </script>
</html>

阴影

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      body {
        background: #000000;
      }
      canvas {
        background: #ffffff;
        transition: translate 0.5s;
      }
    </style>
  </head>
  <body>
    <canvas  id="canvas" width="400" height="400"></canvas>
  </body>
  <script>
    var oC = document.getElementById("canvas")
    var oGC =oC.getContext("2d") 
   oGC.font ="50px impact"
   oGC.textBaseline ="top"
   oGC.shadowOffsetX=10
   oGC.shadowOffsetY=-4
   oGC.shadowColor='red'
   var w =oGC.measureText('text').width/2
   oGC.fillText('text',(200-w), (200-30));
   
  </script>
</html>

 

标签:文字,200,canvas,text,oC,阴影,var,oGC
来源: https://blog.csdn.net/weixin_41069726/article/details/89433331