HTML5自学笔记[ 19 ]canvas绘图实例之炫彩时钟
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>炫彩时钟</title> <style> body{ background:#000;} canvas{ background:#fff; } </style> <script src="countdown.js"></script> <script src="digit.js"></script> </head> <body> <canvas id="canvas1"></canvas> </body> </html>
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,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] ]//: ];
/** * Created by dell on 2016/1/29. */ window.onload = function(){ var CVS_WIDTH = document.body.clientWidth * 4/5; var CVS_HEIGHT = document.body.clientHeight * 4/5; var MARGIN_LEFT = document.body.clientWidth * 1/10; var MARGIN_TOP = document.body.clientHeight * 1/10; var RADIUS = (CVS_WIDTH*4/5)/108 - 1; var PADDINGLEFT = (CVS_WIDTH - 108*(RADIUS+1))/2; var color = '#00688B';//数字颜色 var curShowTime = getCurShowTime();//当前剩余时间 var colors = ['#FF3030','#7CCD7C','#0000EE','#EE9A49','#EEEE00','#FF00FF','#96CDCD','#D15FEE','#EEAEEE','#EEDC82'];//彩球颜色集合 var balls = []; var oCvs = document.getElementById('canvas1'); var oCxt = oCvs.getContext('2d'); //设置画布大小 oCvs.width = CVS_WIDTH; oCvs.height = CVS_HEIGHT; oCvs.style.marginLeft = MARGIN_LEFT + 'px'; oCvs.style.marginTop = MARGIN_TOP + 'px'; setInterval(function () { render(oCxt); update(); },50); function update(){ var nextShowTime = getCurShowTime(); var nextHour = parseInt(nextShowTime/3600); var nextMin = parseInt((nextShowTime - nextHour*3600)/60); var nextSec = parseInt(nextShowTime%60); var curHour = parseInt(curShowTime/3600); var curMin = parseInt((curShowTime - curHour*3600)/60); var curSec = parseInt(curShowTime%60); if(nextSec != curSec){ curShowTime = nextShowTime; if(parseInt(nextHour/10) != parseInt(curHour/10)){ addBalls(PADDINGLEFT,MARGIN_TOP,parseInt(nextSec/10)); } if(parseInt(nextHour%10) != parseInt(curHour%10)){ addBalls(PADDINGLEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(nextSec%10)); } if(parseInt(nextMin/10) != parseInt(curMin/10)){ addBalls(PADDINGLEFT+40*(RADIUS+1),MARGIN_TOP,parseInt(nextSec/10)); } if(parseInt(nextMin%10) != parseInt(curMin%10)){ addBalls(PADDINGLEFT+55*(RADIUS+1),MARGIN_TOP,parseInt(nextSec%10)); } if(parseInt(nextSec/10) != parseInt(curSec/10)){ addBalls(PADDINGLEFT+79*(RADIUS+1),MARGIN_TOP,parseInt(nextSec/10)); } if(parseInt(nextSec%10) != parseInt(curSec%10)){ addBalls(PADDINGLEFT+94*(RADIUS+1),MARGIN_TOP,parseInt(nextSec%10)); } //console.info(balls) } updateBalls(); //console.info(balls.length) } function updateBalls(){ for(var i=0;i<balls.length;i++) { balls[i].x += balls[i].xv; balls[i].y += balls[i].yv; balls[i].yv += balls[i].g; if(balls[i].y >= CVS_HEIGHT-RADIUS){ balls[i].y = CVS_HEIGHT - RADIUS; balls[i].yv = -balls[i].yv*0.75; } } var num = 0; for(var j=0;j<balls.length;j++){ if(balls[j].x+RADIUS>0 && balls[j].x-RADIUS<CVS_WIDTH){ balls[num++] = balls[j]; } } balls = balls.slice(0,Math.min(500,num)); } 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 ball = { x:x+j*2*(RADIUS+1)+(RADIUS+1), y:y+i*2*(RADIUS+1)+(RADIUS+1), color:colors[Math.floor(Math.random()*colors.length)], g:1.5+Math.random(), xv:Math.pow(-1,Math.ceil(Math.random()*1000))*4, yv:-10 }; balls.push(ball); } } } } function getCurShowTime(){ var date = new Date(); var ret = date.getHours()*3600 + date.getMinutes()*60 + date.getSeconds(); return ret; } function render(cxt){ var hour = parseInt(curShowTime/3600); var min = parseInt((curShowTime - hour*3600)/60); var sec = parseInt(curShowTime%60); cxt.clearRect(0,0,CVS_WIDTH,CVS_HEIGHT); renderDigit(cxt,PADDINGLEFT,MARGIN_TOP,parseInt(hour/10)); renderDigit(cxt,PADDINGLEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(hour%10)); renderDigit(cxt,PADDINGLEFT+31*(RADIUS+1),MARGIN_TOP,10); renderDigit(cxt,PADDINGLEFT+40*(RADIUS+1),MARGIN_TOP,parseInt(min/10)); renderDigit(cxt,PADDINGLEFT+55*(RADIUS+1),MARGIN_TOP,parseInt(min%10)); renderDigit(cxt,PADDINGLEFT+70*(RADIUS+1),MARGIN_TOP,10); renderDigit(cxt,PADDINGLEFT+79*(RADIUS+1),MARGIN_TOP,parseInt(sec/10)); renderDigit(cxt,PADDINGLEFT+94*(RADIUS+1),MARGIN_TOP,parseInt(sec%10)); for(var i=0;i<balls.length;i++){ cxt.fillStyle = balls[i].color; cxt.beginPath(); cxt.arc(balls[i].x,balls[i].y,RADIUS,0,2*Math.PI); cxt.closePath(); cxt.fill(); } } function renderDigit(cxt,x,y,num){ cxt.fillStyle = color; 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){ cxt.beginPath(); cxt.arc(x+j*2*(RADIUS+1)+(RADIUS+1),y+i*2*(RADIUS+1)+(RADIUS+1),RADIUS,0,2*Math.PI); cxt.closePath(); cxt.fill(); } } } } }
实例出处:慕课网http://www.imooc.com/,喜欢的朋友可以到慕课网搜索学习哦~
HTML5自学笔记[ 19 ]canvas绘图实例之炫彩时钟的更多相关文章
- HTML5自学笔记[ 15 ]canvas绘图实例之钟表
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- HTML5自学笔记[ 21 ]canvas绘图实例之马赛克
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- HTML5自学笔记[ 20 ]canvas绘图实例之绘制倒影
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- HTML5自学笔记[ 13 ]canvas绘图小实例之方块移动
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- HTML5自学笔记[ 12 ]canvas绘图小示例之鼠标画线
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- HTML5自学笔记[ 11 ]canvas绘图基础1
html5新增<canvas>标签用于绘制图像,默认宽高是300*150,canvas的宽高需要在这里设置,在css中设置会有问题:设置的形状,如矩形,会等比缩放,而非设置的宽高. 在不支 ...
- HTML5自学笔记[ 24 ]canvas绘图之星空草地
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- HTML5自学笔记[ 17 ]canvas绘图基础4
绘制图像: drawImage(oImg,x,y),oImg是一个Image对象,(x,y)为绘制起点,绘制的图像大小和源图大小一样. drawImage(oImg,x,y,w,h),后两个参数设置绘 ...
- HTML5自学笔记[ 16 ]canvas绘图基础3
canvas还提供提供了一些动态方法,使图像可以旋转.缩放和移动,与css3中的方法一样. 移动:translate(x,y),x和y为横竖方向的偏移量 旋转:rotate(弧度),弧度=角度*Mat ...
随机推荐
- C#操作oracle 到ExecuteNonQuery卡死不执行
方法1: 可能大家都解决了,我也遇到这个问题,c#更新卡住,但是plsql更新没问题.一直没找到答案,无意中想起,可能是oracle的表锁定了. 用下面的方法,顺利解锁保存了. SELECT /*+ ...
- 在JSP页面中输出完整的时间
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- BZOJ 3546 Life of the Party (二分图匹配-最大流)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3546 题意:给定一个二分图.(AB两个集合的点为n,m),边有K个.问去掉哪些点后 ...
- BZOJ 2132 圈地计划(最小割)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2132 题意:n*m的格子染色黑白,对于格子(i,j)染黑色则价值为A[i][j],白色为 ...
- Android 内部存储和外部存储
应用程序的一些配置文件需要存储在手机上.一般分为内部存储和SD卡存储. 一. 内部存储 ,以 FileOutputStream File file = new File(getFilesDir(),& ...
- [HDOJ5877]Weak Pair(DFS,线段树,离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5877 题意:给一棵树和各点的权值a,求点对(u,v)个数,满足:1.u是v的祖先,2.a(u)*a(v ...
- Spring的起源和背景
上图为Spring框架的组成结构 下面这几张也是的 Spring将大量实际开发中需要重复解决的步骤,抽象成了一个框架. 其中Spring Core Container是Spring框架的核心机制. S ...
- 4,帮助命令man
一:man man是manual的缩写,文档的意思 man man(1),代表man下是分用户级别的,
- 《Linux内核设计的艺术》学习笔记(六)执行setup.s
参考资料 1. 8259A可编程中断控制器 jmpi , SETUPSEG // 0x90200 到这里,bootsect.s的执行就算结束了.控制权转移到了setup.s文件的手中. setup程序 ...
- C++中的一些定义
PS: 这篇博客用来记录一些一般的C++书中草草掠过的一些概念. 或者一些不太容易理解的概念的详细解释. 欢迎新手进入,欢迎高手指正! Orz . 引用: 为对象起了另外一个名字, 引用类型引用(re ...