移动端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

136 regels
4.1 KiB

  1. <template>
  2. <div class="time-graph">
  3. <canvas :id="id" :width="120" :height="120"></canvas>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "circleProccess",
  9. props:{
  10. ids:{
  11. type:Number
  12. },
  13. counts:{
  14. type:Number,
  15. default:0,
  16. }
  17. },
  18. data() {
  19. return {
  20. context:null,
  21. center_x:null,
  22. center_y:null,
  23. rad:null,
  24. speed:0,
  25. context:null,
  26. id:this.ids,
  27. };
  28. },
  29. watch: {
  30. },
  31. mounted() {
  32. let cc = this.counts
  33. if(this.counts>0&&this.counts<99){
  34. cc+=1
  35. }
  36. let time_canvas = document.getElementById(this.ids);
  37. time_canvas.width = document.documentElement.clientWidth/6;
  38. time_canvas.height = document.documentElement.clientWidth/6;
  39. if(this.counts<50){
  40. this.drawMain(time_canvas, cc, "#FA5353", "#eef7e4");
  41. }else if(this.counts<100){
  42. this.drawMain(time_canvas, cc, "#22B7F2", "#eef7e4");
  43. }else{
  44. this.drawMain(time_canvas, cc, "#85d824", "#eef7e4");
  45. }
  46. },
  47. methods: {
  48. drawMain(drawing_elem, percent, forecolor, bgcolor){
  49. this.context = drawing_elem.getContext("2d");
  50. this.center_x = drawing_elem.width / 2;
  51. this.center_y = drawing_elem.height / 2;
  52. this.rad = 3.1415926 * 2 / 100;
  53. this.speed = 0;
  54. if (this.speed <= percent) {
  55. this.run(drawing_elem, percent, forecolor, bgcolor,this.speed,percent)
  56. }
  57. },
  58. run(drawing_elem, percent, forecolor, bgcolor){
  59. while(this.speed <= percent){
  60. window.requestAnimationFrame(this.run);
  61. this.context.clearRect(0, 0, drawing_elem.width, drawing_elem.height);
  62. this.backgroundCircle(bgcolor);
  63. this.text(this.speed,forecolor);
  64. this.foregroundCircle(this.speed,forecolor);
  65. this.speed += 1;
  66. }
  67. },
  68. backgroundCircle(bgcolor) {
  69. this.context.save();
  70. this.context.translate(0, 0)
  71. this.context.setLineDash([5, 5])
  72. this.context.lineWidth = 5; //设置线宽
  73. this.context.textAlign = 'center'
  74. this.context.textBaseline = 'middle'
  75. this.context.font = '18px arial'
  76. this.context.strokeStyle = bgcolor;
  77. this.context.beginPath();
  78. var radius = this.center_x - this.context.lineWidth;
  79. // context.lineCap = "round";
  80. this.context.arc(this.center_x, this.center_y, radius, 0, 3.1415926 * 2, false);
  81. this.context.stroke();
  82. this.context.closePath();
  83. this.context.restore();
  84. },
  85. foregroundCircle(n,forecolor) {
  86. this.context.save();
  87. this.context.strokeStyle = forecolor;
  88. this.context.lineWidth = 5;
  89. this.context.setLineDash([5, 5])
  90. // context.lineCap = "round";
  91. var radius = this.center_x - this.context.lineWidth;
  92. this.context.beginPath();
  93. this.context.arc(this.center_x, this.center_y, radius, -3.1415926 / 2, -3.1415926 / 2 + n * this.rad, false); //用于绘制圆弧context.arc(x坐标,y坐标,半径,起始角度,终止角度,顺时针/逆时针)
  94. this.context.stroke();
  95. this.context.closePath();
  96. this.context.restore();
  97. },
  98. text(n,forecolor) {
  99. this.context.save(); //save和restore可以保证样式属性只运用于该段canvas元素
  100. this.context.fillStyle = forecolor;
  101. if(this.counts==100){
  102. var font_size = document.documentElement.clientWidth/15;
  103. this.context.font = font_size + "px arial";
  104. var text_width = this.context.measureText(n.toFixed(0) + "%").width;
  105. this.context.fillText("✔", this.center_x - text_width / 6, this.center_y + font_size / 2.5);
  106. }else{
  107. var font_size = document.documentElement.clientWidth/35;
  108. this.context.font = font_size + "px arial";
  109. var text_width = this.context.measureText(n.toFixed(0) + "%").width;
  110. this.context.fillText(n.toFixed(0) + "%", this.center_x - text_width / 2, this.center_y + font_size / 2.5);
  111. }
  112. this.context.restore();
  113. }
  114. }
  115. }
  116. </script>
  117. <style scoped lang="scss">
  118. ::v-deep .time-graph {
  119. padding-top: 20px;
  120. display: flex;
  121. display: -webkit-flex;
  122. justify-content: center;
  123. align-items: center;
  124. }
  125. ::v-deep #time-graph-canvas {
  126. width: 60px;
  127. height: 60px;
  128. }
  129. </style>