選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

circliful.js 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. define(factory);// AMD
  4. } else if (typeof exports === 'object' && typeof module !== 'undefined') {
  5. module.exports = factory(); // CommonJS
  6. } else {
  7. window.Progressbar = factory(); // Browser globals
  8. }
  9. }(this, function () {
  10. function Progressbar(options){
  11. this.id = options.id;
  12. this.value = options.value || 0;
  13. this.width = options.width || 200;
  14. this.height = options.height || 200;
  15. this.bgColor = options.bgColor || '#F0ECEC';
  16. this.barColor = options.barColor || '#F9A825';
  17. this.fontColor = options.fontColor || '#606266';
  18. this.text = options.text || '%'
  19. if(options.init){
  20. this.init();
  21. }
  22. }
  23. Progressbar.prototype.init = function(){
  24. console.log(this.id, '创建的id')
  25. var canvas = document.getElementById(this.id);
  26. console.log(canvas, '找没找到', document.getElementById(this.id))
  27. if(!canvas.getContext) {
  28. throw new Error('your browser does not support canvas')
  29. }
  30. if(!this.id){
  31. throw new Error('your need pass id ')
  32. }
  33. var width = parseInt(this.width);
  34. var height = parseInt(this.height);
  35. canvas.setAttribute('width',width);
  36. canvas.setAttribute('height',height);
  37. var ctx = canvas.getContext("2d");
  38. var startAngle = 3 / 2 * Math.PI;
  39. var percentage = 0;
  40. var diffAngle = 0;
  41. var cx = width / 2;
  42. var cy = height / 2;
  43. var timer = setInterval(draw, 50);
  44. var value = this.value;
  45. var bgColor = this.bgColor;
  46. var barColor = this.barColor;
  47. var fontColor = this.fontColor;
  48. var text = this.text;
  49. function draw(){
  50. diffAngle = (percentage / 100) * Math.PI * 2;
  51. //清除矩形区域
  52. ctx.clearRect(0, 0, width, height);
  53. ctx.beginPath();
  54. //设置线段宽度
  55. ctx.lineWidth = 15;
  56. //绘制圆
  57. ctx.arc(cx, cy, 50, 0, 2 * Math.PI, false);
  58. //设置填充色
  59. ctx.fillStyle = '#FFF';
  60. ctx.fill();
  61. //绘制图形轮廓
  62. ctx.strokeStyle = bgColor;
  63. ctx.stroke();
  64. //绘制百分比进度
  65. ctx.beginPath();
  66. ctx.arc(cx, cy, 50, startAngle, diffAngle + startAngle, false);
  67. ctx.strokeStyle = barColor;
  68. ctx.stroke();
  69. //绘制百分比文字
  70. ctx.fillStyle = fontColor;
  71. ctx.textAlign = 'center';
  72. ctx.font = '16px serif';
  73. ctx.fillText(percentage + text, cx, cy + 6);
  74. if (percentage >= value) {
  75. clearTimeout(timer);
  76. }
  77. percentage++;
  78. }
  79. }
  80. return Progressbar;
  81. }));