样例:http://www.zhaojz.com.cn/demo/draw12.html

依赖:圆

一、定义对象:针

        //定义钟表指针
//dotClock 原点
//len 指针长度
function ClockHand(dotClock, len, opts){
this.points = [];
this.dotClock = dotClock;
this.len = len;
this.currentAngle = opts.angle?(270+opts.angle):270; //偏移角
this.drawed = false;
this.type = opts.type?opts.type:""; //指针类型,分时针、分针和秒针
this.color = opts.color?opts.color:"DarkRed"; //指针颜色
this.weight = opts.weight?opts.weight:1; //指针宽度
}
//绘制指针
ClockHand.prototype.draw = function(){
if(!this.drawed){
var rad = this.currentAngle*Math.PI/180; //计算弧度
this.points = getLine(this.dotClock, [this.dotClock[0]+this.len*Math.cos(rad), this.dotClock[1]+this.len*Math.sin(rad)]
,{
color: this.color,
pw: this.weight,
ph: this.weight
});
var i = 0;
while(i < this.points.length){
document.body.appendChild(this.points[i]);
i++;
}
this.drawed = true;
}
}
//擦除指针
ClockHand.prototype._erase_ = function(){
if(this.drawed){
var i = 0;
while(i < this.points.length){
document.body.removeChild(this.points[i]);
i++;
}
this.points = [];
this.drawed = false;
}
}

二、定义对象:钟

        //定义钟表
function Clock(dot, r){
this.cid = new Date().getTime(); //唯一标识
this.dot = dot; //原点
this.r = r; //半径
if(!window.myClock){
window.myClock = {};
}
window.myClock[this.cid+""] = this; //保存引用
}
//初始化,画表盘、刻度和指针
Clock.prototype.init = function(){
drawCircle(this.dot, this.r, 1);
var d = new Date();
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();
this.hourHand = new ClockHand(this.dot, this.r*0.5, {type:"HOUR",color:"BLACK",weight: 2, angle: (hour%12*(360/12)+minute*(360/(12*60))+second*(360/(12*60*60)))});
this.minuteHand = new ClockHand(this.dot, this.r*0.89, {type:"MIN",color:"DarkRed",weight: 2, angle: (minute*(360/60)+second*(360/(60*60)))});
this.secondHand = new ClockHand(this.dot, this.r*0.91, {type:"SEC",color:"RED",weight: 1, angle: (second*(360/60))});
this.hourHand.draw();
this.minuteHand.draw();
this.secondHand.draw();
return this;
}
//开始走时
Clock.prototype.start = function(){
setInterval("myClock['"+this.cid+"']._running_();",1000);
} Clock.prototype._running_ = function(){

        this.secondHand._erase_();
        this.minuteHand._erase_();
        this.minuteHand._erase_();
        this.init();

        }

三、调用

        //半径
var r= 100;// 原点
var dot2 = [200, 200]; new Clock(dot2, r).init().start();

PS:上述脚本所依赖的其它方法,可以通过查看网页源代码以及下载页面所引用的js文件获得。

JS画图之七【时钟】的更多相关文章

  1. [JS,Canvas]日历时钟

    [JS,Canvas]日历时钟 Html: <!doctype html> <html> <head> <meta charset="UTF-8&q ...

  2. js动态数字时钟

    js动态数字时钟 主要用到知识点: 主要是通过数组的一些方法,如:Array.from() Array.reduce() Array.find() 时间的处理和渲染 js用到面向对象的写法 实现的功能 ...

  3. js绘制圆形时钟

    纯js制作圆形时钟 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. js数字滑动时钟

    js数字滑动时钟: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  5. 通过Canvas + JS 实现简易时钟实战

    最近通过各种渠道学习了下html5中的canvas元素,为了练练手就随手写了一个简易的时钟.时钟本身不复杂,没有使用图片进行美化,下面就与大家分享一下具体的代码: 这是最终实现的效果: 部分的启发点来 ...

  6. 原生js实现中文时钟

    零.寒暄 终于一个月可以更新两篇博客了,开心.昨天花了大概一天的时间玩了下github,基本的clone和push都搞定了,如果有和我一样的新手没调通的,大家可以交流. 另外,说个题外话,大家发现我的 ...

  7. js+css3动态时钟-------Day66

    昨天,有一天招,宽带到底没装上.相当恼火,不过包了一天租新房,有很多想法下来,其中,率先实现了--动态时钟(它已上载的资源,一些粗略的全貌.汗...) 这里记录.这个看似简单的功能,以达到良好的,我在 ...

  8. 简单的js实现网页时钟

    js实现时钟. <div id="clock"></div> <script type="text/javascript"> ...

  9. 史上最简单的js+css3实现时钟效果

    今天我看到百度搜索的时间那个效果不错,于是就产生了模仿一下的效果,不过为了节省时间,就随便布了下局,废话不多说,先看看效果吧,顺便把百度的效果也拿过来. 对比样子差了好多啊,但是基本功能都是实现了的, ...

随机推荐

  1. C语言之二分猜数字游戏

    #include <stdio.h>#include <windows.h>#include<string.h>int main() { int oldprice, ...

  2. 使用sklearn进行数据挖掘-房价预测(5)—训练模型

    使用sklearn进行数据挖掘系列文章: 1.使用sklearn进行数据挖掘-房价预测(1) 2.使用sklearn进行数据挖掘-房价预测(2)-划分测试集 3.使用sklearn进行数据挖掘-房价预 ...

  3. DIV居中的经典方法

    1. 实现DIV水平居中 设置DIV的宽高,使用margin设置边距0 auto,CSS自动算出左右边距,使得DIV居中. 1 div{ 2 width: 100px; 3 height: 100px ...

  4. 判断是否AVL平衡二叉书

    #include<iostream> #include<vector> #include<stack> #include<string> #includ ...

  5. jquery获取iframe页面的元素

    $("#iframe_id").contents().find("#iframe_page_id").val(); 其中,iframe_id是页面引用的ifra ...

  6. 16. 使用Exhibitor管理ZooKeeper

    Exhibitor是管理ZooKeeper服务实例的主管服务.由Netflix开发和开放源码,对于ZooKeeper的实例监控.备份/恢复.清理和可视化非常有用. Note Netflix Exhib ...

  7. 用shape画内圆外方,形成一个圆形头像

    很多人都有过这样的经历,想要在自己写的程序里,上传一张随便大小形状的照片在程序里显示都是圆形照片,或者是方形,或者是三角形,但是写代码又非常麻烦,这里就有一个也可以实现一样效果的方法,那就是用 lay ...

  8. Spring Cloud Ribbon 整合 Hystrix

    在前面随笔 Spring Cloud 之 Ribbon 的ribbon工程基础上进行改造 1.pom.xml 加入依赖 <dependency> <groupId>org.sp ...

  9. 在php代码中执行liunx命令

  10. 在学java继承中

    看到一片个人认为的好博客,http://www.cnblogs.com/dolphin0520/p/3803432.html   ;这一节看的比较慢,加油,就是干: 分类不好意思分到Java中,嘿嘿还 ...