开发项目,PM会跟踪项目进度;完成某个事情,也能够设置一个完成的进度。html
这里用canvas绘制一个简单百分比圆环进度条。canvas
看下效果:app
1. 动画方式ide
2. 静默方式函数
贴上代码,仅供参考 动画
/** * LBS drawRing * Date: 2015-04-24 * ================================== * opts.parent 插入到哪里 一个JS元素对象 * opts.width 宽度 = 2* (半径+弧宽) * opts.radius 半径 * opts.arc 弧宽 * opts.perent 百分比 * opts.color 弧渲染颜色 [底色,进度色] * opts.textColor 文字渲染颜色 * opts.textSize 文字渲染大小 * opts.animated 是否以动画的方式绘制 默认false * opts.after 绘制完成时执行函数 * ================================== **/ function drawRing(opts) { var _opts = { parent: document.body, width: 100, radius: 45, arc: 5, perent: 100, color: ['#ccc', '#042b61'], textColor: '#000', textSize: '14px', animated: false, after: function() {} }, k; for (k in opts) _opts[k] = opts[k]; var parent = _opts.parent, width = _opts.width, radius = _opts.radius, arc = _opts.arc, perent = parseFloat(_opts.perent), color = _opts.color, textSize = _opts.textSize, textColor = _opts.textColor, c = document.createElement('canvas'), ctx = null, x = 0, animated = _opts.animated, after = _opts.after; parent.appendChild(c); ctx = c.getContext("2d"); ctx.canvas.width = width; ctx.canvas.height = width; function clearFill() { ctx.clearRect(0, 0, width, width); } function fillBG() { ctx.beginPath(); ctx.lineWidth = arc; ctx.strokeStyle = color[0]; ctx.arc(width / 2, width / 2, radius, 0, 2 * Math.PI); ctx.stroke(); } function fillArc(x) { ctx.beginPath(); ctx.lineWidth = arc; ctx.strokeStyle = color[1]; ctx.arc(width / 2, width / 2, radius, -90 * Math.PI / 180, (x * 3.6 - 90) * Math.PI / 180); ctx.stroke(); } function fillText(x) { ctx.font = textSize + ' Arial'; ctx.fillStyle = textColor; ctx.textBaseline = "middle"; ctx.textAlign = 'center'; ctx.fillText(x.toFixed(1) + '%', width / 2, width / 2); } function fill(x) { fillBG(); fillArc(x); fillText(x); } if (!animated) return fill(perent); fill(x); !function animate() { if (++x > perent) return after && after(); setTimeout(animate, 10); clearFill(); fill(x); }(); }
很简单的一段代码spa
先建立一个canvas画布对象,设置宽高。 code
var c = document.createElement('canvas'); document.body.appendChild(c); var ctx = c.getContext("2d"); ctx.canvas.width = width; ctx.canvas.height = width;
圆环由两部分组成,底部灰色完整的环,根据百分比变更的环。htm
先绘制底部完整的环。对象
//起始一条路径 ctx.beginPath(); //设置当前线条的宽度 ctx.lineWidth = 10; //10px //设置笔触的颜色 ctx.strokeStyle = '#ccc'; //arc() 方法建立弧/曲线(用于建立圆或部分圆) ctx.arc(100, 100, 90, 0, 2 * Math.PI); //绘制已定义的路径 ctx.stroke();
重点理解:canvas arc() 方法 :HTML5 canvas arc() 方法
arc方法默认是从3点钟方向(90度)开始绘制的,通常要把这个开始处设置日常习惯的0点方向。
也须要理解弧度和角度的互相转换。
degrees = radians * 180/Math.PI
角度等于弧度乘于180再除于PI
radians = degrees * Math.PI/180
弧度等于角度度乘于PI再除于180
绘制根据百分比变更的环。
ctx.beginPath(); ctx.lineWidth = 10; ctx.strokeStyle = '#f30'; //设置开始处为0点钟方向(-90 * Math.PI / 180) //x为百分比值(0-100) ctx.arc(100, 100, 90, -90 * Math.PI / 180, (x * 3.6 - 90) * Math.PI / 180); ctx.stroke();
绘制中间的文字
ctx.font = '40px Arial'; ctx.fillStyle = '#f30'; ctx.textBaseline = 'middle'; ctx.textAlign = 'center'; ctx.fillText('50.0%', 100, 100);
到此一个静止的百分比圆环进度条就绘制完成了。
不知足于绘制静态的,动态的更生动有趣一些。
canvas动态绘制就是在一段时间内:绘制一个区域的内容,清除这个区域内容,再从新绘制内容,重复这个过程(绘制-清除-绘制)。
有兴趣能够去研究一下,上面的代码也能够参考,若是结合动画函数和运动公式能够绘制更生动的百分比圆环进度条哦。
--------------------------------------------
参考: