index.js 6.1 KB

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