登录  /  注册
首页 > web前端 > H5教程 > 正文

详解HTML5 Canvas绘制时指定颜色与透明度的方法_html5教程技巧

php中文网
发布: 2016-05-16 15:45:15
原创
1962人浏览过

指定颜色

黑色是Canvas绘制的默认色彩,要想换一种颜色的话,就得在实际画之前指定颜色。

JavaScript Code复制内容到剪贴板
  1. ctx.strokeStyle = color   

指定绘制线的颜色:

JavaScript Code复制内容到剪贴板
  1. ctx.fillStyle = color   

指定填充的颜色:
来看看实际的例子:

JavaScript

JavaScript Code复制内容到剪贴板
  1. onload = function() {   
  2.   draw();   
  3. };   
  4. function draw() {   
  5.   var canvas = document.getElementById('c1');   
  6.   if ( ! canvas || ! canvas.getContext ) { return false; }   
  7.   var ctx = canvas.getContext('2d');   
  8.   ctx.beginPath();   
  9.   ctx.fillStyle = 'rgb(192, 80, 77)'// 红   
  10.   ctx.arc(70, 45, 35, 0, Math.PI*2, false);   
  11.   ctx.fill();   
  12.   ctx.beginPath();   
  13.   ctx.fillStyle = 'rgb(155, 187, 89)'// 绿   
  14.   ctx.arc(45, 95, 35, 0, Math.PI*2, false);   
  15.   ctx.fill();   
  16.   ctx.beginPath();   
  17.   ctx.fillStyle = 'rgb(128, 100, 162)'// 紫   
  18.   ctx.arc(95, 95, 35, 0, Math.PI*2, false);   
  19.   ctx.fill();   
  20. }  

效果如下图:
2016325112217008.png (142×142)

指定透明度

和普通的CSS中一样,我们指定颜色的时候还可以带一个alpha值(不过用的不多,IE9之前都不支持)。看代码:

JavaScript

JavaScript Code复制内容到剪贴板
  1. onload = function() {   
  2.   draw();   
  3. };   
  4. function draw() {   
  5.   var canvas = document.getElementById('c1');   
  6.   if ( ! canvas || ! canvas.getContext ) { return false; }   
  7.   var ctx = canvas.getContext('2d');   
  8.   ctx.beginPath();   
  9.   ctx.fillStyle = 'rgba(192, 80, 77, 0.7)'//   
  10.   ctx.arc(70, 45, 35, 0, Math.PI*2, false);   
  11.   ctx.fill();   
  12.   ctx.beginPath();   
  13.   ctx.fillStyle = 'rgba(155, 187, 89, 0.7)'//   
  14.   ctx.arc(45, 95, 35, 0, Math.PI*2, false);   
  15.   ctx.fill();   
  16.   ctx.beginPath();   
  17.   ctx.fillStyle = 'rgba(128, 100, 162, 0.7)'//   
  18.   ctx.arc(95, 95, 35, 0, Math.PI*2, false);   
  19.   ctx.fill();   
  20. }   

结果就是下面这样:
2016325112248089.png (142×142)

和上面的代码基本没变化,就是把rgb(r, g, b)变成了rgba(r, g, b, a)而已,a的值也是0~1,0表示完全透明,1则是完全不透明(所以alpha的值实际上是“不透明度”)。


全局透明globalAlpha
这个也是很简单的一个属性,默认值为1.0,代表完全不透明,取值范围是0.0(完全透明)~1.0。这个属性与阴影设置是一样的,如果不想针对全局设置不透明度,就得在下次绘制前重置globalAlpha。

总结一下:基于状态的属性有哪些?

——globalAlpha

——globalCompositeOpeartion

——strokeStyle

——textAlign,textBaseline

——lineCap,lineJoin,lineWidth,miterLimit

——fillStyle

——font

——shadowBlur,shadowColor,shadowOffsetX,shadowOffsetY
我们通过一个代码,来体验一下globalAlpha的神奇之处~

JavaScript Code复制内容到剪贴板
  1. nbsp;html>   
  2. "zh">   
  3.   
  4.      "UTF-8">   
  5.     全局透明   
  6.     
  7.         body { background: url("./images/bg3.jpg") repeat; }  
  8.         #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; }   
  9.        
  10.   
  11.   
  12. "canvas-warp">   
  13.     "canvas">   
  14.         你的浏览器居然不支持Canvas?!赶快换一个吧!!   
  15.        
  
  •   
  • <script> </script>
  •     window.onload = function(){   
  •         var canvas = document.getElementById("canvas");   
  •         canvas.width = 800;   
  •         canvas.height = 600;   
  •         var context = canvas.getContext("2d");   
  •         context.fillStyle = "#FFF";   
  •         context.fillRect(0,0,800,600);   
  •   
  •         context.globalAlpha = 0.5;   
  •   
  •         for(var i=0; i
  •             var R = Math.floor(Math.random() * 255);   
  •             var G = Math.floor(Math.random() * 255);   
  •             var B = Math.floor(Math.random() * 255);   
  •   
  •             context.fillStyle = "rgb(" + R + "," + G + "," + B + ")";   
  •   
  •             context.beginPath();   
  •             context.arc(Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 100, 0, Math.PI * 2);   
  •             context.fill();   
  •         }   
  •     };   
  •   
  •   
  •   
  • 运行结果:
    2016325112320763.jpg (850×500)

    是不是非常的酷?终于有点艺术家的范儿了吧。

    智能AI问答
    PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
    来源:php中文网
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    关于CSS思维导图的课件在哪? 课件
    凡人来自于2024-04-16 10:10:18
    热门推荐
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习
    PHP中文网抖音号
    发现有趣的

    Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号