canvas教程

jQuery + Canvas 旋转的时钟

字号+ 作者:H5之家 来源:H5之家 2017-02-25 18:02 我要评论( )

jQuery + Canvas 旋转的时钟

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Canvas</title> <script type="text/javascript" src="http://www.cnblogs.com/lovling/p/jQuery.js"></script> </head> <style type="text/css"> body { padding: 0; margin: 0; background: black; } #canvas { background: white; margin: 100px 0 0 300px; } #canvas>span { color: white; font-size: 14px; } </style> <body> <canvas> <span>您的浏览器不支持</span> </canvas> </body> </html> <script type="text/javascript"> /*获取绘制环境*/ var oc = $('#canvas')[0]; canvas = oc.getContext('2d'); /*定义圆心和半径*/ var x = 250, y = 250, r = 150; var time = setInterval(function() { ClocksWatche(); }, 1000); function ClocksWatche() { /*清理画布*/ canvas.clearRect(0, 0, oc.width, oc.height); /*开始绘制*/ canvas.beginPath(); for(var i = 0; i < 60; i++) { /*绘制起始点移到圆心*/ canvas.moveTo(x, y); /*根据圆心和半径每6度绘制一次(圆心横坐标,圆心纵坐标,其实弧度,结束弧度,是否逆时针)*/ canvas.arc(x, y, r, 6 * i * Math.PI / 180, 6 * (i + 1) * Math.PI / 180, false); } /*连接至起始点*/ canvas.closePath(); /*画线*/ canvas.stroke(); /*设置线的粗细*/ canvas.lineWidth = 3; /*开始绘制*/ canvas.beginPath(); for(var i = 0; i < 60; i++) { /*绘制起始点移到圆心*/ canvas.moveTo(x, y); /*根据圆心和半径没30度绘制一次*/ canvas.arc(x, y, r, 30 * i * Math.PI / 180, 30 * (i + 1) * Math.PI / 180, false); } /*连接至起始点*/ canvas.closePath(); /*画线*/ canvas.stroke(); /*设置填充颜色*/ canvas.fillStyle = 'white'; canvas.beginPath(); canvas.moveTo(x, y); canvas.arc(x, y, r * 14 / 15, 0, 360, false); canvas.closePath(); canvas.fill(); var loacalDate = new Date(); var hours = loacalDate.getHours(); var minute = loacalDate.getMinutes(); var sencond = loacalDate.getSeconds(); hours = (hours * 30 - 90 + minute / 2) * Math.PI / 180; minute = (minute * 6 - 90) * Math.PI / 180; sencond = (sencond * 6 - 90) * Math.PI / 180; canvas.beginPath(); canvas.lineWidth = 5; canvas.moveTo(x, y); canvas.arc(x, y, r * 8 / 15, hours, hours, false); canvas.stroke(); canvas.beginPath(); canvas.lineWidth = 3; canvas.moveTo(x, y); canvas.arc(x, y, r * 10 / 15, minute, minute, false); canvas.stroke(); canvas.beginPath(); canvas.lineWidth = 1; canvas.moveTo(x, y); canvas.arc(x, y, r * 14 / 15, sencond, sencond, false); canvas.stroke(); } </script>

Tags:jQuery

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • jQuery + Canvas 伸缩旋转方块

    jQuery + Canvas 伸缩旋转方块

    2017-02-26 08:05

  • HTML5 Canvas核心技术:图形、动画与游 HTML5 Canvas核心技术:图

    HTML5 Canvas核心技术:图形、动画与游 HTML5 Canvas核心技术:图

    2017-02-24 15:05

  • uGUI 系统的介绍与创建 Canvas 画布

    uGUI 系统的介绍与创建 Canvas 画布

    2017-02-24 14:02

  • 用H5中的Canvas等技术制作海报

    用H5中的Canvas等技术制作海报

    2017-02-24 14:00

网友点评