详解HTML5 Canvas绘制时指定颜色与透明度的方法

2022-10-20,,,,

指定颜色

黑色是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. }  

效果如下图:

指定透明度

和普通的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. }   

结果就是下面这样:

和上面的代码基本没变化,就是把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. <!doctype html>   
  2. <html lang="zh">   
  3. <head>   
  4.     <meta charset="utf-8">   
  5.     <title>全局透明</title>   
  6.     <style>   
  7.         body { background: url("./images/bg3.jpg") repeat; }  
  8.         #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; }   
  9.     </style>   
  10. </head>   
  11. <body>   
  12. <div id="canvas-warp">   
  13.     <canvas id="canvas">   
  14.         你的浏览器居然不支持canvas?!赶快换一个吧!!   
  15.     </canvas>   
  16. </div>   
  17.   
  18. <script>   
  19.     window.onload = function(){   
  20.         var canvas = document.getelementbyid("canvas");   
  21.         canvas.width = 800;   
  22.         canvas.height = 600;   
  23.         var context = canvas.getcontext("2d");   
  24.         context.fillstyle = "#fff";   
  25.         context.fillrect(0,0,800,600);   
  26.   
  27.         context.globalalpha = 0.5;   
  28.   
  29.         for(var i=0; i<=50; i++){   
  30.             var r = math.floor(math.random() * 255);   
  31.             var g = math.floor(math.random() * 255);   
  32.             var b = math.floor(math.random() * 255);   
  33.   
  34.             context.fillstyle = "rgb(" + r + "," + g + "," + b + ")";   
  35.   
  36.             context.beginpath();   
  37.             context.arc(math.random() * canvas.width, math.random() * canvas.height, math.random() * 100, 0, math.pi * 2);   
  38.             context.fill();   
  39.         }   
  40.     };   
  41. </script>   
  42. </body>   
  43. </html>  

运行结果:

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

《详解HTML5 Canvas绘制时指定颜色与透明度的方法.doc》

下载本文的Word格式文档,以方便收藏与打印。