绘制要点:  

  1.getContext('2d');    -->绘图环境,2维空间

  2.fillRect(x,y,w,h);    -->矩形:实心(黑色背景)

  3.strokeRect(x,y,w,h);    -->矩形:空心(白色背景)

  4.clearRect(x,y,w,h);    -->矩形:在已存在的矩形中挖空一个矩形

  5.ctx.fillStyle='红色';    -->给图添加颜色

代码如下:

<canvas id="cavas" width="" height="" style="border: 1px solid;"></canvas>
<!--高宽不能写在style里-->
    var cas=document.querySelector('#cavas');    var ctx=cas.getContext('2d');
var n;
function rgb(){
var r=Math.floor(Math.random()*);
var g=Math.floor(Math.random()*);
var b=Math.floor(Math.random()*);
return 'rgb('+r+','+g+','+b+')';
}
setInterval(function(){ //定时器
for (var i=;i<;i++) {
for (var j=;j<;j+=) { //循环行
i%==?n=j:n=j+;    //如果是偶数行,n=j,如果是奇数行,n=j+1
ctx.fillStyle=rgb(); //添加颜色
ctx.fillRect(*n,*i,,);
}
}
},);
  //也可以用下面的方法
   setInterval(function(){      //定时器
for (var i=0;i<8;i++) {
for (var j=0;j<8;j++) { //循环行

          if (i%2==0&&j%2==0) {    //偶数行
            ctx.fillRect(100*i,100*j,100,100);
          } else if(i%2!=0){     //奇数行
            ctx.fillRect(100*i,100*(2*j+1),100,100);
          }
          ctx.fillStyle=rgb();

            }
}
},200);

效果(代码有个定时器会不停的闪动):

最新文章

  1. 监控Activity的启动等状态--- 源码级
  2. Uva 1629 切蛋糕
  3. VS2010 中 Entity Framework 多数据库并存方法
  4. HP PCS 云监控大数据解决方案
  5. IOS第18天(4,核心动画,时钟效果,定时器,图片旋转角度,CALayer 锚点,获取当前,小时,秒,分)
  6. ORA-00265: instance recovery required, cannot set ARCHIVELOG
  7. 使用ApplicationLoader中出现报错:The IPA is invalid. It does not inlude a Payload directory
  8. URLEncode和URLDecoder作用
  9. verilog抓外部低频输入信号的上升沿和下降沿
  10. 纯手写AJAX
  11. configparser模块--配置文件
  12. 3、jeecg 笔记之 模糊查询
  13. 2、使用Angular-CLI初始化Angular项目(踩过的深坑!!!)
  14. Annotation 标注
  15. C#基础巩固(3)-Linq To XML 读取XML
  16. @Autowired与@Resource 详细诠释和区别(附带例子)
  17. 数据科学的完整学习路径—Python版(转载)
  18. UVa 10298 - Power Strings
  19. Spring Boot 中使用 jpa
  20. ftp之心脏病

热门文章

  1. c++中STL中的next_permutation函数基本用法
  2. asp.net mvc AjaxHelper 获取 JSON 的方法
  3. 在Pycharm上编写WordCount程序
  4. Python读写操作Excel模块_xlrd_xlwt_xlutils
  5. Win10家庭版组策略gpedit.msc的问题
  6. WCF rest 的帮助页面和缓存机制
  7. spark常用算子总结
  8. sqlserver 2012 部署详解
  9. JAVA高并发秒杀API项目的学习笔记
  10. Murano为镜像包添加Root用户密码