index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { __awaiter, __generator } from "tslib";
  2. import { ProgressBarDefaultProps } from './props';
  3. import '../_util/assert-component2';
  4. Component({
  5. props: ProgressBarDefaultProps,
  6. data: {
  7. curProgress: 0,
  8. canvasWidth: 100,
  9. },
  10. ctx: null,
  11. drawColor: null,
  12. canvas: null,
  13. didMount: function () {
  14. this.calProgress();
  15. },
  16. didUpdate: function () {
  17. this.calProgress();
  18. },
  19. methods: {
  20. requestAnimationFrame: function (fn) {
  21. setTimeout(fn, 16);
  22. },
  23. getDrawColor: function () {
  24. return __awaiter(this, void 0, void 0, function () {
  25. var _a, strokeColor, trailColor, drawColor;
  26. return __generator(this, function (_b) {
  27. _a = this.props, strokeColor = _a.strokeColor, trailColor = _a.trailColor;
  28. drawColor = {
  29. strokeColor: strokeColor || '#1677ff',
  30. trailColor: trailColor || '#F5F5F5',
  31. };
  32. return [2 /*return*/, drawColor];
  33. });
  34. });
  35. },
  36. getCanvasContext: function () {
  37. return __awaiter(this, void 0, void 0, function () {
  38. var systemInfo, pixelRatio, width;
  39. return __generator(this, function (_a) {
  40. switch (_a.label) {
  41. case 0:
  42. if (this.ctx)
  43. return [2 /*return*/];
  44. return [4 /*yield*/, my.getSystemInfo()];
  45. case 1:
  46. systemInfo = _a.sent();
  47. pixelRatio = systemInfo.pixelRatio;
  48. width = this.props.width;
  49. this.ctx = my.createCanvasContext("ant-progress-canvas-".concat(this.$id));
  50. this.ctx.imageSmoothingEnabled = true;
  51. this.ctx.imageSmoothingQuality = 'high';
  52. this.setData({
  53. canvasWidth: width * pixelRatio,
  54. });
  55. return [2 /*return*/];
  56. }
  57. });
  58. });
  59. },
  60. clearCanvas: function () {
  61. var ctx = this.ctx;
  62. var canvasWidth = this.data.canvasWidth;
  63. ctx.clearRect(0, 0, canvasWidth, canvasWidth);
  64. },
  65. updateCanvasProgress: function (prev) {
  66. return __awaiter(this, void 0, void 0, function () {
  67. var drawColor, curRad, targetRad, direction, draw;
  68. var _this = this;
  69. return __generator(this, function (_a) {
  70. switch (_a.label) {
  71. case 0: return [4 /*yield*/, this.getDrawColor()];
  72. case 1:
  73. drawColor = _a.sent();
  74. return [4 /*yield*/, this.getCanvasContext()];
  75. case 2:
  76. _a.sent();
  77. curRad = Math.floor((prev / 100) * 360);
  78. targetRad = Math.floor((this.data.curProgress / 100) * 360);
  79. direction = curRad < targetRad ? 1 : -1;
  80. draw = function () {
  81. if (curRad == targetRad)
  82. return;
  83. curRad = direction * _this.props.speed + curRad;
  84. if (direction == -1) {
  85. curRad = Math.max(curRad, targetRad);
  86. }
  87. else {
  88. curRad = Math.min(curRad, targetRad);
  89. }
  90. _this.clearCanvas();
  91. _this.drawOrbit(drawColor.trailColor);
  92. _this.drawProgress(drawColor.strokeColor, curRad);
  93. _this.ctx.draw(true);
  94. _this.requestAnimationFrame(draw);
  95. };
  96. draw();
  97. return [2 /*return*/];
  98. }
  99. });
  100. });
  101. },
  102. drawProgress: function (color, rad) {
  103. var ctx = this.ctx;
  104. var canvasWidth = this.data.canvasWidth;
  105. ctx.beginPath();
  106. ctx.strokeStyle = color;
  107. ctx.lineWidth = this.props.strokeWidth;
  108. ctx.setLineCap('round');
  109. ctx.arc(canvasWidth / 2, canvasWidth / 2, canvasWidth / 2 - this.props.strokeWidth, -Math.PI / 2, -Math.PI / 2 + (rad / 360) * 2 * Math.PI, false);
  110. ctx.stroke();
  111. },
  112. drawOrbit: function (color) {
  113. var ctx = this.ctx;
  114. var canvasWidth = this.data.canvasWidth;
  115. ctx.beginPath();
  116. ctx.strokeStyle = color;
  117. ctx.lineWidth = this.props.strokeWidth;
  118. ctx.arc(canvasWidth / 2, canvasWidth / 2, canvasWidth / 2 - this.props.strokeWidth, 0, Math.PI * 2, false);
  119. ctx.stroke();
  120. },
  121. calProgress: function () {
  122. var percent = +this.props.percent;
  123. if (isNaN(percent)) {
  124. return this.setData({ curProgress: 0 });
  125. }
  126. var prevProgress = this.data.curProgress;
  127. this.setData({
  128. curProgress: percent > 100 ? 100 : percent < 0 ? 0 : percent,
  129. });
  130. if (this.props.type === 'circle') {
  131. this.updateCanvasProgress(prevProgress);
  132. }
  133. },
  134. },
  135. });