draw.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. function Draw() {
  2. this.canvas = document.getElementById('c');
  3. this.ctx = this.canvas.getContext('2d');
  4. this.rangeValue = 0;
  5. this.nowRange = 0;
  6. this.mW = this.canvas.width = 250;
  7. this.mH = this.canvas.height = 134;
  8. this.lineWidth = 2;
  9. this.r = this.mH / 2;
  10. this.cR = this.r - 16 * this.lineWidth;
  11. this.sX = 0;
  12. this.sY = this.mH / 2;
  13. this.axisLength = this.mW;
  14. this.waveWidth = 0.015;
  15. this.waveHeight = 6;
  16. this.speed = 0.09;
  17. this.xOffset = 0;
  18. this.ctx.lineWidth = this.lineWidth;
  19. this.IsdrawCircled = false;
  20. this.render();
  21. }
  22. Draw.prototype = {
  23. drawCircle: function () {
  24. this.ctx.beginPath();
  25. this.ctx.strokeStyle = 'transprant';
  26. this.ctx.arc(this.r, this.r, this.cR + 5, 0, 2 * Math.PI);
  27. this.ctx.stroke();
  28. this.ctx.beginPath();
  29. this.ctx.arc(this.r, this.r, this.cR, 0, 2 * Math.PI);
  30. this.ctx.clip();
  31. },
  32. drawSin: function () {
  33. this.ctx.save();
  34. var points = [];
  35. this.ctx.beginPath();
  36. for (var x = this.sX; x < this.sX + this.axisLength; x += 20 / this.axisLength) {
  37. var y = -Math.sin((this.sX + x) * this.waveWidth + this.xOffset);
  38. var dY = this.mH * (1 - this.nowRange / 100);
  39. points.push([x, dY + y * this.waveHeight]);
  40. this.ctx.lineTo(x, dY + y * this.waveHeight);
  41. }
  42. this.ctx.lineTo(this.axisLength, this.mH);
  43. this.ctx.lineTo(this.sX, this.mH);
  44. this.ctx.lineTo(points[0][0], points[0][1]);
  45. var my_gradient=this.ctx.createLinearGradient(0,0,0,134);
  46. my_gradient.addColorStop(0,"#e0cde3");
  47. my_gradient.addColorStop(1,"#f3edf5");
  48. this.ctx.fillStyle = my_gradient;
  49. this.ctx.fill();
  50. this.ctx.restore();
  51. },
  52. drawText: function () {
  53. this.ctx.save();
  54. var size = 0.4 * this.cR;
  55. this.ctx.font = "30px 'PingFang SC Light'";
  56. this.ctx.textAlign = 'center';
  57. this.ctx.fillStyle = '#fff';
  58. this.ctx.fillText(~~this.nowRange + '%', this.r + 70, this.r + size / 2);
  59. this.ctx.restore();
  60. },
  61. render: function () {
  62. // this.rangeValue = rangeValue;
  63. this.ctx.clearRect(0, 0, this.mW, this.mH);
  64. if (this.IsdrawCircled == false) {
  65. // this.drawCircle();
  66. }
  67. if (this.nowRange <= this.rangeValue) {
  68. var tmp = 1;
  69. this.nowRange += tmp;
  70. }
  71. if (this.nowRange > this.rangeValue) {
  72. var tmp = 1;
  73. this.nowRange -= tmp;
  74. }
  75. this.drawSin(this.xOffset);
  76. this.drawText();
  77. this.xOffset += this.speed;
  78. requestAnimationFrame(this.render.bind(this));
  79. },
  80. setValue: function(value) {
  81. this.rangeValue = value;
  82. }
  83. }