实例介绍
【实例截图】
【核心代码】
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>一个html5实现的简单进度条效果</title> <script type="text/javascript"> 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); } } /** * Draws a rounded rectangle. * @param {CanvasContext} ctx * @param {Number} x The top left x coordinate * @param {Number} y The top left y coordinate * @param {Number} width The width of the rectangle * @param {Number} height The height of the rectangle * @param {Number} radius The corner radius; */ 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(); } /** * Draws a rounded rectangle. * @param {CanvasContext} ctx * @param {Number} x The top left x coordinate * @param {Number} y The top left y coordinate * @param {Number} width The width of the rectangle * @param {Number} height The height of the rectangle * @param {Number} radius The corner radius; */ function roundInsetRect(ctx, x, y, width, height, radius) { ctx.beginPath(); // Draw huge anti-clockwise box ctx.moveTo(1000, 1000); ctx.lineTo(1000, -1000); ctx.lineTo(-1000, -1000); ctx.lineTo(-1000, 1000); ctx.lineTo(1000, 1000); 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(); } /** * Draws a half-rounded progress bar to properly fill rounded under-layer * @param {CanvasContext} ctx * @param {Number} x The top left x coordinate * @param {Number} y The top left y coordinate * @param {Number} width The width of the bar * @param {Number} height The height of the bar * @param {Number} radius The corner radius; * @param {Number} max The under-layer total width; */ 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<radius) { 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<max-1) { 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(); } } /** * Draws properly-positioned progress bar percent text * @param {CanvasContext} ctx * @param {Number} x The top left x coordinate * @param {Number} y The top left y coordinate * @param {Number} width The width of the bar * @param {Number} height The height of the bar * @param {Number} radius The corner radius; * @param {Number} max The under-layer total width; */ function progressText(ctx, x, y, width, height, radius, max) { ctx.save(); ctx.fillStyle = 'white'; 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(); } </script> </head> <body> <p> <canvas id="myCanvas" width="500" height="150">Your browser does not have support for canvas.</canvas> </p> </body> </html>
标签: HTML5 ProgressBar HTML t 5
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论