memoize calculations in SVG gauge

This commit is contained in:
Alex Holliday committed 2024-11-20 12:13:46 +08:00
1 parent b1449d3587
commit 9bbcaaf156
1 file changed
+9 -4
@@ -1,5 +1,5 @@
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { useEffect, useState } from "react"; import { useEffect, useState, useMemo } from "react";
import { Box, Typography } from "@mui/material"; import { Box, Typography } from "@mui/material";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import "./index.css"; import "./index.css";
@@ -31,9 +31,14 @@ const CustomGauge = ({
strokeWidth = 15, strokeWidth = 15,
}) => { }) => {
// Calculate the length of the stroke for the circle // Calculate the length of the stroke for the circle
const circumference = 2 * Math.PI * radius; const { circumference, totalSize, strokeLength } = useMemo(
const totalSize = radius * 2 + strokeWidth * 2; // This is the total size of the SVG, needed for the viewBox () => ({
const strokeLength = (progress / 100) * circumference; circumference: 2 * Math.PI * radius,
totalSize: radius * 2 + strokeWidth * 2,
strokeLength: (progress / 100) * (2 * Math.PI * radius),
}),
[radius, strokeWidth, progress]
);
const [offset, setOffset] = useState(circumference); const [offset, setOffset] = useState(circumference);
const theme = useTheme(); const theme = useTheme();