//半径
var r = 130;
//重置原点
ctx.save();
ctx.translate(400, 500); //使用translate重置原点
function drawClock() { //画时钟不动的背景
//时钟外圈
ctx.beginPath();
ctx.arc(0, 0, r, 0, 2*Math.PI);
ctx.strokeStyle = 'blue';
ctx.lineWidth = 5;
ctx.stroke();
ctx.closePath(); //圆心
ctx.beginPath();
ctx.arc(0,0,5,0, 2*Math.PI);
ctx.fillStyle = 'black';
ctx.lineWidth = 1;
ctx.fill();
ctx.closePath(); //画hour数字
var hour = [1,2,3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
hour.forEach(function (value) {
var xValue = (r-28)*Math.cos(Math.PI*(value-3)/6);
var yValue = (r-28)*Math.sin(Math.PI*(value-3)/6);
ctx.font = "18px sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = 'black';
ctx.fillText(value, xValue, yValue);
});
//画刻度
for (var i=0; i<60; i++) {
ctx.beginPath();
var x = (r - 15)*Math.cos(i*2*Math.PI/60);
var y = (r - 15)*Math.sin(i*2*Math.PI/60);
if (i%5 === 0) {
ctx.strokeStyle = "#000";
ctx.arc(x, y, 2, 0, 2*Math.PI, true);
} else {
ctx.strokeStyle = "#bbb";
ctx.arc(x, y, 2, 0, 2*Math.PI, true);
}
ctx.stroke();
ctx.closePath();
}
} function moveClock() { //画会动的指针
let now = new Date();
let h = now.getHours();
ctx.save();
ctx.beginPath();
ctx.rotate(h*Math.PI/6);
ctx.moveTo(0,10);
ctx.lineTo(0, 55-r);
ctx.strokeStyle = 'blue';
ctx.lineWidth = 8;
ctx.lineCap = 'round';
ctx.stroke();
ctx.closePath();
ctx.restore(); //分针
var min = now.getMinutes();
ctx.save();
ctx.beginPath();
ctx.rotate(min*Math.PI/30);
ctx.moveTo(0,10);
ctx.lineTo(0, 40-r);
ctx.strokeStyle = 'green';
ctx.lineWidth = 8;
ctx.lineCap = 'round';
ctx.stroke();
ctx.closePath();
ctx.restore(); //s针
var s = now.getSeconds();
ctx.save();
ctx.beginPath();
ctx.rotate(s*Math.PI/30);
ctx.moveTo(0,10);
ctx.lineTo(0, 30-r);
ctx.strokeStyle = 'red';
ctx.lineWidth = 3;
ctx.lineCap = 'round';
ctx.stroke();
ctx.closePath();
ctx.restore();
} setInterval(function () { //隔一秒调用一次上面两个方法,调用之前先清除画板,否则会有很多指针。
ctx.clearRect(-130,-130, 260, 260);
drawClock();
moveClock();
}, 1000);

canvas 画时钟 会动呦的更多相关文章

  1. canvas画时钟

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. canvas画时钟,重拾乐趣!

    canvas时钟--效果图 一.先来简单介绍画时钟需要的canvas知识 1.在HTML页面中添加canvas元素,必须定义canvas元素的id属性值以便接下来的调用. HTML代码: <ca ...

  3. html5学习(一)--canvas画时钟

    利用空余时间学习一下html5. <!doctype html> <html> <head></head> <body> <canva ...

  4. 用canvas画时钟

    效果图在博客首页上. html: <canvas id="canvas" >Your browser does not support canvas</canva ...

  5. 用canvas画“哆啦A梦”时钟

    前言:今天看完了Js书的canvas画布那张,好开心~又是心爱的canvas~欧耶~ 之前看到有人建议我画蓝胖子,对哦,我怎么把童年最喜欢的蓝胖子忘了,为了表达我对蓝胖子的歉意,所以今天画了会动的he ...

  6. 深夜,用canvas画一个时钟

    深夜,用canvas画一个时钟 查看demo 这几天准备阿里巴巴的笔试,可以说已经是心力交瘁,自从阿里和蘑菇街的内推被刷掉之后,开始越来越怀疑起自己的能力来,虽然这点打击应该是微不足道的.毕竟校招在刚 ...

  7. 简单酷炫的Canvas数字时钟

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 我记得很早之前就看过这个DEMO,是岑安大大博客里看到的: 就是这个数字时钟,当时觉得这个创意不错,但是也没去折腾.直到昨天同事又在网上看 ...

  8. 用canvas绘制时钟

    用canvas做时钟其实很简单,下面是我做出的效果: 是不是还挺漂亮的? 下面上代码: html <div class="whole"> <canvas id=& ...

  9. [JS,Canvas]日历时钟

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

随机推荐

  1. 【高速接口-RapidIO】3、RapidIO串行物理层的包传输过程

    一.引言 前几篇文章已经谈到RapidIO的协议,串行物理层与控制符号. RapidIO协议包括读事务(NREAD),写事务(NWRITE),流写事务(SWRITE),有响应的写事务(NWRITE_R ...

  2. nodejs常用代码片段

    自动创建目录(多级) 相比起使用递归创建,调用 sheljsl 模块简单得多 const shell = require('shelljs') const fs = require('fs') if ...

  3. 实现简易Promise

    概述 异步编程离不开promise, async, 事件响应这些东西,为了更好地异步编程,我打算探究一下promise的实现原理,方法是自己实现一个简易的promise. 根据promise mdn上 ...

  4. 一份从0到1的java项目实践清单

    虽说工作就是简单的事情重复做,但不是所有简单的事你都能有机会做的. 我们平日工作里,大部分时候都是在做修修补补的工作,而这也是非常重要的.做好修补工作,做好优化工作,足够让你升职加薪! 但是如果有机会 ...

  5. AI - 框架(Frameworks)

    1 - Scikit-Learn Sklearn(scikit-learn: machine learning in Python):https://scikit-learn.org/ 文档丰富而又详 ...

  6. C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告

    剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...

  7. Android--通知之Toast

    前言 这篇博客讲解一下Android下的一个简单信息提示的方式:Toast.如果一直看我的博客,会发现在之前的Demo中,一直有用到Toast去提示消息,在这篇博客中就专门讲它.Toast提供一个浮动 ...

  8. hashMap的hashCode() 和equal()的使用

    hashMap的hashCode() 和equa()的使用 在java的集合中,判断两个对象是否相等的规则是: ,判断两个对象的hashCode是否相等 如果不相等,认为两个对象也不相等,完毕 如果相 ...

  9. SpringCloud入门之Spring Boot多环境配置切换指南

    在 spring boot 中,有两种配置文件,一种是application.properties,另一种是application.yml,两种都可以配置spring boot 项目中的一些变量的定义 ...

  10. leetcode — jump-game-ii

    /** * // Source : https://oj.leetcode.com/problems/jump-game-ii/ * * Created by lverpeng on 2017/7/1 ...