<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
*{
margin: 0;
padding: 0;
}
html,body{
height: 100%;
}
</style>
</head>
<body>
<canvas id="canvas" style="height: 99%"></canvas>
</body>
<script src="digit.js"></script>
<script src="countdown.js"></script>
<script></script>
</html>
var digit=
[
[
[0,0,1,1,1,0,0],
[0,1,1,0,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],
[1,1,0,0,0,1,1],
[1,1,0,0,0,1,1],
[0,1,1,0,1,1,0],
[0,0,1,1,1,0,0]
],//
[
[0,0,0,1,1,0,0],
[0,1,1,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,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],
[1,1,1,1,1,1,1]
],//
[
[0,1,1,1,1,1,0],
[1,1,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,0,1,1,0,0,0],
[0,1,1,0,0,0,0],
[1,1,0,0,0,0,0],
[1,1,0,0,0,1,1],
[1,1,1,1,1,1,1]
],//
[
[1,1,1,1,1,1,1],
[0,0,0,0,0,1,1],
[0,0,0,0,1,1,0],
[0,0,0,1,1,0,0],
[0,0,1,1,1,0,0],
[0,0,0,0,1,1,0],
[0,0,0,0,0,1,1],
[0,0,0,0,0,1,1],
[1,1,0,0,0,1,1],
[0,1,1,1,1,1,0]
],//
[
[0,0,0,0,1,1,0],
[0,0,0,1,1,1,0],
[0,0,1,1,1,1,0],
[0,1,1,0,1,1,0],
[1,1,0,0,1,1,0],
[1,1,1,1,1,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,1,1,1,1]
],//
[
[1,1,1,1,1,1,1],
[1,1,0,0,0,0,0],
[1,1,0,0,0,0,0],
[1,1,1,1,1,1,0],
[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,0,1,1],
[1,1,0,0,0,1,1],
[0,1,1,1,1,1,0]
],//
[
[0,0,0,0,1,1,0],
[0,0,1,1,0,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,1,0,0],
[0,0,1,1,1,0,0],
[0,0,1,1,1,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,1,1,1,0,0],
[0,0,1,1,1,0,0],
[0,0,1,1,1,0,0],
[0,0,0,0,0,0,0]
]//:
];

digit.js

var window_width=1024,
window_height=768,
RADIUS= 8,
MARGIN_TOP=60,
MARGIN_LEFT=30,
//endTime=new Date(2017,5,10,12,47,52),//第二个参数是月份,值为0-11,表示1月 11表示12月
endTime=new Date();
endTime.setTime(endTime.getTime()+3*24*3600*1000);//设置为距离当前时间还有3天
curShowTimeSeconds=0;
var balls=[],
colors=["#33B5E5","#0099CC","#AA66CC","#9933CC","#99CC00","#669900","#FFBB33","#FF8800","#FF6666","#C01110"]; window.onload=function(){
window_width=document.body.clientWidth;
window_height=document.body.clientHeight;
MARGIN_LEFT=Math.round(window_width/10);
RADIUS=Math.round(window_width*4/5/108)-1;
var canvas=document.getElementById("canvas");
var context=canvas.getContext("2d");
canvas.width=window_width;
canvas.height=window_height;
curShowTimeSeconds=getCurrentShowTimeSeconds();
setInterval(function(){
render(context);
update();
},50);
};
function getCurrentShowTimeSeconds(){
var curTime=new Date(),ret=endTime.getTime()-curTime.getTime();
ret=Math.round(ret/1000);
return ret >=0?ret:0;
}
function update(){
var nextShowTimeSeconds=getCurrentShowTimeSeconds(),
nextHours=parseInt(nextShowTimeSeconds/3600),
nextMinutes=parseInt((nextShowTimeSeconds-nextHours*3600)/60),
nextSeconds=parseInt(nextShowTimeSeconds%60),
curHours=parseInt(curShowTimeSeconds/3600),
curMinutes=parseInt((curShowTimeSeconds-curHours*3600)/60),
curSeconds=parseInt(curShowTimeSeconds%60);
if(nextSeconds != curSeconds){
if(parseInt(curHours/10) != parseInt(nextHours/10)){
addBalls(MARGIN_LEFT+0,MARGIN_TOP,parseInt(curHours/10));
}
if(parseInt(curHours%10) != parseInt(nextHours%10)){
addBalls(MARGIN_LEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(curHours/10));
} if(parseInt(curMinutes/10) != parseInt(nextMinutes/10)){
addBalls(MARGIN_LEFT+39*(RADIUS+1),MARGIN_TOP,parseInt(curMinutes/10));
}
if(parseInt(curMinutes%10) != parseInt(nextMinutes%10)){
addBalls(MARGIN_LEFT+54*(RADIUS+1),MARGIN_TOP,parseInt(curMinutes%10));
}
if(parseInt(curSeconds/10) != parseInt(nextSeconds/10)){
addBalls(MARGIN_LEFT+78*(RADIUS+1),MARGIN_TOP,parseInt(curSeconds/10));
}
if(parseInt(curSeconds%10) != parseInt(nextSeconds%10)){
addBalls(MARGIN_LEFT+93*(RADIUS+1),MARGIN_TOP,parseInt(nextSeconds%10));
} curShowTimeSeconds=nextShowTimeSeconds;
}
updateBalls();
}
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>=window_height-RADIUS){
balls[i].y=window_height-RADIUS;
balls[i].vy=-balls[i].vy*0.75;
}
}
var cnt=0;
for(var i=0;i<balls.length;i++){
if(balls[i].x+RADIUS>0 && balls[i].x - RADIUS<window_width){
balls[cnt++]=balls[i];
}
}
while(balls.length>Math.min(300,cnt)){
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*(RADIUS+1)+(RADIUS+1),
y:y+i*2*(RADIUS+1)+(RADIUS+1),
g:1.5+Math.random(),
vx:Math.pow(-1,Math.ceil(Math.random()*1000))*4,//取-4或者正4
vy:-5,
color:colors[Math.floor(Math.random()*colors.length)]
};
balls.push(aBall);
}
}
}
}
function render(ctx){
ctx.clearRect(0,0,window_width,window_height);
var hours=parseInt(curShowTimeSeconds/3600),minutes=parseInt((curShowTimeSeconds-hours*3600)/60),seconds=parseInt(curShowTimeSeconds%60);
renderDigit(MARGIN_LEFT,MARGIN_TOP,parseInt(hours/10),ctx);
renderDigit(MARGIN_LEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(hours%10),ctx);
renderDigit(MARGIN_LEFT+28*(RADIUS+1),MARGIN_TOP,10,ctx);
renderDigit(MARGIN_LEFT+39*(RADIUS+1),MARGIN_TOP,parseInt(minutes/10),ctx);
renderDigit(MARGIN_LEFT+54*(RADIUS+1),MARGIN_TOP,parseInt(minutes%10),ctx);
renderDigit(MARGIN_LEFT+67*(RADIUS+1),MARGIN_TOP,10,ctx);
renderDigit(MARGIN_LEFT+78*(RADIUS+1),MARGIN_TOP,parseInt(seconds/10),ctx);
renderDigit(MARGIN_LEFT+93*(RADIUS+1),MARGIN_TOP,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,RADIUS,0,2*Math.PI,true);
ctx.closePath();
ctx.fill();
}
}
function renderDigit(x,y,num,ctx){//绘制数字
ctx.fillStyle="rgb(0,102,153)";
for(var i=0;i<digit[num].length;i++){//i是行数 j是列数
for(var j=0;j<digit[num][i].length;j++){
if(digit[num][i][j] == 1){
ctx.beginPath();//圆心的位置centerX:x+j*2*(R+1)+(R+1) centerY:y+i*2*(R+1)+(R+1)
ctx.arc(x+j*2*(RADIUS+1)+(RADIUS+1),y+i*2*(RADIUS+1)+(RADIUS+1),RADIUS,0,2*Math.PI);
ctx.closePath();
ctx.fill()
}
}
}
}

countdown.js

var window_width=1024,
window_height=768,
RADIUS= 8,
MARGIN_TOP=60,
MARGIN_LEFT=30,
//endTime=new Date(2017,5,10,12,47,52),//第二个参数是月份,值为0-11,表示1月 11表示12月
/* endTime=new Date();
endTime.setTime(endTime.getTime()+3*24*3600*1000);//设置为距离当前时间还有3天*/
curShowTimeSeconds=0;
var balls=[],
colors=["#33B5E5","#0099CC","#AA66CC","#9933CC","#99CC00","#669900","#FFBB33","#FF8800","#FF6666","#C01110"]; window.onload=function(){
window_width=document.body.clientWidth;
window_height=document.body.clientHeight;
MARGIN_LEFT=Math.round(window_width/10);
RADIUS=Math.round(window_width*4/5/108)-1;
var canvas=document.getElementById("canvas");
var context=canvas.getContext("2d");
canvas.width=window_width;
canvas.height=window_height;
curShowTimeSeconds=getCurrentShowTimeSeconds();
setInterval(function(){
render(context);
update();
},50);
};
function getCurrentShowTimeSeconds(){
var curTime=new Date(),
//ret=endTime.getTime()-curTime.getTime();
ret=curTime.getHours()*3600*1000+curTime.getMinutes()*60*1000+curTime.getSeconds()*1000;
ret=Math.round(ret/1000);
return ret >=0?ret:0;
}
function update(){
var nextShowTimeSeconds=getCurrentShowTimeSeconds(),
nextHours=parseInt(nextShowTimeSeconds/3600),
nextMinutes=parseInt((nextShowTimeSeconds-nextHours*3600)/60),
nextSeconds=parseInt(nextShowTimeSeconds%60),
curHours=parseInt(curShowTimeSeconds/3600),
curMinutes=parseInt((curShowTimeSeconds-curHours*3600)/60),
curSeconds=parseInt(curShowTimeSeconds%60);
if(nextSeconds != curSeconds){
if(parseInt(curHours/10) != parseInt(nextHours/10)){
addBalls(MARGIN_LEFT+0,MARGIN_TOP,parseInt(curHours/10));
}
if(parseInt(curHours%10) != parseInt(nextHours%10)){
addBalls(MARGIN_LEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(curHours/10));
} if(parseInt(curMinutes/10) != parseInt(nextMinutes/10)){
addBalls(MARGIN_LEFT+39*(RADIUS+1),MARGIN_TOP,parseInt(curMinutes/10));
}
if(parseInt(curMinutes%10) != parseInt(nextMinutes%10)){
addBalls(MARGIN_LEFT+54*(RADIUS+1),MARGIN_TOP,parseInt(curMinutes%10));
}
if(parseInt(curSeconds/10) != parseInt(nextSeconds/10)){
addBalls(MARGIN_LEFT+78*(RADIUS+1),MARGIN_TOP,parseInt(curSeconds/10));
}
if(parseInt(curSeconds%10) != parseInt(nextSeconds%10)){
addBalls(MARGIN_LEFT+93*(RADIUS+1),MARGIN_TOP,parseInt(nextSeconds%10));
} curShowTimeSeconds=nextShowTimeSeconds;
}
updateBalls();
}
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>=window_height-RADIUS){
balls[i].y=window_height-RADIUS;
balls[i].vy=-balls[i].vy*0.75;
}
}
var cnt=0;
for(var i=0;i<balls.length;i++){
if(balls[i].x+RADIUS>0 && balls[i].x - RADIUS<window_width){
balls[cnt++]=balls[i];
}
}
while(balls.length>Math.min(300,cnt)){
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*(RADIUS+1)+(RADIUS+1),
y:y+i*2*(RADIUS+1)+(RADIUS+1),
g:1.5+Math.random(),
vx:Math.pow(-1,Math.ceil(Math.random()*1000))*4,//取-4或者正4
vy:-5,
color:colors[Math.floor(Math.random()*colors.length)]
};
balls.push(aBall);
}
}
}
}
function render(ctx){
ctx.clearRect(0,0,window_width,window_height);
var hours=parseInt(curShowTimeSeconds/3600),minutes=parseInt((curShowTimeSeconds-hours*3600)/60),seconds=parseInt(curShowTimeSeconds%60);
renderDigit(MARGIN_LEFT,MARGIN_TOP,parseInt(hours/10),ctx);
renderDigit(MARGIN_LEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(hours%10),ctx);
renderDigit(MARGIN_LEFT+28*(RADIUS+1),MARGIN_TOP,10,ctx);
renderDigit(MARGIN_LEFT+39*(RADIUS+1),MARGIN_TOP,parseInt(minutes/10),ctx);
renderDigit(MARGIN_LEFT+54*(RADIUS+1),MARGIN_TOP,parseInt(minutes%10),ctx);
renderDigit(MARGIN_LEFT+67*(RADIUS+1),MARGIN_TOP,10,ctx);
renderDigit(MARGIN_LEFT+78*(RADIUS+1),MARGIN_TOP,parseInt(seconds/10),ctx);
renderDigit(MARGIN_LEFT+93*(RADIUS+1),MARGIN_TOP,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,RADIUS,0,2*Math.PI,true);
ctx.closePath();
ctx.fill();
}
}
function renderDigit(x,y,num,ctx){//绘制数字
ctx.fillStyle="rgb(0,102,153)";
for(var i=0;i<digit[num].length;i++){//i是行数 j是列数
for(var j=0;j<digit[num][i].length;j++){
if(digit[num][i][j] == 1){
ctx.beginPath();//圆心的位置centerX:x+j*2*(R+1)+(R+1) centerY:y+i*2*(R+1)+(R+1)
ctx.arc(x+j*2*(RADIUS+1)+(RADIUS+1),y+i*2*(RADIUS+1)+(RADIUS+1),RADIUS,0,2*Math.PI);
ctx.closePath();
ctx.fill()
}
}
}
}

countdown.js显示当前时间版

canvas小球 时间倒计时demo-优化的更多相关文章

  1. canvas小球 时间demo

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. 使用canvas编写时间轴插件

    使用canvas编写时间轴插件 背景 项目中有一个视频广场的功能,需要一个时间轴类似视频播放中进度条功能一样显示录像情况,并且可以点击.拖动.放大缩小展示时间轴,获取到时间轴的某个时间.原来的时间轴是 ...

  3. js时间倒计时

    看了网上的其他的例子,觉得写的都有点复杂,不好理解,于是自己动手写了个. 本来想封装成jquery插件,但是觉得因为功能很简单,没有必要做成jquery插件,引用的时候不需要引入jqery库,这里直接 ...

  4. h5 canvas 小球移动

    h5 canvas 小球移动 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  5. 原生js 当前时间 倒计时代码

    源:https://www.oschina.net/code/snippet_2318153_54763 <!DOCTYPE html> <html> <head> ...

  6. Vue的实时时间转换Demo

    Vue的实时时间转换Demo time.html: <!DOCTYPE html> <html lang="en"> <head> <me ...

  7. Java 获取当前时间及实现时间倒计时功能

    引言 在一些项目中或是一些特殊的业务场景中,需要用到显示系统的当前时间,以及一些固定的时间倒计时,时间到后做一些什么事情的业务 .接下来咱们就具体看看代码是怎么实现的: <%@ page lan ...

  8. js+canvas(H5)实现小球移动小demo

    *canvas提供画布,大小自定义,js得到画布,从画布对象通过getContext('2d')来得到画笔,然后就可以开始画了 代码: <!DOCTYPE html> <html l ...

  9. canvas小球动画原理

    随着html5发展,canvas标签作为h5革命性的发展标志也越来越流行.canvas标签的强大之处,不仅在于它可以作为一个独立的画布,也可以利用canvas做一些动画而不用导入flash文件.同时, ...

随机推荐

  1. 详解 Webpack+Babel+React 开发环境的搭建

    1.认识Webpack 构建应用前我们先来了解一下Webpack, Webpack是一个模块打包工具,能够把各种文件(例如:ReactJS.Babel.Coffeescript.Less/Sass等) ...

  2. Web开发者的福利 30段超实用CSS代码

    1.花式连字符(&) 这个类应该在span元素里使用,并且里面包括&字符.它使用经典的serif字体和斜体来增强&符号. .amp { font-family: Baskerv ...

  3. [javaSE] IO流(管道流)

    之前我们使用io流,都是需要一个中间数组,管道流可以直接输入流对接输出流,一般和多线程配合使用,当读取流中没数据时会阻塞当前的线程,对其他线程没有影响 定义一个类Read实现Runable接口,实现r ...

  4. layui对json数据的格式要求

    layui有自己的一套特定的数据格式交互,必须参数code:0,msg:“”,count:数据size(int),data:”数据List”.**一般我们选择封装返回接收类**. 若想要绑定数据到la ...

  5. BigDecimal 类的使用

    BigDecimal 类的使用 1.使用 BigDecimal 的原因   由于需要计算金额,所有需要高精度计算,所有需要使用 BigDecimal 类. BigDecimal能够精确的表示一个小数, ...

  6. groovy类、构造函数、方法

    数据类型:groovy支持Java语言规范定义的数据类型 类:与Java类的主要区别 1.没有可见修饰符的类或者方法是自动公开的 2.类不需要与源文件定义相同名称,但是默认规定定义一样 3.一个源文件 ...

  7. jQuery UI简单的讲解

    我们先进入一下问答时间,你都知道多少呢? (1)什么是jQuery UI 呢? 解答:jQuery UI 是以 jQuery 为基础的开源 JavaScript 网页用户界面代码库.包含底层用户交互. ...

  8. JavaWeb学习总结(九):Cookie进行会话管理

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...

  9. Tomcat学习总结(一):目录简介

    一:下载地址和目录结构说明. Tomcat官方站点:http://jakarta.apache.org 下载Tomcat安装程序包:http://tomcat.apache.org/

  10. Maven学习总结(四):更改maven的编码格式方式

    安装系统之后,一般中文系统默认字符集是GBK.我们安装的软件一般都继承使用操作系统的默认字符集.所以当在中文XP或者win7系统开发,在使用maven(mvn compile)编译项目的时候,就会出现 ...