canvas-菜鸟版画布时钟
这是以前自己练习写的一个画布时钟
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
canvas{
margin: 20px 400px 0;
}
</style>
</head>
<body>
<canvas width="500px" height="500px"></canvas>
<script>
var can=document.getElementsByTagName("canvas")[0];
var x=can.getContext("2d");
function clock(){
//每次执行代码清楚一次画布
x.clearRect(0,0,500,500);
//画一个蓝色实心圆
x.beginPath();
x.fillStyle="blue";
x.arc(250,250,250,Math.PI*0/180,Math.PI*360/180);
x.fill();
x.closePath();
//再来一个白色填充圆,半径小于上边的蓝色填充圆,这样就会出现一个蓝色的圆环
x.beginPath();
x.fillStyle="#ffffff";
x.arc(250,250,230,Math.PI*0/180,Math.PI*360/180);
x.fill();
x.closePath();
//分钟刻度,通过循环画出60根分钟刻度,360/6 得出每个得间隔
for(var i=0;i<60;i++){
x.save(); //保存环境变量
x.beginPath();
x.lineWidth=2;
x.translate(250,250);
x.rotate(i*6*Math.PI/180); //每个刻度间隔是6° ,所以i*6 在进行旋转就画出了所有刻度
x.moveTo(0,220);
x.lineTo(0,230);
x.stroke();
x.closePath();
x.restore(); //返回环境变量
}
//时钟刻度,时钟刻度只有12个所以,360/12 每个之间的间隔是30°,原理和分钟刻度相同当然你还可以循环更多,比如刻度换为数字
for(var a=0;a<12;a++){
x.save();
x.beginPath();
x.lineWidth=4;
x.translate(250,250);
x.rotate(a*30*Math.PI/180);
x.moveTo(0,215);
x.lineTo(0,230);
x.stroke();
x.closePath();
x.restore();
}
//获取当前的本地时间,并分别获取,时,分,秒,时和分都要精确到小数点后
var time=new Date();
var seconds=time.getSeconds();
var minutes=time.getMinutes()+seconds/60;
var hours=time.getHours()+minutes/60;
//当大于12的时候也就是要进入13点了 也就是1点 所以要减去12
if(hours>12){
hours=hours-12
};
//表盘上显示本地时间,如2017/06/14 下午 20:23
x.beginPath()
x.font="20px 黑体"
x.strokeText(time.toLocaleString(),150,200)
x.closePath();
//时
x.save();
x.translate(250,250)
x.lineWidth=4;
x.beginPath();
x.rotate(hours*30*Math.PI/180);//画好时针后当前的小时*30就是应该旋转的°数。
x.moveTo(0,10);
x.lineTo(0,-180);
x.stroke();
x.closePath();
x.restore();
//分
x.save();
x.beginPath();
x.translate(250,250)
x.lineWidth=3;
x.rotate(minutes*6*Math.PI/180); //分针同理
x.moveTo(0,10);
x.lineTo(0,-200)
x.stroke();
x.closePath();
x.restore();
//秒
x.save();
x.beginPath();
x.translate(250,250);
x.lineWidth=2;
x.rotate(seconds*6*Math.PI/180);//秒针也一样
x.moveTo(0,10);
x.lineTo(0,-210);
x.stroke();
x.closePath();
x.restore();
//秒针上的小圆点
x.save();
x.beginPath();
x.translate(250,250);
x.rotate(seconds*6*Math.PI/180);//让秒针小圆点随秒针位置,其实和秒针相同
x.fillStyle="blue";
x.arc(0,-170,4,0,Math.PI*360/180);
x.fill();
x.closePath();
x.restore();
//中心蓝色小圆点
x.beginPath();
x.fillStyle="blue"
x.arc(250,250,6,0,Math.PI*360/180);
x.fill();
x.closePath();
//中心红色小圆点
x.beginPath();
x.fillStyle="red"
x.arc(250,250,3,0,Math.PI*360/180);
x.fill();
x.closePath();
}
setInterval(clock,1000); //隔一秒运行一下这方法,先会清空掉画布然后重新画这样每秒时间都在动了。
</script>
</body>
</html>
效果

canvas-菜鸟版画布时钟的更多相关文章
- 基于canvas的原生JS时钟效果
概述 运用html5新增画布canvas技术,绘制时钟效果,无需引用任何插件,纯js. 详细 代码下载:http://www.demodashi.com/demo/11935.html 给大家介绍一个 ...
- html5 canvas简易版捕鱼达人游戏源码
插件描述:html5利用canvas写的一个js版本的捕鱼,有积分统计,鱼可以全方位移动,炮会跟着鼠标移动,第一次打开需要鼠标移出背景图,再移入的时候就可以控制炮的转动,因为是用的mouseover触 ...
- Canvas组件:画布,可以实现动画操作。
Module 10 Canvas组件:画布,可以实现动画操作. TextArea:文本域. 在单行文本域中回车会激发ActionEvent. 用CheckBoxGroup实现单选框功能. Java中 ...
- Canvas组件:画布,可以实现动画操作
Canvas组件:画布,可以实现动画操作. TextArea:文本域. 在单行文本域中回车会激发ActionEvent. 用CheckBoxGroup实现单选框功能. Java中,单选框和复选框都是使 ...
- USBasp制作资料及全过程(菜鸟版)
源:USBasp制作资料及全过程(菜鸟版) 一.usbasp 的一般性介绍: 1.下载线,是“ISP”(In System Programmability:“在系统编程”)或“IAP”(In Appl ...
- 通过Canvas + JS 实现简易时钟实战
最近通过各种渠道学习了下html5中的canvas元素,为了练练手就随手写了一个简易的时钟.时钟本身不复杂,没有使用图片进行美化,下面就与大家分享一下具体的代码: 这是最终实现的效果: 部分的启发点来 ...
- 公告栏添加时钟——利用canvas画出一个时钟
前言 最近在学习HTML5标签,学到Canvas,觉得很有趣.便在慕课网找了个demo练手.就是Canvas时钟. 对于canvas,w3shcool上是这么描述的: HTML5 <canvas ...
- 用canvas绘制一个简易时钟
在见识了html5中canvas的强大,笔者准备制作一个简易时钟. 下面就是成果啦,制作之前我们先分析一下,绘制一个时钟需要做哪些准备. 一 . 1.首先这个时钟分为表盘,指针(时针,分针,秒针)和数 ...
- Html 5 版 电子时钟
效果图: html 5 canvas元素 Html 5的canvas元素可以用于在网页上绘制图形[即canvas的作用]. canvas画布使用JavaScript在网页上绘制图形 其拥有绘制各种路 ...
随机推荐
- Vue 环境配置
最开始摸element的时候,像盲人摸象,完全没有头绪. https://panjiachen.github.io/vue-element-admin-site/zh/guide/essentials ...
- sharding-jdbc数据分片配置
数据分片 不使用Spring 引入Maven依赖 <dependency> <groupId>org.apache.shardingsphere</groupId> ...
- 兼容 火狐、IE 的中a标签用 javascript:void(0); 依然执行跳转的问题
<a onclick="return false;" href="javascript: void(0)" target="_blank&quo ...
- dataTable 加了竖向滚动条导致列头样式错位的问题 / 亲测可用,不好用你打我,用好了记得点推荐
tab在没有显示之前,容器是没有高度宽度的,而dt在自动计算高度和宽度时是获取的外部容器的高度和宽度,当切换tab时,dt获取不到这个高度宽度,导致列头都挤在一起,是用下面代码解决此问题 $('a[d ...
- 江西理工大学南昌校区排名赛 F: 单身狗的骑马游戏
题目描述 萌樱花是一只单身狗. 萌樱花今天在学姐那里做了一道题: 假设赛马场上有n只马儿,第i只马儿的起点在第i米的位置,这些马儿都会朝着同一个方向奔跑. 每只马儿的速度都不一样,而且大家都不知道这些 ...
- Linux按键设备驱动一
① request_irq函数用于注册中断 int request_irq(unsigned int irq, void(*handler)(int, void*, struct pt_reg*), ...
- redis 网络架构
https://blog.csdn.net/simplemurrina/article/details/53890542 GDB redis https://gitbook.cn/gitchat/c ...
- GraphQL 暂停
别人的文章 http://blog.csdn.net/imwebteam/article/details/53888708 Java 文档都打不开,已经在GitHub上提了 in README.md ...
- Maven---pom.xml 详解(转)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 抽象工厂方法模式(Abstract Factory Pattern)
Provide an interface for creating families of related or dependent objects without specifying their ...