您好,欢迎来到555游戏网。
搜索
您的当前位置:首页详解HTML5Canvas绘制时指定颜色与透明度的方法_html5教程技巧

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

来源:555游戏网
 指定颜色

黑色是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, )'; // 绿
  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, , 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. 全局透明
  2. "canvas-warp">
  3. 你的浏览器居然不支持Canvas?!赶快换一个吧!!
  4. window.onload = function(){
  5. var canvas = document.getElementById("canvas");
  6. canvas.width = 800;
  7. canvas.height = 600;
  8. var context = canvas.getContext("2d");
  9. context.fillStyle = "#FFF";
  10. context.fillRect(0,0,800,600);
  11. context.globalAlpha = 0.5;
  12. for(var i=0; i<=50; i++){
  13. var R = Math.floor(Math.random() * 255);
  14. var G = Math.floor(Math.random() * 255);
  15. var B = Math.floor(Math.random() * 255);
  16. context.fillStyle = "rgb(" + R + "," + G + "," + B + ")";
  17. context.beginPath();
  18. context.arc(Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 100, 0, Math.PI * 2);
  19. context.fill();
  20. }
  21. };

运行结果:

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

Copyright © 2019- 555spj.com 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务