JS画图之七【时钟】
样例: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画图之七【时钟】的更多相关文章
- [JS,Canvas]日历时钟
[JS,Canvas]日历时钟 Html: <!doctype html> <html> <head> <meta charset="UTF-8&q ...
- js动态数字时钟
js动态数字时钟 主要用到知识点: 主要是通过数组的一些方法,如:Array.from() Array.reduce() Array.find() 时间的处理和渲染 js用到面向对象的写法 实现的功能 ...
- js绘制圆形时钟
纯js制作圆形时钟 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- js数字滑动时钟
js数字滑动时钟: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 通过Canvas + JS 实现简易时钟实战
最近通过各种渠道学习了下html5中的canvas元素,为了练练手就随手写了一个简易的时钟.时钟本身不复杂,没有使用图片进行美化,下面就与大家分享一下具体的代码: 这是最终实现的效果: 部分的启发点来 ...
- 原生js实现中文时钟
零.寒暄 终于一个月可以更新两篇博客了,开心.昨天花了大概一天的时间玩了下github,基本的clone和push都搞定了,如果有和我一样的新手没调通的,大家可以交流. 另外,说个题外话,大家发现我的 ...
- js+css3动态时钟-------Day66
昨天,有一天招,宽带到底没装上.相当恼火,不过包了一天租新房,有很多想法下来,其中,率先实现了--动态时钟(它已上载的资源,一些粗略的全貌.汗...) 这里记录.这个看似简单的功能,以达到良好的,我在 ...
- 简单的js实现网页时钟
js实现时钟. <div id="clock"></div> <script type="text/javascript"> ...
- 史上最简单的js+css3实现时钟效果
今天我看到百度搜索的时间那个效果不错,于是就产生了模仿一下的效果,不过为了节省时间,就随便布了下局,废话不多说,先看看效果吧,顺便把百度的效果也拿过来. 对比样子差了好多啊,但是基本功能都是实现了的, ...
随机推荐
- 【原创】python爬虫获取网站数据并存入本地数据库
#coding=utf-8 import urllib import re import MySQLdb dbnumber = MySQLdb.connect('localhost', 'root', ...
- ctrl+z 以后怎么恢复挂起的进程
(1) CTRL+Z挂起进程并放入后台 (2) jobs 显示当前暂停的进程 (3) bg %N 使第N个任务在后台运行(%前有空格) (4) fg %N 使第N个任务在前台运行 默认bg,fg不带% ...
- Shell编程基础篇
1.1 前言 1.1.1 为什么学Shell Shell脚本语言是实现Linux/UNIX系统管理及自动化运维所必备的重要工具, Linux/UNIX系统的底层及基础应用软件的核心大都涉及Shell脚 ...
- 【转】javascript中的LHS与RHS
原文链接:http://www.cnblogs.com/yangxiaoguai132/p/5064625.html 最近在学习javascript过程中,接触了LHS与RHS的概念,刚开始的时候有点 ...
- UWP 调用outlook邮箱反馈
public static async Task FeedbackAsync(string address, string subject, string body) { if (address == ...
- 关于websorm卡顿的问题
要是电脑不卡的话,使用webstorm真可谓是一种享受,但是随着项目的开展,文件逐渐增大,webstorm自然也会出现卡顿(毕竟缺点就是吃内存),这个时候我们可以增加Xms设置 Start1: 1 找 ...
- Solr6.5.0配置solrcore图文详解
准备环境: solr6.5.0安装完成 jdk1.8 solrhome配置成功 详情:
- Python的用户交互程序及格式化输出
1. 用户输入 在Python 3 中,用户输入用input()函数即可实现用户交互程序. 例如,我们根据程序提示输入用户名和密码,并且打印输入的信息. 2. 字符串格式化输出 例如,我们根据程序提 ...
- 如何在BIOS里设置定时关机?
如何在BIOS里设置定时关机? 通过CMOS设置实现定时开机的设置过程如下: 首先进入"CMOS SETUP"程序(大多数主板是在计算机启动时按DEL键进入): 然后将光条移到&q ...
- vue项目的骨架及常用组件介绍
vue项目基础结构 一个vue的项目,我觉得最小的子集其实就是{vue,vue-router,component},vue作为基础库,为我们提供双向绑定等功能.vue-router连接不同的" ...