使用HTML5的canvas做一个会动的时钟
| [日期:2012-03-19] |
来源:Linux社区
作者:shyleoking |
[字体:大 中 小]
|
组合
- $(
- function() {
- var canvas = document.getElementById("mycanvas");
- canvas.height = 100;
- canvas.width = 100;
- var context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
-
- context.globalCompositeOperation = "source-over";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- $(canvas).toggle(
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "destination-over";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- },
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "source-in";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- },
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "destination-in";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
-
- },
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "source-out";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- },
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "destination-out";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- },
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "source-atop";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- },
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "destination-atop";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- },
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "lighter";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- },
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "darker";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- },
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "xor";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- },
- function() {
- canvas.width = 100;
-
- context.clearRect(0, 0, 500, 500);
- context.beginPath();
- context = canvas.getContext("2d");
- context.fillStyle = "#AABBCC";
- context.fillRect(10, 10, 50, 50);
- context.globalCompositeOperation = "copy";
- context.fillStyle = "blue";
- context.arc(70, 30, 25, (Math.PI / 180) * 0, (Math.PI / 180) * 360, false);
- context.fill();
- $("span").html(context.globalCompositeOperation);
- alert("演示结束");
- }
- );
- }
- );
字体(看文档说canvas的字体支持CSS样式的描写,但是,我不知道怎么样让canvas的font支持CSS3的在线字体)
- <script src="js/jquery-1.7.1.min.js" type="text/Javascript"></script>
-
- <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Tangerine">
-
- <script type="text/javascript">
- $.log = function(msg) {
- console.log(msg);
- }
-
- $(
- function() {
- var canvas = document.getElementById("mycanvas");
- canvas.height = 200;
- canvas.width = 200;
- var context = canvas.getContext("2d");
- context.font = "20px 新宋体";
- context.fillText("这是实心新宋体", 10, 30);
- context.strokeText("这是空心新宋体", 10, 60);
- context.font = "20px Tangerine serif";
- context.fillText("Hello HTML5", 10, 100);
- context.strokeText("Hello HTML5", 10, 150);
-
- }
- );
- </script>
我们尝试写一圈旋转的文字,吧上面的知识点合起来看看效果
- $(
- function() {
- var canvas = document.getElementById("mycanvas");
- canvas.height = 500;
- canvas.width = 500;
- var context = canvas.getContext("2d");
- context.translate(150, 150);
- context.scale(0.7, 0.7);
- context.font = "12px Tahoma";
- for (var i = 0; i < 12; i++) {
- context.fillText((i + 3) % 12 == 0 ? 12 : (i + 3) % 12, 150, 10);
- context.rotate((Math.PI / 6));
- }
- }
- );

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