手机版
你好,游客 登录 注册
背景:
阅读新闻

使用HTML5的canvas做一个会动的时钟

[日期:2012-03-19] 来源:Linux社区  作者:shyleoking [字体:大 中 小]
mitre的限定
  1. $(  
  2. function() {  
  3.     var canvas = document.getElementById("mycanvas");  
  4.     var context = canvas.getContext("2d");  
  5.     context.strokeStyle = "rgb(255, 0, 0)";  
  6.     context.lineWidth = 10;  
  7.     context.shadowColor = "#0000FF";  
  8.   
  9.   
  10.     context.beginPath();  
  11.     context.miterLimit = 1; //miterLimit 属性为斜面的长度设置一个上限。   
  12.     //只对线条使用设置为 "miter" 的 lineJoin 属性绘制并且两条线段以锐角相交的时候有效   
  13.     context.lineJoin = "miter"; //两条线段的外边缘一直扩展到它们相交   
  14.     context.moveTo(10, 70);  
  15.     context.lineTo(50, 10);  
  16.     context.lineTo(80, 70);  
  17.     context.stroke();  
  18.   
  19. }  
  20. );  

各种几何图形

  1. $(  
  2. function() {  
  3.     var canvas = document.getElementById("mycanvas");  
  4.     canvas.height = 500; //改变默认高度   
  5.     canvas.width = 500;  
  6.     var context = canvas.getContext("2d");  
  7.     context.strokeStyle = "rgb(255, 0, 0)";  
  8.     context.fillStyle = "#AABBCC";  
  9.     context.lineWidth = 2;  
  10.     context.shadowColor = "#0000FF";  
  11.   
  12.     //矩形   
  13.     context.beginPath();  
  14.     context.fillRect(10, 10, 50, 50); //实体矩形:x,y,width,height   
  15.     context.strokeRect(70, 10, 50, 50)//空心矩形:x,y,width,height   
  16.   
  17.     //context.move(10,100);   
  18.   
  19.     //圆弧:x, y, radius, startAngle, endAngle, anticlockwise   
  20.     context.beginPath();  
  21.     context.arc(35, 110, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  22.     context.stroke();  
  23.     //context.closePath();   
  24.   
  25.     context.beginPath();  
  26.     context.arc(85, 110, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 180, false);  
  27.     context.stroke();  
  28.     //context.closePath();   
  29.   
  30.     context.beginPath();  
  31.     context.arc(135, 110, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 180, true);  
  32.     context.stroke();  
  33.     //context.closePath();   
  34.   
  35.     context.beginPath();  
  36.     context.arc(185, 110, 25, (Math.PI / 180) * 180, (Math.PI / 180) * 360, true);  
  37.     context.stroke();  
  38.     //context.closePath();   
  39.   
  40.     context.beginPath();  
  41.     context.arc(235, 110, 25, (Math.PI / 180) * 90, (Math.PI / 180) * 0, false);  
  42.     context.fillStyle = "blue";  
  43.     context.fill();  
  44.     //context.stroke();   
  45.     //context.closePath();   
  46.   
  47.     context.beginPath();  
  48.     context.arc(285, 110, 25, (Math.PI / 180) * 180, (Math.PI / 180) * 45, false);  
  49.     context.closePath();  
  50.     context.stroke();  
  51.   
  52.     context.beginPath();  
  53.     context.arc(335, 110, 25, (Math.PI / 180) * 180, (Math.PI / 180) * 45, false);  
  54.     context.closePath();  
  55.     context.fillStyle = "blue";  
  56.     context.fill();  
  57.     context.stroke();  
  58.   
  59.     //曲线   
  60.   
  61.   
  62.     context.beginPath();  
  63.     context.moveTo(10, 160); //二次贝塞尔曲线的起始点   
  64.     //controlX, controlY, endX, endY   
  65.     context.quadraticCurveTo(70, 280, 235, 140);  
  66.     context.stroke();  
  67.     context.closePath();  
  68.   
  69.     context.beginPath();  
  70.     context.moveTo(10, 300); //三次贝塞尔曲线的起始点   
  71.     //controlX1, controlY1, controlX2, controlY2, endX, endY   
  72.     context.bezierCurveTo(70, 280, 50, 400, 235, 190);  
  73.     context.stroke();  
  74.     context.closePath();  
  75.   
  76.   
  77. }  
  78. );  
各种变换

记得CSS3中的transform不?canvas肯定也有啊

平移

  1. $(  
  2. function() {  
  3.     var canvas = document.getElementById("mycanvas");  
  4.     canvas.height = 500; //改变默认高度   
  5.     canvas.width = 500;  
  6.     var context = canvas.getContext("2d");  
  7.     context.strokeStyle = "rgb(255, 0, 0)";  
  8.     context.fillStyle = "#AABBCC";  
  9.     context.lineWidth = 2;  
  10.     context.shadowColor = "#0000FF";  
  11.     context.moveTo(10, 10);  
  12.     //context.beginPath();   
  13.     //context.beginPath();   
  14.     context.fillRect(10, 10, 50, 50); //实体矩形:x,y,width,height   
  15.     //context.stroke();   
  16.     $(canvas).on(  
  17.     "click",  
  18.     { "context": context },  
  19.     function(e) {  
  20.         $.log(e.data.context);  
  21.         var ctx = e.data.context;  
  22.         ctx.translate(10, 10); //再最后的路径点上偏移10*10的位置   
  23.         context.fillRect(10, 10, 50, 50);  
  24.         context.stroke();  
  25.     }  
  26.     );  
  27. }  
  28. );  
缩放
  1. $(  
  2. function() {  
  3.     var canvas = document.getElementById("mycanvas");  
  4.     canvas.height = 500; //改变默认高度   
  5.     canvas.width = 500;  
  6.     var context = canvas.getContext("2d");  
  7.     context.strokeStyle = "rgb(255, 0, 0)";  
  8.     context.fillStyle = "#AABBCC";  
  9.     context.lineWidth = 2;  
  10.     context.shadowColor = "#0000FF";  
  11.     context.moveTo(10, 10);  
  12.     //context.beginPath();   
  13.     //context.beginPath();   
  14.     context.fillRect(10, 10, 50, 50); //实体矩形:x,y,width,height   
  15.     //context.stroke();   
  16.     $(canvas).on(  
  17.     "click",  
  18.     { "context": context },  
  19.     function(e) {  
  20.         $.log(e.data.context);  
  21.         var ctx = e.data.context;  
  22.         ctx.scale(1.1, 1.1); //在最后的大小基础上缩放倍数 必须是正数   
  23.         context.fillRect(10, 10, 50, 50);  
  24.         context.stroke();  
  25.     }  
  26.     );  
  27. }  
  28. );  

旋转

  1. $(  
  2. function() {  
  3.     var canvas = document.getElementById("mycanvas");  
  4.     canvas.height = 500; //改变默认高度   
  5.     canvas.width = 500;  
  6.     var context = canvas.getContext("2d");  
  7.     context.strokeStyle = "rgb(255, 0, 0)";  
  8.     context.fillStyle = "#AABBCC";  
  9.     context.lineWidth = 2;  
  10.     context.shadowColor = "#0000FF";  
  11.     context.moveTo(10, 10);  
  12.     //context.beginPath();   
  13.     //context.beginPath();   
  14.     context.fillRect(10, 10, 50, 50); //实体矩形:x,y,width,height   
  15.     //context.stroke();   
  16.     $(canvas).on(  
  17.     "click",  
  18.     { "context": context },  
  19.     function(e) {  
  20.         $.log(e.data.context);  
  21.         var ctx = e.data.context;  
  22.         ctx.rotate((Math.PI / 180) * 10); //旋转的角度,旋转的中心是canvas坐标原点   
  23.         context.fillRect(10, 10, 50, 50);  
  24.         context.stroke();  
  25.     }  
  26.     );  
  27. }  
  28. );  
transform,transform的参数比较多,也比较难理解,简单的说transform是最自由的变形方式,下面给出些参考
  1. //以下两段代码结果一致   
  2. context.transform(1, 0, 0, 1, 10, 10)  
  3. context.translate(10, 10);  
  4.   
  5. //以下两段代码结果一致   
  6. context.transform(10, 0, 0, 10, 0, 0);  
  7. context.scale(10, 10);  
  8.   
  9. //以下三段代码结果一致   
  10. context.transform(Math.cos((Math.PI / 180) * 10), Math.sin((Math.PI / 180) * 10), -Math.sin((Math.PI / 180) * 10), Math.cos((Math.PI / 180)) * 10, 0, 0);  
  11. context.transform(-Math.sin((Math.PI/180)*10),Math.cos((Math.PI/180)*10),Math.cos((Math.PI/180)*10),Math.sin((Math.PI/180)*10), 0,0);  
  12. context.rotate(10);  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款