canvas-绘制时钟
把最近学到的一些canvas技能全部发上来,刚开始写博客,感觉还不太习惯,不过我相信慢慢就会习惯了。不啰嗦了,把代码送上,看不懂的话可以先去学习下基础教程,把基础学好了也就能看懂了。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<canvas id="canvas" width="500" height="500">你的浏览器不支持canvas</canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d"); function clock(){
ctx.clearRect(0,0,canvas.width,canvas.height);
var date = new Date();
var s = date.getSeconds();
var m = date.getMinutes();
var h = date.getHours();
var r = canvas.width/2.5; //半径
ctx.save();
ctx.translate(250,250);//平移之后坐标系跟着变化
ctx.rotate(-Math.PI/2);//旋转之后坐标系跟着变化 ctx.save();// 记录旋转画布之前初始状态
ctx.lineWidth = 3;
ctx.strokeStyle = "#CCCCCC";
//分刻度
for(var i = 0;i < 60;i++ ){
ctx.beginPath();
ctx.rotate(Math.PI/30);
ctx.moveTo(165,0);
ctx.lineTo(180,0);
ctx.stroke();
}
ctx.restore();// 恢复初始状态,未旋转前 ctx.save();
ctx.lineWidth = 5;
ctx.strokeStyle="black";
//时刻度
for (var i = 0; i < 12; i++) {
ctx.beginPath();
ctx.rotate(Math.PI/6);// 旋转画布绘制刻度
ctx.moveTo(155,0);
ctx.lineTo(180,0);
ctx.stroke();
}
ctx.restore(); ctx.save();
//秒针
ctx.strokeStyle = "blue";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.rotate(s*Math.PI/30);
ctx.moveTo(-40,0);
ctx.lineTo(170,0);
ctx.stroke(); ctx.restore(); ctx.save();
//分针
ctx.strokeStyle = "black";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.rotate((m*Math.PI/30) + (s*Math.PI/1800));
ctx.moveTo(-30,0);
ctx.lineTo(160,0);
ctx.stroke();
ctx.restore(); ctx.save();
//时针
ctx.strokeStyle = "red";
ctx.lineWidth = 4;
ctx.beginPath();
ctx.rotate((h*Math.PI/6)+(m*Math.PI/360) + (s*Math.PI/21600));
ctx.moveTo(-10,0);
ctx.lineTo(150,0);
ctx.stroke();
ctx.restore(); ctx.beginPath();
ctx.arc(0,0,5,0,360,false);
ctx.closePath();
ctx.fillStyle = "#ebebeb";
ctx.fill();
ctx.stroke(); ctx.restore(); //添加数字
for(i = 0;i<12;i++){
angle = i * 30;
// 转换为弧度制,Math.sin、Math.cos都接受弧度制单位
angle = angle*Math.PI/180;;
font = (i + 3 > 12)?i+3-12 : i+3;
fontX = 244+Math.round(Math.cos(angle)*(r-60));
fontY = 256+Math.round(Math.sin(angle)*(r-60));
ctx.font = 'bold 14px 微软雅黑';
ctx.fillText(font+'',fontX,fontY);
} ctx.restore();
window.requestAnimationFrame(clock);
//外圆框
ctx.lineWidth=4;
ctx.strokeStyle="gray";
ctx.beginPath();
ctx.arc(250,250,r,0,Math.PI*2,true);
ctx.stroke();
ctx.restore();
ctx.restore();
}
window.requestAnimationFrame(clock); clock ();
</script>
</body>
</html>
效果图:

canvas-绘制时钟的更多相关文章
- 使用canvas绘制时钟
使用canvas绘制时钟 什么使canvas呢?HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图 ...
- Canvas绘制时钟
①首先在HTML的body标签中添加一个canvas标签,用于绘制时钟. <canvas id="myCanvas" width="600" height ...
- HTML5 之Canvas 绘制时钟 Demo
<!DOCTYPE html> <html> <head> <title>Canvas 之 时钟 Demo</title> <!--简 ...
- html5 Canvas绘制时钟以及绘制运动的圆
1.绘制时钟 <!-- js代码 --> <script type="text/javascript"> window.onload=function(){ ...
- 小任务之Canvas绘制时钟
背景图的绘制(大圆.数字.小圆点) 掌握基础知识:圆的绘制(arc方法),关于圆的弧度的计算,数学中关于sin cos的用法 圆的弧度为2*Math.PI 12个数字分得弧度每个为2*Math.PI/ ...
- 用canvas绘制时钟
用canvas做时钟其实很简单,下面是我做出的效果: 是不是还挺漂亮的? 下面上代码: html <div class="whole"> <canvas id=& ...
- HTML5 Canvas 绘制时钟
网上会看到很多绘制的时钟,看代码也是云里雾里,自学了下Canvas,觉得不难,就自己做了一个. 先看一下截图: 比较简陋,但是该有的都有了,样式只加了个阴影. html代码就不贴了,就一个canvas ...
- 使用canvas绘制时钟 (http://heeroluo.net/Article/Detail/95)
准备工作 在HTML中指定一个区域放置时钟: <div id="clock" style="position: relative;"></di ...
- canvas绘制时钟及注释及save和restore的用法
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 用canvas绘制一个简易时钟
在见识了html5中canvas的强大,笔者准备制作一个简易时钟. 下面就是成果啦,制作之前我们先分析一下,绘制一个时钟需要做哪些准备. 一 . 1.首先这个时钟分为表盘,指针(时针,分针,秒针)和数 ...
随机推荐
- POJ 2739 Sum of Consecutive Prime Numbers 难度:0
题目链接:http://poj.org/problem?id=2739 #include <cstdio> #include <cstring> using namespace ...
- c#中使用servicestackredis操作redis
下载地址: https://github.com/mythz/ServiceStack.Redis 添加dll引用: using ServiceStack.Common.Extensions;usin ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- 神奇的Noip模拟试题 T3 科技节 位运算
3 科技节 (scifest.pas/.c/.cpp) [问题描述] 一年一度的科技节即将到来.同学们报名各项活动的名单交到了方克顺校长那,结果校长一看皱了眉头:这帮学生热情竟然如此高涨,每个人都报那 ...
- tableview 刷新 @property属性的用法
1.tableView的刷新1> 数据刷新的总体步骤* 修改模型数据* 刷新表格(刷新界面) 2> 刷新表格(刷新界面)的方法* 全局刷新(每一行都会重新刷新)- (void)reload ...
- 【转】linux /centos 中OpenSSL升级方法详解
相关软件下载地址 Apache:http://httpd.apache.org/ Nginx:http://nginx.org/en/download.html OpenSSL:http://www. ...
- iOS之沙盒机制和如何获取沙盒路径
iOS APP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URL Scheme.沙盒里面的文件可以是照片.声音文件. ...
- php 封装分页查询类
<?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $lis ...
- 极客DIY:廉价电视棒玩转GNSS-SDR,实现GPS实时定位
0×00 前言 GNSS是Global Navigation Satellite System的缩写.中文称作:全球卫星导航系统.全球导航卫星系统. GNSS泛指所有的卫星导航系统,包括全球的.区域的 ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...