Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

98 wiersze
2.9 KiB

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