index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  2. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  17. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  18. if (y = 0, t) op = [op[0] & 2, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. import { useMemo, useRef, useState } from 'functional-mini/compat';
  38. import { useComponent, useEffect } from 'functional-mini/component';
  39. import '../_util/assert-component2';
  40. import { mountComponent } from '../_util/component';
  41. import { createCanvasContext } from '../_util/jsapi/create-canvas-context';
  42. import { getSystemInfo } from '../_util/jsapi/get-system-info';
  43. import { ProgressBarFunctionalProps } from './props';
  44. function requestAnimationFrame(fn) {
  45. setTimeout(fn, 16);
  46. }
  47. function toNumber(value, defaultValue) {
  48. if (typeof value === 'number') {
  49. return value;
  50. }
  51. if (typeof value === 'string') {
  52. var val = parseInt(value, 10);
  53. if (isNaN(val)) {
  54. return defaultValue;
  55. }
  56. return val;
  57. }
  58. return defaultValue;
  59. }
  60. var Progress = function (props) {
  61. var _a = useState(0), curProgress = _a[0], setCurProgress = _a[1];
  62. var canvasRequestRef = useRef(null);
  63. var _b = useState(100), canvasWidthState = _b[0], setCanvasWidthState = _b[1];
  64. function getDrawColor() {
  65. return __awaiter(this, void 0, void 0, function () {
  66. var strokeColor, trailColor, drawColor;
  67. return __generator(this, function (_a) {
  68. strokeColor = props.strokeColor, trailColor = props.trailColor;
  69. drawColor = {
  70. strokeColor: strokeColor || '#1677ff',
  71. trailColor: trailColor || '#F5F5F5',
  72. };
  73. return [2 /*return*/, drawColor];
  74. });
  75. });
  76. }
  77. function drawProgress(ctx, canvasWidth, color, rad) {
  78. ctx.beginPath();
  79. ctx.strokeStyle = color;
  80. ctx.lineWidth = toNumber(props.strokeWidth, 8);
  81. ctx.setLineCap('round');
  82. ctx.arc(canvasWidth / 2, canvasWidth / 2, canvasWidth / 2 - toNumber(props.strokeWidth, 8), -Math.PI / 2, -Math.PI / 2 + (rad / 360) * 2 * Math.PI, false);
  83. ctx.stroke();
  84. }
  85. function drawOrbit(ctx, canvasWidth, color) {
  86. ctx.beginPath();
  87. ctx.strokeStyle = color;
  88. ctx.lineWidth = toNumber(props.strokeWidth, 8);
  89. ctx.arc(canvasWidth / 2, canvasWidth / 2, canvasWidth / 2 - toNumber(props.strokeWidth, 8), 0, Math.PI * 2, false);
  90. ctx.stroke();
  91. }
  92. function clearCanvas(ctx, canvasWidth) {
  93. ctx.clearRect(0, 0, canvasWidth, canvasWidth);
  94. }
  95. var instance = useComponent();
  96. var canvasId = useMemo(function () {
  97. if (instance.$id) {
  98. return "ant-progress-canvas-".concat(instance.$id);
  99. }
  100. return "ant-progress-canvas";
  101. }, []);
  102. function getCanvasContext() {
  103. return __awaiter(this, void 0, void 0, function () {
  104. var ctx;
  105. return __generator(this, function (_a) {
  106. switch (_a.label) {
  107. case 0:
  108. ctx = canvasRequestRef.current;
  109. if (!ctx) return [3 /*break*/, 2];
  110. return [4 /*yield*/, ctx];
  111. case 1: return [2 /*return*/, _a.sent()];
  112. case 2:
  113. canvasRequestRef.current = createCanvasContext([canvasId, instance]);
  114. return [4 /*yield*/, canvasRequestRef.current];
  115. case 3:
  116. ctx = _a.sent();
  117. ctx.imageSmoothingEnabled = true;
  118. ctx.imageSmoothingQuality = 'high';
  119. return [2 /*return*/, ctx];
  120. }
  121. });
  122. });
  123. }
  124. function getCanvasWidth() {
  125. return __awaiter(this, void 0, void 0, function () {
  126. var systemInfo, pixelRatio, width;
  127. return __generator(this, function (_a) {
  128. switch (_a.label) {
  129. case 0: return [4 /*yield*/, getSystemInfo()];
  130. case 1:
  131. systemInfo = _a.sent();
  132. pixelRatio = systemInfo.pixelRatio;
  133. width = props.width;
  134. // 微信小程序不支持 CanvasWidth 参数,此时 pixelRatio 默认为 1
  135. pixelRatio = 1;
  136. return [2 /*return*/, pixelRatio * width];
  137. }
  138. });
  139. });
  140. }
  141. function updateCanvasProgress(prev, targetPercent) {
  142. return __awaiter(this, void 0, void 0, function () {
  143. var drawColor, ctx, curRad, canvasWidth, targetRad, direction, draw;
  144. return __generator(this, function (_a) {
  145. switch (_a.label) {
  146. case 0: return [4 /*yield*/, getDrawColor()];
  147. case 1:
  148. drawColor = _a.sent();
  149. return [4 /*yield*/, getCanvasContext()];
  150. case 2:
  151. ctx = _a.sent();
  152. curRad = Math.floor((prev / 100) * 360);
  153. return [4 /*yield*/, getCanvasWidth()];
  154. case 3:
  155. canvasWidth = _a.sent();
  156. setCanvasWidthState(canvasWidth);
  157. targetRad = Math.floor((targetPercent / 100) * 360);
  158. direction = curRad < targetRad ? 1 : -1;
  159. draw = function () {
  160. if (curRad == targetRad)
  161. return;
  162. curRad = direction * props.speed + curRad;
  163. if (direction == -1) {
  164. curRad = Math.max(curRad, targetRad);
  165. }
  166. else {
  167. curRad = Math.min(curRad, targetRad);
  168. }
  169. clearCanvas(ctx, canvasWidth);
  170. drawOrbit(ctx, canvasWidth, drawColor.trailColor);
  171. drawProgress(ctx, canvasWidth, drawColor.strokeColor, curRad);
  172. ctx.draw(true);
  173. requestAnimationFrame(draw);
  174. };
  175. draw();
  176. return [2 /*return*/];
  177. }
  178. });
  179. });
  180. }
  181. useEffect(function () {
  182. var percent = +props.percent;
  183. if (isNaN(percent)) {
  184. percent = 0;
  185. }
  186. if (percent !== curProgress) {
  187. setCurProgress(percent > 100 ? 100 : percent < 0 ? 0 : percent);
  188. }
  189. if (props.type === 'circle') {
  190. updateCanvasProgress(curProgress, percent);
  191. }
  192. }, [props.type, props.speed, props.percent]);
  193. return {
  194. curProgress: curProgress,
  195. canvasWidth: canvasWidthState,
  196. canvasId: canvasId,
  197. };
  198. };
  199. mountComponent(Progress, ProgressBarFunctionalProps);