html canvas直线进度条,HTML5 Canvas实现的好看的进度条

HTML5 Canvas炫丽进度条实现-HTML5星空

var i = 0;

var res = 0;

var context = null;

var total_width = 300;

var total_height = 34;

var initial_x = 20;

var initial_y = 20;

var radius = total_height/2;

window.onload = function() {

var elem = document.getElementById('myCanvas');

if (!elem || !elem.getContext) {

return;

}

context = elem.getContext('2d');

if (!context) {

return;

}

// set font

context.font = "16px Verdana";

// Blue gradient for progress bar

var progress_lingrad = context.createLinearGradient(0,initial_y+total_height,0,0);

progress_lingrad.addColorStop(0, '#4DA4F3');

progress_lingrad.addColorStop(0.4, '#ADD9FF');

progress_lingrad.addColorStop(1, '#9ED1FF');

context.fillStyle = progress_lingrad;

//draw();

res = setInterval(draw, 30);

}

function draw() {

i+=1;

// Clear everything before drawing

context.clearRect(initial_x-5,initial_y-5,total_width+15,total_height+15);

progressLayerRect(context, initial_x, initial_y, total_width, total_height, radius);

progressBarRect(context, initial_x, initial_y, i, total_height, radius, total_width);

progressText(context, initial_x, initial_y, i, total_height, radius, total_width );

if (i>=total_width) {

clearInterval(res);

}

}

function roundRect(ctx, x, y, width, height, radius) {

ctx.beginPath();

ctx.moveTo(x + radius, y);

ctx.lineTo(x + width - radius, y);

ctx.arc(x+width-radius, y+radius, radius, -Math.PI/2, Math.PI/2, false);

ctx.lineTo(x + radius, y + height);

ctx.arc(x+radius, y+radius, radius, Math.PI/2, 3*Math.PI/2, false);

ctx.closePath();

ctx.fill();

}

function progressLayerRect(ctx, x, y, width, height, radius) {

ctx.save();

// Set shadows to make some depth

ctx.shadowOffsetX = 2;

ctx.shadowOffsetY = 2;

ctx.shadowBlur = 5;

ctx.shadowColor = '#666';

// Create initial grey layer

ctx.fillStyle = 'rgba(189,189,189,1)';

roundRect(ctx, x, y, width, height, radius);

// Overlay with gradient

ctx.shadowColor = 'rgba(0,0,0,0)'

var lingrad = ctx.createLinearGradient(0,y+height,0,0);

lingrad.addColorStop(0, 'rgba(255,255,255, 0.1)');

lingrad.addColorStop(0.4, 'rgba(255,255,255, 0.7)');

lingrad.addColorStop(1, 'rgba(255,255,255,0.4)');

ctx.fillStyle = lingrad;

roundRect(ctx, x, y, width, height, radius);

ctx.fillStyle = 'white';

//roundInsetRect(ctx, x, y, width, height, radius);

ctx.restore();

}

function progressBarRect(ctx, x, y, width, height, radius, max) {

// var to store offset for proper filling when inside rounded area

var offset = 0;

ctx.beginPath();

if (width

offset = radius - Math.sqrt(Math.pow(radius,2)-Math.pow((radius-width),2));

ctx.moveTo(x + width, y+offset);

ctx.lineTo(x + width, y+height-offset);

ctx.arc(x + radius, y + radius, radius, Math.PI - Math.acos((radius - width) / radius), Math.PI + Math.acos((radius - width) / radius), false);

}

else if (width+radius>max) {

offset = radius - Math.sqrt(Math.pow(radius,2)-Math.pow((radius - (max-width)),2));

ctx.moveTo(x + radius, y);

ctx.lineTo(x + width, y);

ctx.arc(x+max-radius, y + radius, radius, -Math.PI/2, -Math.acos((radius - (max-width)) / radius), false);

ctx.lineTo(x + width, y+height-offset);

ctx.arc(x+max-radius, y + radius, radius, Math.acos((radius - (max-width)) / radius), Math.PI/2, false);

ctx.lineTo(x + radius, y + height);

ctx.arc(x+radius, y+radius, radius, Math.PI/2, 3*Math.PI/2, false);

}

else {

ctx.moveTo(x + radius, y);

ctx.lineTo(x + width, y);

ctx.lineTo(x + width, y + height);

ctx.lineTo(x + radius, y + height);

ctx.arc(x+radius, y+radius, radius, Math.PI/2, 3*Math.PI/2, false);

}

ctx.closePath();

ctx.fill();

// draw progress bar right border shadow

if (width

ctx.save();

ctx.shadowOffsetX = 1;

ctx.shadowBlur = 1;

ctx.shadowColor = '#666';

if (width+radius>max)

offset = offset+1;

ctx.fillRect(x+width,y+offset,1,total_height-offset*2);

ctx.restore();

}

}

function progressText(ctx, x, y, width, height, radius, max) {

ctx.save();

ctx.fillStyle = '#f00';

var text = Math.floor(width/max*100)+"%";

var text_width = ctx.measureText(text).width;

var text_x = x+width-text_width-radius/2;

if (width<=radius+text_width) {

text_x = x+radius/2;

}

ctx.fillText(text, text_x, y+22);

ctx.restore();

}

HTML5 Canvas进度条

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常精美的h5 进度条 |DEMO_jQuery之家-自由分享jQuery、html5、css3的插件库 <!----> .ClassyCountdownDemo { margin:0 auto 30px auto; max-width:800px; width:calc(100%); padding:30px; display:block } #countdown2 { background:#FFF } #countdown3 { background:rgb(52, 73, 94) } #countdown4 { background:#222 } #countdown5 { background:#222 } #countdown6 { background:#222 } #countdown7 { background:#222 } #countdown8 { background:#222 } #countdown9 { background:#FFF } #countdown10 { background:#3498db } jQuery炫酷图片预览Lightbox插件 A jQuery plugin designed to provide gallery view for images jQuery之家 返回下载页 Example $(document).ready(function() { $('#countdown15').ClassyCountdown({ theme: "flat-colors", end: $.now() + 10000 }); $('#countdown16').ClassyCountdown({ theme: "flat-colors-wide", end: $.now() + 10000 }); $('#countdown17').ClassyCountdown({ theme: "flat-colors-very-wide", end: $.now() + 10000 }); $('#countdown18').ClassyCountdown({ theme: "flat-colors-black", end: $.now() + 10000 }); $('#countdown1').ClassyCountdown({ theme: "white", end: $.now() + 645600 }); $('#countdown5').ClassyCountdown({ theme: "white", end: $.now() + 10000 }); $('#countdown6').ClassyCountdown({ theme: "white-wide", end: $.now() + 10000 }); $('#countdown7').ClassyCountdown({ theme: "white-very-wide", end: $.now() + 10000 }); $('#countdown8').ClassyCountdown({ theme: "white-black", end: $.now() + 10000 }); $('#countdown11').ClassyCountdown({ theme: "black", style: { secondsElement: { gauge: { fgColor: "#F00" } } }, end: $.now() + 10000 }); $('#countdown12').ClassyCountdown({ theme: "black-wide", labels: false, end: $.now() + 10000 }); $('#countdown13').ClassyCountdown({ theme: "black-very-wide", labelsOptions: { lang: { days: 'D', hours: 'H', minutes: 'M', seconds: 'S' }, style: 'font-size:0.5em; text-transform:uppercase;' }, end: $.now() + 10000 }); $('#countdown14').ClassyCountdown({ theme: "black-black", labelsOptions: { style: 'font-size:0.5em; text-transform:uppercase;' }, end: $.now() + 10000 }); $('#countdown4').ClassyCountdown({ end: $.now() + 10000, labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#1abc9c" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, hours: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#2980b9" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, minutes: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#8e44ad" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' }, seconds: { gauge: { thickness: .03, bgColor: "rgba(255,255,255,0.05)", fgColor: "#f39c12" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#fff;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown2').ClassyCountdown({ end: '1388468325', now: '1378441323', labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#1abc9c" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, hours: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#2980b9" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, minutes: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#8e44ad" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, seconds: { gauge: { thickness: .01, bgColor: "rgba(0,0,0,0.05)", fgColor: "#f39c12" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown9').ClassyCountdown({ end: '1388468325', now: '1380501323', labels: true, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#1abc9c", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, hours: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#2980b9", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, minutes: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#8e44ad", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' }, seconds: { gauge: { thickness: .05, bgColor: "rgba(0,0,0,0)", fgColor: "#f39c12", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:#34495e;' } }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown10').ClassyCountdown({ end: '1397468325', now: '1388471324', labels: true, labelsOptions: { lang: { days: 'D', hours: 'H', minutes: 'M', seconds: 'S' }, style: 'font-size:0.5em; text-transform:uppercase;' }, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, hours: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, minutes: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, seconds: { gauge: { thickness: .02, bgColor: "rgba(255,255,255,0.1)", fgColor: "rgba(255,255,255,1)", lineCap: 'round' }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, }, onEndCallback: function() { console.log("Time out!"); } }); $('#countdown3').ClassyCountdown({ end: '1390868325', now: '1388461323', labels: true, labelsOptions: { lang: { days: 'Zile', hours: 'Ore', minutes: 'Minute', seconds: 'Secunde' }, style: 'font-size:0.5em; text-transform:uppercase;' }, style: { element: "", textResponsive: .5, days: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, hours: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, minutes: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' }, seconds: { gauge: { thickness: .2, bgColor: "rgba(255,255,255,0.2)", fgColor: "rgb(241, 196, 15)" }, textCSS: 'font-family:\'Open Sans\'; font-size:25px; font-weight:300; color:rgba(255,255,255,0.7);' } }, onEndCallback: function() { console.log("Time out!"); } }); }); 如果你喜欢这个插件,那么你可能也喜欢: html5+jquery通过鼠标控制的圆形进度条 jQuery和css3旋钮控制按钮-knobKnob

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值