最近在网上看到了一个css3实现的可爱时钟,觉得很nice,然后就想着用canvas试试实现这个时钟效果。

首先,要实现时钟需要先计算时钟上的数字应该占整个圆的大小。

因为一个圆是360度,所以数字之间就是360/12=30度,所以我在圆上画出数字的代码是这样的:

drawNumber:function(){
this.context.save();
for(var i = 1;i < 13; i++){
var angle = i*30;
angle = angle*Math.PI/180;
var x = Math.round(Math.sin(angle)*(this.r-20));
var y = Math.round(-Math.cos(angle)*(this.r-20));
this.context.fillStyle="#8DA6B8";
this.context.font = "bold 20px Calibri";
this.context.textAlign = "center";
this.context.textBaseline = "middle";
this.context.fillText(i,x+this.widths/2,y+this.heights/2);
}
this.context.restore();
},

因为Math.cos()和Math.sin()接受的都是弧度制,所以需要把30度转换成对应的弧度喔。

其次,我们来看看怎么画时钟上的时针、分针以及秒针。

因为时针、分针、秒针要随着时间的改变而改变,所以我们需要获取当前时间以及context.translate(this.widths/2,this.heights/2);context.rotate(rotate);来对时针、分针、秒针的旋转。

其中时针的旋转弧度是:hrotate = (h*30+(m/60)*30+(s/3600)*30)*Math.PI/180; (ps:每过一个小时,时针就需要旋转30度)

分针的旋转弧度是:mrotate = (m*6+(s/60)*6)*Math.PI/180;(ps:每过一分钟,分针就需要旋转6度)

秒针的旋转弧度是:srotate = (s*6)*Math.PI/180;(ps:每过一秒,秒针就需要旋转6度)

            var time = new Date();
var h = time.getHours();
var m = time.getMinutes();
var s = time.getSeconds();
var hrotate = (h*30+(m/60)*30+(s/3600)*30)*Math.PI/180;
var mrotate = (m*6+(s/60)*6)*Math.PI/180;
var srotate = (s*6)*Math.PI/180;
// console.log(h + ':' + m + ':' + s); //时针
this.context.save();
this.context.translate(this.widths/2,this.heights/2);
this.context.save();
this.context.rotate(hrotate);
this.context.beginPath();
this.context.lineCap="round";
this.context.moveTo(0,0);
this.context.lineTo(0,-40);
this.context.strokeStyle="#1AA9D8";
this.context.lineWidth=6;
this.context.stroke();
this.context.closePath();
this.context.restore();
//分针
this.context.save();
this.context.rotate(mrotate);
this.context.beginPath();
this.context.lineCap="round";
this.context.moveTo(0,0);
this.context.lineTo(0,-50);
this.context.strokeStyle="#23BCEF";
this.context.lineWidth=4;
this.context.stroke();
this.context.closePath();
this.context.restore();
//秒针
this.context.save();
this.context.rotate(srotate);
this.context.beginPath();
this.context.lineCap="round";
this.context.moveTo(0,0);
this.context.lineTo(0,-65);
this.context.strokeStyle="#23BCEF";
this.context.lineWidth=1;
this.context.stroke();
this.context.closePath();
this.context.restore();
this.context.restore();

最后,使用setInterval()方法来实现画布的周期性更新,在每次绘画之前需要先将画布清空哟,要不然,嘿嘿,你懂的。

这样我们的时钟就完成了,canvas是不是也很赞呀!好了,canvas时钟就讲到这啦,有什么讲的不对的地方希望各位大神指正,小女子在此谢过!

canvas实现的时钟效果的更多相关文章

  1. canvas实现简易时钟效果

    代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...

  2. 基于canvas的原生JS时钟效果

    概述 运用html5新增画布canvas技术,绘制时钟效果,无需引用任何插件,纯js. 详细 代码下载:http://www.demodashi.com/demo/11935.html 给大家介绍一个 ...

  3. transform实现的时钟效果

    又来一个时钟效果了,这个的实现不需要canvas,都是div.ul.li画出的,好玩有真实. 哈哈~ 需要的js才能实现到走动这个效果,但js的内容不多,也不难. 主要是一个css里transform ...

  4. 利用js+canvas实现的时钟效果图

    canvas+js时钟特效 运用js+canvas方面的知识完成一个时钟的效果图,再利用for循环实现指针的转动效果: <!--网页文档的声明--> <!doctype html&g ...

  5. canvas自适应圆形时钟绘制

    前面的话 前面介绍过canvas粒子时钟的绘制,本文将详细介绍canvas自适应圆形时钟绘制 效果演示 最终自适应圆形时钟的效果如下所示 功能分析 下面来分析一下该圆形时钟的功能 [1]静态背景 对于 ...

  6. canvas炫酷时钟

    canvas炫酷时钟 实现的功能 主要用到canvas的一些基础api 直接看效果 html: <canvas id="myCanvas" width="500&q ...

  7. 圆盘时钟效果 原生JS

    圆盘时钟 旋转时钟 数字时钟 写在前面 仿荣耀手机时钟,设计的同款时钟效果 实现效果 实现原理 数字时钟 利用Date内置对象获取当下的时间,通过处理呈现在页面上 这一步获取时间是非常简单的,通过Da ...

  8. Flash AS实现时钟效果(全脚本实现)

    最近工作中用到个Flash效果,好久没有写FlashAS脚本了,就想从以前写的代码中找一些实例.竟然看到硬盘中还留有若干年前的代码. 这个时钟效果是全部采用脚本实现,图形也是用脚本绘制的.写于2005 ...

  9. 酷!使用 jQuery & Canvas 制作相机快门效果

    在今天的教程中,我们将使用 HTML5 的 Canvas 元素来创建一个简单的摄影作品集,它显示了一组精选照片与相机快门的效果.此功能会以一个简单的 jQuery 插件形式使用,你可以很容易地整合到任 ...

随机推荐

  1. State of the official Elasticsearch Java clients

    Elasticsearch Java Clients | Elastic https://www.elastic.co/blog/state-of-the-official-elasticsearch ...

  2. SQLAlchemy中scoped_session实现线程安全

    不多说,先上代码 from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine from sqlalchem ...

  3. 【Python】sasa版:文件中csv读取在写入csv读取的数据和执行是否成功。

    sasa写的文件(包含解析文字) # coding=utf- from selenium import webdriver from time import sleep import keyword ...

  4. 007-spring cache-缓存实现-02-springboot ehcahe3实现、springboot caffeine实现

    一.springboot ehcahe3实现步骤 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认CacheProvider.Ehcache是一种广泛 ...

  5. Openstack(十五)快速添加新计算节点

    当后期添加新物理服务器作为计算节点,如果按照上面的过程安装配置的话会非常的慢,但是可以通过复制配置文件的方式快速添加. 15.1计算节点服务安装 #提前将yum仓库.防火墙.selinux.主机名.时 ...

  6. Sql Server CPU 性能排查及优化的相关 Sql

    Sql Server CPU 性能排查及优化的相关 Sql 语句,非常好的SQL语句,记录于此: --Begin Cpu 分析优化的相关 Sql --使用DMV来分析SQL Server启动以来累计使 ...

  7. #ifdef和#if defined的差别

    注意两者都有个define的作用,区别在于使用方式上.前者的通常用法是:#ifdef  XXX .... #else .... #endif 只能在两者中选择是否有定义.对于后者,常用法是: #if ...

  8. pip 解决 ImportError: cannot import name 'main'

    当 pip 更新至最新版的时候,不管是执行 pip list 还说 pip install packageName 安装包,都会抛出一个异常 Traceback (most recent call l ...

  9. D题:数学题(贪心+二分)

    原题大意:原题链接  题解链接 给定两个集合元素,求出两集合间任意两元素相除后得到的新集合中的第k大值 #include<cstdio> #include<algorithm> ...

  10. 《JDK 8.0 学习笔记》1~3章

    第一章 Java平台概论 了解Java的发展历程和相关术语如JDK.JVM.JRE等 第二章 从JDK到IDE 书本介绍了新建Java程序的注意事项以及在cmd和Eclipse环境下如何运行Java, ...