import { __awaiter, __generator } from "tslib"; import { useMemo, useRef, useState } from 'functional-mini/compat'; import { useComponent, useEffect } from 'functional-mini/component'; import '../_util/assert-component2'; import { mountComponent } from '../_util/component'; import { createCanvasContext } from '../_util/jsapi/create-canvas-context'; import { getSystemInfo } from '../_util/jsapi/get-system-info'; import { ProgressBarFunctionalProps } from './props'; function requestAnimationFrame(fn) { setTimeout(fn, 16); } function toNumber(value, defaultValue) { if (typeof value === 'number') { return value; } if (typeof value === 'string') { var val = parseInt(value, 10); if (isNaN(val)) { return defaultValue; } return val; } return defaultValue; } var Progress = function (props) { var _a = useState(0), curProgress = _a[0], setCurProgress = _a[1]; var canvasRequestRef = useRef(null); var _b = useState(100), canvasWidthState = _b[0], setCanvasWidthState = _b[1]; function getDrawColor() { return __awaiter(this, void 0, void 0, function () { var strokeColor, trailColor, drawColor; return __generator(this, function (_a) { strokeColor = props.strokeColor, trailColor = props.trailColor; drawColor = { strokeColor: strokeColor || '#1677ff', trailColor: trailColor || '#F5F5F5', }; return [2 /*return*/, drawColor]; }); }); } function drawProgress(ctx, canvasWidth, color, rad) { ctx.beginPath(); ctx.strokeStyle = color; ctx.lineWidth = toNumber(props.strokeWidth, 8); ctx.setLineCap('round'); ctx.arc(canvasWidth / 2, canvasWidth / 2, canvasWidth / 2 - toNumber(props.strokeWidth, 8), -Math.PI / 2, -Math.PI / 2 + (rad / 360) * 2 * Math.PI, false); ctx.stroke(); } function drawOrbit(ctx, canvasWidth, color) { ctx.beginPath(); ctx.strokeStyle = color; ctx.lineWidth = toNumber(props.strokeWidth, 8); ctx.arc(canvasWidth / 2, canvasWidth / 2, canvasWidth / 2 - toNumber(props.strokeWidth, 8), 0, Math.PI * 2, false); ctx.stroke(); } function clearCanvas(ctx, canvasWidth) { ctx.clearRect(0, 0, canvasWidth, canvasWidth); } var instance = useComponent(); var canvasId = useMemo(function () { if (instance.$id) { return "ant-progress-canvas-".concat(instance.$id); } return "ant-progress-canvas"; }, []); function getCanvasContext() { return __awaiter(this, void 0, void 0, function () { var ctx; return __generator(this, function (_a) { switch (_a.label) { case 0: ctx = canvasRequestRef.current; if (!ctx) return [3 /*break*/, 2]; return [4 /*yield*/, ctx]; case 1: return [2 /*return*/, _a.sent()]; case 2: canvasRequestRef.current = createCanvasContext([canvasId, instance]); return [4 /*yield*/, canvasRequestRef.current]; case 3: ctx = _a.sent(); ctx.imageSmoothingEnabled = true; ctx.imageSmoothingQuality = 'high'; return [2 /*return*/, ctx]; } }); }); } function getCanvasWidth() { return __awaiter(this, void 0, void 0, function () { var systemInfo, pixelRatio, width; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, getSystemInfo()]; case 1: systemInfo = _a.sent(); pixelRatio = systemInfo.pixelRatio; width = props.width; return [2 /*return*/, pixelRatio * width]; } }); }); } function updateCanvasProgress(prev, targetPercent) { return __awaiter(this, void 0, void 0, function () { var drawColor, ctx, curRad, canvasWidth, targetRad, direction, draw; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, getDrawColor()]; case 1: drawColor = _a.sent(); return [4 /*yield*/, getCanvasContext()]; case 2: ctx = _a.sent(); curRad = Math.floor((prev / 100) * 360); return [4 /*yield*/, getCanvasWidth()]; case 3: canvasWidth = _a.sent(); setCanvasWidthState(canvasWidth); targetRad = Math.floor((targetPercent / 100) * 360); direction = curRad < targetRad ? 1 : -1; draw = function () { if (curRad == targetRad) return; curRad = direction * props.speed + curRad; if (direction == -1) { curRad = Math.max(curRad, targetRad); } else { curRad = Math.min(curRad, targetRad); } clearCanvas(ctx, canvasWidth); drawOrbit(ctx, canvasWidth, drawColor.trailColor); drawProgress(ctx, canvasWidth, drawColor.strokeColor, curRad); ctx.draw(true); requestAnimationFrame(draw); }; draw(); return [2 /*return*/]; } }); }); } useEffect(function () { var percent = +props.percent; if (isNaN(percent)) { percent = 0; } if (percent !== curProgress) { setCurProgress(percent > 100 ? 100 : percent < 0 ? 0 : percent); } if (props.type === 'circle') { updateCanvasProgress(curProgress, percent); } }, [props.type, props.speed, props.percent]); return { curProgress: curProgress, canvasWidth: canvasWidthState, canvasId: canvasId, }; }; mountComponent(Progress, ProgressBarFunctionalProps);