Chinaunix首页 | 论坛 | 博客
  • 博客访问: 326312
  • 博文数量: 206
  • 博客积分: 1040
  • 博客等级: 少尉
  • 技术积分: 1756
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-09 17:22
文章分类

全部博文(206)

文章存档

2015年(3)

2014年(147)

2013年(2)

2012年(54)

我的朋友

分类: HTML5

2014-02-11 15:15:29

1、方块移动

点击(此处)折叠或打开

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>

  4. <style>
  5.         body{background:gray}
  6.     #c1{background:white}
  7.     span{color:white}
  8. </style>
  9. <script language="javascript">
  10. window.onload=function(){
  11.     var oc=document.getElementById('c1');
  12.     var ogc=oc.getContext('2d');
  13.     var num=0;
  14.     ogc.fillRect(0,0,100,100);
  15.     setInterval(function(){
  16.         num++;
  17.         ogc.clearRect(0,0,oc.width,oc.Height);
  18.         ogc.fillRect(num,num,100,100);
  19.         },30)
  20.     }
  21. </script>
  22. </head>
  23. <body>
  24. <canvas id="c1" width="400" height="400"></canvas>
  25. </body>
  26. </html>
2、随鼠标划线

点击(此处)折叠或打开

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <style>
  5.     body{background:gray}
  6.     #c1{background:white}
  7.     span{color:white}
  8. </style>
  9. <script language="javascript">
  10. window.onload=function(){
  11.     var oc=document.getElementById('c1');
  12.     var ogc=oc.getContext('2d');
  13.     oc.onmousedown=function(ev){
  14.         var ev=ev||window.event;
  15.         ogc.moveTo(ev.clientX-oc.offsetLeft,ev.clientY-oc.offsetTop);
  16.         document.onmousemove=function(ev){
  17.             var ev=ev||window.event;
  18.             ogc.lineTo(ev.clientX-oc.offsetLeft,ev.clientY-oc.offsetTop);
  19.             ogc.stroke();
  20.             };
  21.         document.onmouseup=function(){
  22.             document.onmouseup=null;
  23.             document.onmousemove=null;
  24.             };
  25.         }
  26.     }
  27. </script>
  28. </head>
  29. <body>
  30. <canvas id="c1" width="400" height="400"></canvas>
  31. </body>
  32. </html>
3、
阅读(304) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~