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,0,0],
[0,1,1,0,0,0,0],
[1,1,0,0,0,0,0],
[1,1,0,1,1,1,0],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[0,1,1,1,1,1,0]
],//
[
[1,1,1,1,1,1,1],
[1,1,0,0,0,1,1],
[0,0,0,0,1,1,0],
[0,0,0,0,1,1,0],
[0,0,0,1,1,0,0],
[0,0,0,1,1,0,0],
[0,0,1,1,0,0,0],
[0,0,1,1,0,0,0],
[0,0,1,1,0,0,0],
[0,0,1,1,0,0,0]
],//
[
[0,1,1,1,1,1,0],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[0,1,1,1,1,1,0],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[0,1,1,1,1,1,0]
],//
[
[0,1,1,1,1,1,0],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[0,1,1,1,0,1,1],
[0,0,0,0,0,1,1],
[0,0,0,0,0,1,1],
[0,0,0,0,1,1,0],
[0,0,0,1,1,0,0],
[0,1,1,0,0,0,0]
],//
[
[0,0,0,0],
[0,0,0,0],
[0,1,1,0],
[0,1,1,0],
[0,0,0,0],
[0,0,0,0],
[0,1,1,0],
[0,1,1,0],
[0,0,0,0],
[0,0,0,0]
]//:
];
var canvasWidth = document.documentElement.clientWidth - 20,
canvasHeight = document.documentElement.clientHeight - 20,
r = Math.round(canvasWidth * (4 / 5) / 108) - 1,
marginTop = Math.round(canvasHeight / 5),
marginLeft = Math.round(canvasWidth / 10); var curtimeSeconds = getcurtimeSeconds();
var balls = [];
var colors = ['#33b5be', '#0099cc', '#aa66cc', '#9933cc', '#99cc00', '#669900', '#ffbb33', '#ff8800', '#ff4444', '#cc0000'];
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
//获取截止时间与当前时间的秒数
function getcurtimeSeconds() {
var curTime = new Date();
//时钟
var betweenTime = curTime.getHours() * 3600 + curTime.getMinutes() * 60 + curTime.getSeconds();
return betweenTime;
} function renderDigit(x, y, num, ctx) {
ctx.fillStyle = 'orange';
for(var i = 0; i < digit[num].length; i++) {
for(var j = 0; j < digit[num][i].length; j++) {
if(digit[num][i][j] == 1) {
ctx.beginPath();
ctx.arc(x + j * 2 * (r + 1) + (r + 1), y + i * 2 * (r + 1) + (r + 1), r, 0, 2 * Math.PI);
ctx.fill();
}
}
}
} function renderM(ctx) {
renderDigit(marginLeft + 30 * (r + 1), marginTop, 10, ctx);
renderDigit(marginLeft + 69 * (r + 1), marginTop, 10, ctx);
}
//闪烁的冒号
function bilibili(ctx) {
var nowTime = new Date();
var nowString = nowTime.getTime().toString();
var cutTime = nowString.substr(-4);
var nowSeconds = parseInt(cutTime);
// console.log(nowSeconds);
if(Math.round(nowSeconds / 1000) === Math.floor(nowSeconds / 1000)) {
renderM(ctx);
}
} function updateBalls() {
for(var i = 0; i < balls.length; i++) {
balls[i].x += balls[i].vx;
balls[i].y += balls[i].vy;
balls[i].vy += balls[i].g;
//碰撞检测
if(balls[i].y >= canvasHeight - r) {
balls[i].y = canvasHeight - r;
balls[i].vy = -balls[i].vy * 0.8; }
}
//删除画布外的球,提高性能
var count = 0;
for(var i = 0; i < balls.length; i++) {
if(balls[i].x + r > 0 && balls[i].x - r < canvasWidth) {
balls[count++] = balls[i];
}
}
while(balls.length > count) {
balls.pop();
}
} function addBalls(x, y, num) {
for(var i = 0; i < digit[num].length; i++) {
for(var j = 0; j < digit[num][i].length; j++) {
if(digit[num][i][j] == 1) {
var aBall = {
x: x + j * 2 * (r + 1) + (r + 1),
y: y + i * 2 * (r + 1) + (r + 1),
g: 1.5 + Math.random(), //重力加速度
vx: Math.pow(-1, Math.ceil(Math.random() * 1000)) * 3,
vy: -5,
color: colors[Math.floor(Math.random() * colors.length)]
}
balls.push(aBall);
}
}
}
} function render(ctx) {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
var hours = parseInt(curtimeSeconds / 3600),
minutes = parseInt((curtimeSeconds - hours * 3600) / 60),
seconds = curtimeSeconds % 60;
renderDigit(marginLeft, marginTop, parseInt(hours / 10), ctx);
renderDigit(marginLeft + 15 * (r + 1), marginTop, parseInt(hours % 10), ctx); renderDigit(marginLeft + 39 * (r + 1), marginTop, parseInt(minutes / 10), ctx);
renderDigit(marginLeft + 54 * (r + 1), marginTop, parseInt(minutes % 10), ctx); renderDigit(marginLeft + 78 * (r + 1), marginTop, parseInt(seconds / 10), ctx);
renderDigit(marginLeft + 93 * (r + 1), marginTop, parseInt(seconds % 10), ctx); //绘制掉落的小球
for(var i = 0; i < balls.length; i++) {
ctx.fillStyle = balls[i].color;
ctx.beginPath();
ctx.arc(balls[i].x, balls[i].y, r, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
}
}
//更新时间,为小球掉落运动做准备
function update() {
var nexttimeSeconds = getcurtimeSeconds();
var nextHours = parseInt(nexttimeSeconds / 3600),
nextMinutes = parseInt((nexttimeSeconds - nextHours * 3600) / 60),
nextSeconds = nexttimeSeconds % 60;
var curHours = parseInt(curtimeSeconds / 3600),
curMinutes = parseInt((curtimeSeconds - curHours * 3600) / 60),
curSeconds = curtimeSeconds % 60; if(nextSeconds != curSeconds) {
//小时
if(parseInt(curHours / 10) != parseInt(nextHours / 10)) {
addBalls(marginLeft, marginTop, parseInt(curHours / 10));
}
if(parseInt(curHours % 10) != parseInt(nextHours % 10)) {
addBalls(marginLeft + 15 * (r + 1), marginTop, parseInt(curHours / 10));
}
//分
if(parseInt(curMinutes / 10) != parseInt(nextMinutes / 10)) {
addBalls(marginLeft + 39 * (r + 1), marginTop, parseInt(curMinutes % 10));
}
if(parseInt(curMinutes % 10) != parseInt(nextMinutes % 10)) {
addBalls(marginLeft + 54 * (r + 1), marginTop, parseInt(curMinutes % 10));
}
//秒
if(parseInt(curSeconds / 10) != parseInt(nextSeconds / 10)) {
addBalls(marginLeft + 78 * (r + 1), marginTop, parseInt(curSeconds / 10));
}
if(parseInt(curSeconds % 10) != parseInt(nextSeconds % 10)) {
addBalls(marginLeft + 93 * (r + 1), marginTop, parseInt(curSeconds % 10));
} //如果时间变化,那么改变时间
curtimeSeconds = nexttimeSeconds;
}
updateBalls();
} setInterval(function() {
render(context);
update();
bilibili(context);
}, 50);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>倒计时</title>
</head>
<body>
<canvas id="canvas" style="height: 100%;width: 100%;"></canvas>
<script type="text/javascript" src="js/digit.js" ></script>
<script type="text/javascript" src="js/countdown.js" ></script>
</body>
</html>

Javascript学习--时间的更多相关文章

  1. JavaScript学习(一) —— 环境搭建与JavaScript初探

    1.开发环境搭建 本系列教程的开发工具,我们采用HBuilder. 可以去网上下载最新的版本,然后解压一下就能直接用了.学习JavaScript,环境搭建是非常简单的,或者说,只要你有一个浏览器,一个 ...

  2. Web编程基础--HTML、CSS、JavaScript 学习之课程作业“仿360极速浏览器新标签页”

    Web编程基础--HTML.CSS.JavaScript 学习之课程作业"仿360极速浏览器新标签页" 背景: 作为一个中专网站建设出身,之前总是做静态的HTML+CSS+DIV没 ...

  3. JavaScript学习05 定时器

    JavaScript学习05 定时器 定时器1 用以指定在一段特定的时间后执行某段程序. setTimeout(): 格式:[定时器对象名=] setTimeout(“<表达式>”,毫秒) ...

  4. JavaScript学习04 对象

    JavaScript学习04 对象 默认对象 日期对象Date, 格式:日期对象名称=new Date([日期参数]) 日期参数: 1.省略(最常用): 2.英文-数值格式:月 日,公元年 [时:分: ...

  5. JavaScript学习总结-技巧、有用函数、简洁方法、编程细节

    整理JavaScript方面的一些技巧.比較有用的函数,常见功能实现方法,仅作參考 变量转换 //edit http://www.lai18.com var myVar = "3.14159 ...

  6. Java程序猿的JavaScript学习笔记(8——jQuery选择器)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  7. Javascript学习7 - 脚本化浏览器窗口

    原文:Javascript学习7 - 脚本化浏览器窗口 本节讨论了文档对象模型.客户端Javascript下Window中的各项属性,包括计时器.Location对象.Histroy对象.窗口.浏览器 ...

  8. Javascript学习1 - Javascript中的类型对象

    原文:Javascript学习1 - Javascript中的类型对象 1.1关于Numbers对象. 常用的方法:number.toString() 不用具体介绍,把数字转换为字符串,相应的还有一个 ...

  9. javascript学习-对象与原型

    javascript学习-对象与原型 Javascript语言是符合面向对象思想的.一般来说,面向对象思想需要满足以下三个基本要求: 封装,Javascript的对象可以自由的扩充成员变量和方法,自然 ...

随机推荐

  1. HDFS Architecture

    http://hadoop.apache.org/docs/r2.9.0/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html Introduction Ha ...

  2. [转载]基于LVS的AAA负载均衡架构实践

    摘要 本次分享将从一次实际的负载均衡改造案例出发,通过介绍项目背景.选型思路.测试方法和问题分析等方面展开,总结负载均衡架构的一般套路和经验教训. 一.背景 项目背景是某企业的AAA管理系统, AAA ...

  3. 分析uboot中 make xxx_config过程

    make xxx_config实质上就是调用了 首先看MKCONFIG: [注意]SRCTREE=源文件下的目录 之后的语句: @$(MKCONFIG) $(@:_config=) arm arm92 ...

  4. AVFoundation 框架初探究(三)

    这篇总结什么? 在该系列的上一篇的文章中,我们总结的大致内容如下: 1.视频录制  AVCaptureSession + AVCaptureMovieFileOutput 2.视频录制 AVCaptu ...

  5. spring boot +mysql + mybatis + druid的整理(一)——单数据源

    一,使用spring boot脚手架搭建spring boot框架生成maven项目 如下图所示: 设置自定义的坐标,即左侧的Group和Artifact,右侧可以搜索添加一些依赖,搜索不到的可以在p ...

  6. JavaScript 模块化历程

    这是一篇关于js模块化历程的长长的流水账,记录js模块化思想的诞生与变迁,展望ES6模块化标准的未来.经历过这段历史的人或许会感到沧桑,没经历过的人也应该知道这段历史. 无模块时代 在ajax还未提出 ...

  7. BZOJ3529: [Sdoi2014]数表

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3529 挺恶心的数论TAT... 设f[i]是i的约数和,这个可以nln(n)扫出来. ans= ...

  8. 2017广东工业大学程序设竞赛C题爬楼梯

    Description 小时候,我只能一阶一阶得爬楼梯, 后来,我除了能一次爬一阶,还可以一次爬两阶, 到现在,我最多一次可以爬三阶. 那么现在问题来了,我想爬上n层楼,相邻楼层之间有一段楼梯,虽然我 ...

  9. B. OR in Matrix

    B. OR in Matrix time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  10. MongoDB基本命令操作

    在上一篇随笔中记录了如何在Centos7上安装MongoDB数据库,这一篇我们就一起来学学基本的操作命令. 安装完成后,shell交互式下输入mongo就可以直接无密码登录到数据库. show dbs ...