Countdown timer on jQuery Canvas
Countdown timer on jQuery Canvas
<html lang="en-US">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<title>Countdown timer on jQuery Canvas</title>
</head>
<body>
<div class="container">
<div class="header">
<div class="time">
<em class="clock"></em>
<div class="c-block c-block-3"><div class="bl-inner"><span>12</span></div> <span class="etitle etitle-1">days</span></div> <ins></ins>
<div class="c-block c-block-2"><div class="bl-inner"><span>18</span></div> <span class="etitle etitle-2">hours</span></div> <ins></ins>
<div class="c-block c-block-2"><div class="bl-inner"><span>35</span></div> <span class="etitle etitle-3">minutes</span></div> <ins></ins>
<div class="c-block c-block-2"><div class="bl-inner"><span>5</span></div> <span class="etitle etitle-4">seconds</span></div>
</div>
</div><!-- /.header -->
</div><!-- /.container -->
<footer>
<h2><i>Example:</i> Countdown timer on jQuery Canvas</h2>
<a href="http://web-zone.com.ua/tajmer-obratnogo-otscheta-na-jquery-canvas.html" class="link" target="_blank">Return to <strong>Web Zone</strong> and download example</a>
</footer>
</body>
</html>
* {
* {
margin: 0;
padding: 0;
}
html{
width: 100%;
height: 100%;
}
body{
position: relative;
background: #fff url(http://web-zone.com.ua/wp-content/uploads/samples/timer/i/bg.png);
width: 100%;
height: auto !important;
height: 100%;
min-height: 100%;
font: normal 1em/1.2em Tahoma, Arial, Helvetica, sans-serif;
/*min-width: 970px;*/
color: #333;
}
.container{
padding-top: 100px;
width: 94%;
max-width: 1300px;
margin: 0 auto;
}
.time{
display: block;
margin: 0 auto;
padding: 20px 0 50px;
position: relative;
text-align: center;
width: 70%;
}
.time *{vertical-align: top;}
.time ins,
.time .clock{
position: relative;
top: 10px;
width: 40px;
height: 55px;
display: inline-block;
*display: inline;
zoom: 1;
background: transparent url(http://web-zone.com.ua//wp-content/uploads/samples/timer/i/sprites.png) no-repeat -260px -154px;
}
.time .clock{margin-right: 7px;}
.time ins{width: 10px;}
.time ins{background-position: -300px -154px;}
.time .etitle{
position: absolute;
width: 110px;
text-align: center;
left: 50%;
margin-left: -47px;
top: 55px;
line-height: 30px;
}
.etitle-1{font-size: 1.6em;}
.etitle-2{
font-size: 1.37em;
padding-top: .1em;
}
.etitle-3{
font-size: 1.12em;
padding-top: .2em;
}
.etitle-4{
font-size: .87em;
padding-top: .3em;
}
.c-block{
display: inline-block;
*display: inline;
zoom: 1;
margin: 0 18px 0 3px;
padding: 4px 0 40px 3px;
position: relative;
background: transparent url(http://web-zone.com.ua/wp-content/uploads/samples/timer/i/sprites.png) no-repeat -260px 0;
}
.c-block:before{
content: '';
position: absolute;
left: 100%;
top: 0;
width: 31px;
background: transparent url(http://web-zone.com.ua/wp-content/uploads/samples/timer/i/sprites.png) no-repeat 100% -77px;
height: 77px;
}
.bl-inner{
position: relative;
margin-right: -10px;
height: 48px;
background: transparent url(http://web-zone.com.ua/wp-content/uploads/samples/timer/i/sprites.png) no-repeat -5px -48px;
}
.bl-inner span{
display: none;
font-size: 46px;
color: #fff;
line-height: 46px;
margin-left: 2px;
}
.bl-inner:before{
content: '';
position: absolute;
left: 100%;
top: 0;
width: 3px;
background: transparent url(http://web-zone.com.ua/wp-content/uploads/samples/timer/i/sprites.png) no-repeat -122px -48px;
height: 48px;
}
.c-block-1{width: 20px;}
.c-block-2{width: 45px;}
.c-block-3{width: 70px;}
/************* DEMO *****************/
@import url(http://fonts.googleapis.com/css?family=Philosopher&subset=latin,cyrillic);
footer {
background-color: #111111;
bottom: 0;
box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.4);
font: 14px/1.3 'Philosopher',Arial,sans-serif;
height: 45px;
left: 0;
position: fixed;
width: 100%;
z-index: 100000;
}
footer h2 {
color: #EEEEEE;
font-size: 14px;
font-weight: normal;
left: 50%;
margin-left: -400px;
padding: 13px 0 0;
position: absolute;
width: 540px;
}
footer h2 i {
color: #888888;
font-style: normal;
}
footer a.link, a.link:visited {
color: #999999;
font-size: 12px;
left: 50%;
margin: 16px 0 0 110px;
position: absolute;
text-decoration: none;
top: 0;
}
footer a strong {
color: #CCCCCC;
font-style: normal;
}
$(function() {
$(function() {
if (document.createElement('canvas').getContext) {
var image = $('<img />').load(function() {
var image = this,
offset_left = 5,
offset_number = 4;
function drawNumberLine(context, position, number, line) {
context.drawImage(image, number * 25, line, 25, 1, position * 25 + offset_left + offset_number, line, 25, 1);
}
function drawNumberBottom(context, position, number) {
context.drawImage(image, number * 25, 25, 25, 23, position * 25 + offset_left + offset_number, 25, 25, 23);
}
function drawNumberTop(context, position, number) {
context.drawImage(image, number * 25, 0, 25, 23, position * 25 + offset_left + offset_number, 0, 25, 23);
}
function cf_draw(from, to, canvas, step) {
var context = canvas.getContext('2d'),
offsets = [
{w: 2, h: -2},
{w: 4, h: -5},
{w: 6, h: -9},
{w: 8, h: -14},
{w: 10, h: -20},
{w: 0, h: 0},
{w: 10, h: -20},
{w: 8, h: -14},
{w: 6, h: -9},
{w: 4, h: -5},
{w: 2, h: -2},
{w: 0, h: 0}
],
width = canvas.width,
nwidth = width + offsets[step].w,
maxscale = nwidth/width,
height = 23,
nheight = height + offsets[step].h,
scale_y = nheight / height,
dig_col = parseInt($(canvas).parents('div.c-block')[0].className.substr(-1), 10);
from = from.toString();
to = to.toString();
while (from.length < dig_col) {from = '0' + from;}
while (to.length < dig_col) {to = '0' + to;}
context.clearRect(0, 0, width, canvas.height);
context.save();
for (var j = 0; j <= to.length - 1; j++) {
drawNumberTop(context, j, to[j]);
}
if (step == 11) {
for (var j = 0; j <= to.length - 1; j++) {
drawNumberBottom(context, j, to[j]);
}
return;
}
for (var j = 0; j <= from.length - 1; j++) {
drawNumberBottom(context, j, from[j]);
}
// draw bg
context.drawImage(image, 0, 96 + step * 48, width - 5, 48, 0, 0, width - 5, 48);
context.drawImage(image, 125, 96 + step * 48, 4, 48, width - 5, 0, 5, 48);
for (var i = 0; i < 24; i++) {
if (step < 5) {
context.setTransform(maxscale - (maxscale - 1) * i / 23, 0, 0, scale_y, -(nwidth - width) / 2 * (1 - i / 23), -offsets[step].h);
for (var j = 0; j <= from.length - 1; j++) {
drawNumberLine(context, j, from[j], i);
}
}
if (step > 5) {
context.setTransform(1 + (maxscale - 1) * i / 23, 0, 0, scale_y, -(nwidth - width) / 2 * (i / 23), -offsets[step].h);
for (var j = 0; j <= to.length - 1; j++) {
drawNumberLine(context, j, to[j], 25+i);
}
}
context.restore();
}
context.setTransform(1, 0, 0, 1, 0, 0);
// draw blick
context.drawImage(image, 130, 96 + step * 48, width - 5, 48, 0, 0, width - 5, 48);
context.drawImage(image, 255, 96 + step * 48, 4, 48, width -5, 0, 5, 48);
}
function cf_animate(from, to, canvas, step) {
cf_draw(from, to, canvas, step);
step ++;
if (step > 11) return;
setTimeout(function() {
cf_animate(from, to, canvas, step);
}, 50 - 20 * step/11);
}
function seconds(from, canvas) {
var to = from - 1;
if (from <= 0) {
to = 59;
// minutes animate
var minutes_from = parseInt($('span', minutes_cnt).text(), 10);
var minutes_to = minutes_from - 1;
if (minutes_from <= 0) {
minutes_to = 59;
// hourses animate
var hourses_from = parseInt($('span', hourses_cnt).text(), 10);
var hourses_to = hourses_from - 1;
if (hourses_from <= 0) {
hourses_to = 23;
var days_from = parseInt($('span', days_cnt).text(), 10);
var days_to = days_from - 1;
if (days_from == 0) {
return;
}
cf_animate(days_from, days_to, $('canvas', days_cnt)[0], 0);
$('span', days_cnt).text(days_to);
}
cf_animate(hourses_from, hourses_to, $('canvas', hourses_cnt)[0], 0);
$('span', hourses_cnt).text(hourses_to);
}
cf_animate(minutes_from, minutes_to, $('canvas', minutes_cnt)[0], 0);
$('span', minutes_cnt).text(minutes_to);
}
cf_animate(from, to, canvas, 0);
from--;
if (from < 0) {from = 59;}
setTimeout(function() {
seconds(from, canvas);
}, 1000);
}
$('div.bl-inner').each(function() {
var canvas = $('<canvas></canvas>').css({
position: 'absolute',
left: -5,
top: 0
}).appendTo(this)[0];
canvas.width = $(this).width() + offset_left * 2 + 3;
canvas.height = $(this).height();
cf_draw(parseInt($('span', this).text(), 10), parseInt($('span', this).text(), 10), canvas, 11);
});
var seconds_cnt = $('div.time .bl-inner:last'),
minutes_cnt = $('div.time .bl-inner:eq(2)'),
hourses_cnt = $('div.time .bl-inner:eq(1)'),
days_cnt = $('div.time .bl-inner:eq(0)');
seconds(parseInt($('span', seconds_cnt).text(), 10), $('canvas', seconds_cnt)[0]);
});
image.attr('src', 'http://web-zone.com.ua/wp-content/uploads/samples/timer/i/sprites.png');
} else {
$('div.bl-inner span').show();
}
});