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

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

[日期:2012-03-19] 来源:Linux社区  作者:shyleoking [字体:大 中 小]
组合
  1. $(  
  2. function() {  
  3.     var canvas = document.getElementById("mycanvas");  
  4.     canvas.height = 100;  
  5.     canvas.width = 100;  
  6.     var context = canvas.getContext("2d");  
  7.     context.fillStyle = "#AABBCC";  
  8.     context.fillRect(10, 10, 50, 50);  
  9.     //默认 新图形会覆盖在原有内容之上   
  10.     context.globalCompositeOperation = "source-over";  
  11.     context.fillStyle = "blue";  
  12.     context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  13.     context.fill();  
  14.     $("span").html(context.globalCompositeOperation);  
  15.     $(canvas).toggle(  
  16.     function() {  
  17.         canvas.width = 100;  
  18.         // 原有内容之下绘制新图形   
  19.         context.clearRect(0, 0, 500, 500);  
  20.         context.beginPath();  
  21.         context = canvas.getContext("2d");  
  22.         context.fillStyle = "#AABBCC";  
  23.         context.fillRect(10, 10, 50, 50);  
  24.         context.globalCompositeOperation = "destination-over";  
  25.         context.fillStyle = "blue";  
  26.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  27.         context.fill();  
  28.         $("span").html(context.globalCompositeOperation);  
  29.     },  
  30.     function() {  
  31.         canvas.width = 100;  
  32.         //新图形会仅仅出现与原有内容重叠的部分。其它区域都变成透明的   
  33.         context.clearRect(0, 0, 500, 500);  
  34.         context.beginPath();  
  35.         context.fillStyle = "#AABBCC";  
  36.         context.fillRect(10, 10, 50, 50);  
  37.         context.globalCompositeOperation = "source-in";  
  38.         context.fillStyle = "blue";  
  39.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  40.         context.fill();  
  41.         $("span").html(context.globalCompositeOperation);  
  42.     },  
  43.     function() {  
  44.         canvas.width = 100;  
  45.         //原有内容中与新图形重叠的部分会被保留,其它区域都变成透明的destination-in   
  46.         context.clearRect(0, 0, 500, 500);  
  47.         context.beginPath();  
  48.         context = canvas.getContext("2d");  
  49.         context.fillStyle = "#AABBCC";  
  50.         context.fillRect(10, 10, 50, 50);  
  51.         context.globalCompositeOperation = "destination-in";  
  52.         context.fillStyle = "blue";  
  53.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  54.         context.fill();  
  55.         $("span").html(context.globalCompositeOperation);  
  56.   
  57.     },  
  58.     function() {  
  59.         canvas.width = 100;  
  60.         //只有新图形中与原有内容不重叠的部分会被绘制出来source-out   
  61.         context.clearRect(0, 0, 500, 500);  
  62.         context.beginPath();  
  63.         context = canvas.getContext("2d");  
  64.         context.fillStyle = "#AABBCC";  
  65.         context.fillRect(10, 10, 50, 50);  
  66.         context.globalCompositeOperation = "source-out";  
  67.         context.fillStyle = "blue";  
  68.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  69.         context.fill();  
  70.         $("span").html(context.globalCompositeOperation);  
  71.     },  
  72.     function() {  
  73.         canvas.width = 100;  
  74.         //原有内容中与新图形不重叠的部分会被保留   
  75.         context.clearRect(0, 0, 500, 500);  
  76.         context.beginPath();  
  77.         context = canvas.getContext("2d");  
  78.         context.fillStyle = "#AABBCC";  
  79.         context.fillRect(10, 10, 50, 50);  
  80.         context.globalCompositeOperation = "destination-out";  
  81.         context.fillStyle = "blue";  
  82.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  83.         context.fill();  
  84.         $("span").html(context.globalCompositeOperation);  
  85.     },  
  86.     function() {  
  87.         canvas.width = 100;  
  88.         //新图形中与原有内容重叠的部分会被绘制,并覆盖于原有内容之上   
  89.         context.clearRect(0, 0, 500, 500);  
  90.         context.beginPath();  
  91.         context = canvas.getContext("2d");  
  92.         context.fillStyle = "#AABBCC";  
  93.         context.fillRect(10, 10, 50, 50);  
  94.         context.globalCompositeOperation = "source-atop";  
  95.         context.fillStyle = "blue";  
  96.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  97.         context.fill();  
  98.         $("span").html(context.globalCompositeOperation);  
  99.     },  
  100.     function() {  
  101.         canvas.width = 100;  
  102.         //原有内容中与新内容重叠的部分会被保留,并会在原有内容之下绘制新图形   
  103.         context.clearRect(0, 0, 500, 500);  
  104.         context.beginPath();  
  105.         context = canvas.getContext("2d");  
  106.         context.fillStyle = "#AABBCC";  
  107.         context.fillRect(10, 10, 50, 50);  
  108.         context.globalCompositeOperation = "destination-atop";  
  109.         context.fillStyle = "blue";  
  110.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  111.         context.fill();  
  112.         $("span").html(context.globalCompositeOperation);  
  113.     },  
  114.     function() {  
  115.         canvas.width = 100;  
  116.         //两图形中重叠部分作加色处理   
  117.         context.clearRect(0, 0, 500, 500);  
  118.         context.beginPath();  
  119.         context = canvas.getContext("2d");  
  120.         context.fillStyle = "#AABBCC";  
  121.         context.fillRect(10, 10, 50, 50);  
  122.         context.globalCompositeOperation = "lighter";  
  123.         context.fillStyle = "blue";  
  124.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  125.         context.fill();  
  126.         $("span").html(context.globalCompositeOperation);  
  127.     },  
  128.     function() {  
  129.         canvas.width = 100;  
  130.         //两图形中重叠的部分作减色处理darker   
  131.         context.clearRect(0, 0, 500, 500);  
  132.         context.beginPath();  
  133.         context = canvas.getContext("2d");  
  134.         context.fillStyle = "#AABBCC";  
  135.         context.fillRect(10, 10, 50, 50);  
  136.         context.globalCompositeOperation = "darker";  
  137.         context.fillStyle = "blue";  
  138.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  139.         context.fill();  
  140.         $("span").html(context.globalCompositeOperation);  
  141.     },  
  142.     function() {  
  143.         canvas.width = 100;  
  144.         //重叠的部分会变成透明   
  145.         context.clearRect(0, 0, 500, 500);  
  146.         context.beginPath();  
  147.         context = canvas.getContext("2d");  
  148.         context.fillStyle = "#AABBCC";  
  149.         context.fillRect(10, 10, 50, 50);  
  150.         context.globalCompositeOperation = "xor";  
  151.         context.fillStyle = "blue";  
  152.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  153.         context.fill();  
  154.         $("span").html(context.globalCompositeOperation);  
  155.     },  
  156.     function() {  
  157.         canvas.width = 100;  
  158.         //只有新图形会被保留,其它都被清除掉   
  159.         context.clearRect(0, 0, 500, 500);  
  160.         context.beginPath();  
  161.         context = canvas.getContext("2d");  
  162.         context.fillStyle = "#AABBCC";  
  163.         context.fillRect(10, 10, 50, 50);  
  164.         context.globalCompositeOperation = "copy";  
  165.         context.fillStyle = "blue";  
  166.         context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);  
  167.         context.fill();  
  168.         $("span").html(context.globalCompositeOperation);  
  169.         alert("演示结束");  
  170.     }  
  171.     );  
  172. }  
  173. );  
字体(看文档说canvas的字体支持CSS样式的描写,但是,我不知道怎么样让canvas的font支持CSS3的在线字体)
  1. <script src="js/jquery-1.7.1.min.js" type="text/Javascript"></script>  
  2.   
  3. <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Tangerine">  
  4.   
  5. <script type="text/javascript">  
  6.     $.log = function(msg) {  
  7.         console.log(msg);  
  8.     }  
  9.   
  10.     $(  
  11.     function() {  
  12.         var canvas = document.getElementById("mycanvas");  
  13.         canvas.height = 200;  
  14.         canvas.width = 200;  
  15.         var context = canvas.getContext("2d");  
  16.         context.font = "20px 新宋体";  
  17.         context.fillText("这是实心新宋体", 10, 30);  
  18.         context.strokeText("这是空心新宋体", 10, 60);  
  19.         context.font = "20px Tangerine serif";  
  20.         context.fillText("Hello HTML5", 10, 100);  
  21.         context.strokeText("Hello HTML5", 10, 150);  
  22.   
  23.     }  
  24.     );  
  25. </script>  
我们尝试写一圈旋转的文字,吧上面的知识点合起来看看效果
  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.translate(150, 150);  
  8.     context.scale(0.7, 0.7);  
  9.     context.font = "12px Tahoma";  
  10.     for (var i = 0; i < 12; i++) {  
  11.         context.fillText((i + 3) % 12 == 0 ? 12 : (i + 3) % 12, 150, 10);  
  12.         context.rotate((Math.PI / 6));  
  13.     }  
  14. }  
  15. );  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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