Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 RingGraphに良い感じのアニメーションを実装 #65

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions src/components/DashBoard/RingGraph.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
import { css } from '@emotion/react';
import { Center, RingProgress, Text, useMantineTheme } from '@mantine/core';
import type { FC } from 'react';
import { TextAnimation } from '@/components/DashBoard/TextAnimation';

type RingGraphProps = {
myAchieves: number;
allMissions: number;
};

const size = 250;
const thickness = 30;

const ratioToOffset = (ratio: number) => {
const radius = (size * 0.9 - thickness * 2) / 2;
const deg = (Math.PI * radius * 2) / 100;

return `${ratio * deg}, ${(100 - ratio) * deg}`;
};

export const RingGraph: FC<RingGraphProps> = ({ myAchieves, allMissions }) => {
const theme = useMantineTheme();

const ratio = Math.floor((100 * myAchieves) / allMissions);
const ratio = allMissions && Math.floor((100 * myAchieves) / allMissions);

return (
<RingProgress
roundCaps
size={250}
thickness={30}
size={size}
thickness={thickness}
sections={[{ value: ratio, color: 'cyan' }]}
css={css`
svg circle:nth-of-type(1) {
stroke-dasharray: 0 0;
}

svg circle:nth-of-type(2) {
animation: loading 0.5s 0.5s ease-out forwards;
stroke-dasharray: ${ratioToOffset(0)};
}

@keyframes loading {
from {
stroke-dasharray: ${ratioToOffset(0)};
}

to {
stroke-dasharray: ${ratioToOffset(ratio)};
}
}
`}
label={
<Center
css={css`
Expand All @@ -30,7 +61,7 @@ export const RingGraph: FC<RingGraphProps> = ({ myAchieves, allMissions }) => {
align="center"
size="xl"
>
{ratio} %
<TextAnimation start={0} end={ratio} duration={500} delay={300} /> %
</Text>
<Text align="center" size="md">
({myAchieves} / {allMissions})
Expand Down
47 changes: 47 additions & 0 deletions src/components/DashBoard/TextAnimation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useState, type FC, useEffect } from 'react';

type TextAnimationProps = {
start: number;
end: number;
duration: number;
delay?: number;
};

const easeOut = (t: number) => {
return 1 - (1 - t) ** 1.675;
};

export const TextAnimation: FC<TextAnimationProps> = ({
start,
end,
duration,
delay,
}) => {
const diff = end - start;
const [value, setValue] = useState(start);

useEffect(() => {
let interval: NodeJS.Timer | null = null;
const timeout = setTimeout(() => {
interval = setInterval(() => {
setValue((prev) => {
if (prev < end) {
return prev + 1;
}
return prev;
});
}, duration / diff);
}, delay ?? 0);

return () => {
clearTimeout(timeout);
if (interval) {
clearInterval(interval);
}
};
}, [delay, diff, duration, end, start]);

return (
<span>{(easeOut((value - start) / diff) * diff + start).toFixed(0)}</span>
);
};
2 changes: 1 addition & 1 deletion src/components/DashBoard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const DashBoard: FC = () => {
<h2>Your Shinchoku</h2>
<RingGraph
myAchieves={myAchieveNumber ?? 0}
allMissions={allMissionNumber ?? 1}
allMissions={allMissionNumber ?? 0}
/>
</div>
<div
Expand Down