html部分 <!DOCTYPE html> <!--下麵三個百分比都是為了實現後期項目優化的時候,實現自適應屏幕的寬度和高度--> <html style="height: 100%;"> <!--這個需要HTML標簽上也得加上style="height:100%"才行,要不然會顯示一半,經常用C ...
html部分
<!DOCTYPE html>
<!--下麵三個百分比都是為了實現後期項目優化的時候,實現自適應屏幕的寬度和高度-->
<html style="height: 100%;">
<!--這個需要HTML標簽上也得加上style="height:100%"才行,要不然會顯示一半,經常用CSS的都知道,BODY 想高度100%,外標簽HTML也得設置100%-->
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body style="height: 100%;">
<canvas id="canvas" style="height: 100%;"></canvas>
<script type="text/javascript" src="js/digit.js"></script>
<script type="text/javascript" src="js/countdown9.js"></script>
</body>
</html>
digit.js部分
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,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]
],//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]
],//2
[
[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,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0,0,0]
],//9
[
[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]
]//:
];
countdown9.js部分
var WINDOW_WIDTH = 1024;
var WINDOW_HEIGHT = 768;
var RADIUS = 8; //定義小球的半徑
var MARGIN_TOP = 60; //每個數字距離畫布的上邊距的距離
var MARGIN_LEFT = 30; //第一個數字距離畫布的左邊距的距離
//定製截止時間(月是從0開始數一直到11)
//var endTime = new Date();//(2017,11,23,23,12,52)如果在這個括弧裡面填上數字,前面的var改成const就是填寫的時間到當前時間的差的倒計時,最多只能有99個小時,
//endTime.setTime( endTime.getTime()+3600*1000);//這種演算法可以保證每次打開時都是倒計時一小時(1小時=60分鐘=3600秒=3600*1000毫秒)
//小時的位置只有兩位數,一天有24個小時,最多只能倒計時四天,
//如果需要打破這個限制,可以把小時擴充成三位數,甚至增加
//因為倒計時每一秒的變化,要加上動畫的效果,動畫的效果要不停的和當前的時間
//做一個比較,為此設計一個全局變數來表示現在倒計時需要有多少秒,毫秒在計算中
//要轉換成秒,毫秒的細節是不需要的。
var curShowTimeSeconds = 0; //初始化
//為了儲存生成的小球,要設一個數據結構,
var balls = []; //初始化一個空的數組,一旦產生新的小球,就添加在數組裡面
//小球是彩色的,需要一個儲存顏色的數組
const colors = ["red", "yellow", "lime", "purple", "deeppink",
"springgreen", "blue", "green", "tomato", "turquoise"
]
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;
MARGIN_TOP = Math.round(WINDOW_HEIGHT / 5);
var canvas = document.getElementById('canvas');
var context = canvas.getContext("2d");
//這種方式調用寬高,有兩個好處
//第一屏幕的大小改變起來非常的方便
//第二個後續在做屏幕的自適應的時候,只需要計算兩個變數的值,非常的方便
canvas.width = WINDOW_WIDTH;
canvas.height = WINDOW_HEIGHT;
//調用render函數,傳入context參數,獲得繪圖的上下文環境
curShowTimeSeconds = getCurrentShowTimeSeconds();
setInterval(function() {
render(context); //繪製當前的畫面
update(); //根據繪製畫面所需要的數據結構,對數據結構進行一定的調整
}, 50);
}
function getCurrentShowTimeSeconds() {
var curTime = new Date(); //獲取當前的時間
var ret = curTime.getHours()*3600+ curTime.getMinutes()*60+curTime.getSeconds();
return ret ;
}
//對當前的數據進行調整,處理時間的變化
function update() {
//下一次要顯示的時間
var nextShowTimeSeconds = getCurrentShowTimeSeconds();
var nextHours = parseInt(nextShowTimeSeconds / 3600);
var nextMinutes = parseInt((nextShowTimeSeconds - nextHours * 3600) / 60);
var nextSeconds = nextShowTimeSeconds % 60;
var curHours = parseInt(curShowTimeSeconds / 3600);
var curMinutes = parseInt((curShowTimeSeconds - curHours * 3600) / 60);
var curSeconds = curShowTimeSeconds % 60;
//判斷下一次要顯示的時間和當前所顯示的時間有沒有變化,
//如果有變化,就改變curShowTimeSeconds
if(nextSeconds != curSeconds) {
//如果當前的小時的十位數已經不等於下一次要顯示的小時的十位數
if(parseInt(curHours / 10) != parseInt(nextHours / 10)) {
//創建一個新的函數addBalls負責加小球,找到小球的位置,以及相應的數字,生成一系列小球
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();
//數組的元素在不斷的增加,但是沒有減小,這樣的程式是不能長時間運行的,
//每個電腦都有它的極限,,他在不斷的占有我們的記憶體,其實當balls走出我們的畫面的時候
//這個小球就可以不留在數組裡了,設置一個機制,刪除走出畫面的數組,這個程式就可以長時間運行了
// console.log(balls.length);//列印balls數組的長度
}
//讓新產生的小球動起來
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++) //遍歷balls數組
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();
}
}
//實現addBalls()
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) { //如果為1,就添加一個小球
var aBall = {
x: x + j * 2 * (RADIUS + 1) + (RADIUS + 1), //x坐標
y: y + i * 2 * (RADIUS + 1) + (RADIUS + 1), //y坐標
g: 1.5 + Math.random(), //小球的加速度,設置隨機的加速度會使小球更加的活潑
vx: Math.pow(-1, Math.ceil(Math.random() * 1000)) * 4, //小球在x方向的速度
vy: -5, //y方向的速度
color: colors[Math.floor(Math.random() * colors.length)]
}
balls.push(aBall);
}
}
//繪製canvas的畫布
function render(cxt) {
//對一個矩形的圖像進行刷新操作
cxt.clearRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
//先繪製時鐘的數值
var hours = parseInt(curShowTimeSeconds / 3600);
var minutes = parseInt((curShowTimeSeconds - hours * 3600) / 60);
var seconds = curShowTimeSeconds % 60;
//一個數字一個數字的繪製時間
renderDigit(MARGIN_LEFT, MARGIN_TOP, parseInt(hours / 10), cxt)
//15=7*2+1
renderDigit(MARGIN_LEFT + 15 * (RADIUS + 1), MARGIN_TOP, parseInt(hours % 10), cxt)
//30=15+15
renderDigit(MARGIN_LEFT + 3