canvas基础—03文本和阴影

1.文本1

 demo.html

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>canvas学习</title>
<style>
body{background: black}
#c1{background: white}
span{color: white}
</style> 
<script type="text/javascript">
    window.onload = function(){
        var oC = document.getElementById('c1');
        var oGC = oC.getContext('2d');

        oGC.font = '60px impact' //注意这块,后面必须指定文字,任意文字都可,如果不想,则写'impact' 

        oGC.textBaseline = 'top' //文字垂直方向以哪个为基准点  也可以是(middle,bottom)
        oGC.fillText('前端学习',0,0) //(文字,坐标x,坐标y)

        oGC.strokeText('前端学习',0,200) //空心的字体 
        
    }
</script>
</head>
<body>

<canvas id="c1" width="400" height="400">
	<span>不支持canvas浏览器</span>
</canvas>

</body>
</html>

 2.文本2

demo.html

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>canvas学习</title>
<style>
body{background: black}
#c1{background: white}
span{color: white}
</style> 
<script type="text/javascript">
    window.onload = function(){
        var oC = document.getElementById('c1');
        var oGC = oC.getContext('2d');

        oGC.font = '60px impact' //注意这块,后面必须指定文字,任意文字都可,如果不想,则写'impact' 

        oGC.textBaseline = 'top' //文字垂直方向以哪个为基准点  也可以是(middle,bottom)

        var w = oGC.measureText('前端学习').width
        // 注意文字的高其实就是文字的大小,也就是60px
        oGC.fillText('前端学习',(oC.width-w)/2,(oC.height-60)/2) //(文字,坐标x,坐标y)
        
    }
</script>
</head>
<body>

<canvas id="c1" width="400" height="400">
	<span>不支持canvas浏览器</span>
</canvas>

</body>
</html>

 

 3.阴影

demo.html

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>canvas学习</title>
<style>
body{background: black}
#c1{background: white}
span{color: white}
</style> 
<script type="text/javascript">
    window.onload = function(){
        var oC = document.getElementById('c1');
        var oGC = oC.getContext('2d');

        oGC.font = '60px impact' //注意这块,后面必须指定文字,任意文字都可,如果不想,则写'impact' 

        oGC.textBaseline = 'top' //文字垂直方向以哪个为基准点  也可以是(middle,bottom)
        
        oGC.shadowOffsetX = 10
        oGC.shadowOffsetY = 20
        oGC.shadowBlur = 5

        oGC.shadowColor = 'yellow' //默认颜色黑色透明  rgba(0,0,0,0)

        var w = oGC.measureText('前端学习').width
        // 注意文字的高其实就是文字的大小,也就是60px
        oGC.fillText('前端学习',(oC.width-w)/2,(oC.height-60)/2) //(文字,坐标x,坐标y)
        
    }
</script>
</head>
<body>

<canvas id="c1" width="400" height="400">
	<span>不支持canvas浏览器</span>
</canvas>

</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值