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

2019-10-24

指定颜色

黑色是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格式文档,以方便收藏与打印。