This commit is contained in:
imnyang 2025-01-20 21:46:27 +09:00
commit 3855fb8afb
2 changed files with 44 additions and 6 deletions

View file

@ -10,18 +10,16 @@ body {
} }
#avatar { #avatar {
display: inline-block;
font-size: 100px;
transition: transform 0.5s linear; transition: transform 0.5s linear;
} }
#avatar:hover {
animation: rotate 1s linear infinite;
}
@keyframes rotate { @keyframes rotate {
0% { 0% {
transform: rotate(0deg); transform: rotate(var(--rotate-angle, 0deg));
} }
100% { 100% {
transform: rotate(360deg); transform: rotate(calc(var(--rotate-angle, 0deg) + 360deg));
} }
} }

View file

@ -1,4 +1,36 @@
import React, { useState, useRef } from "react";
export default function Top() { export default function Top() {
const [angle, setAngle] = useState(0);
const avatarRef = useRef<HTMLImageElement | null>(null); // Ref 타입 변경
const handleMouseEnter = () => {
if (avatarRef.current) {
avatarRef.current.style.animation = `rotate 1s linear infinite`;
}
};
const handleMouseLeave = () => {
if (avatarRef.current) {
const computedStyle = window.getComputedStyle(avatarRef.current);
const matrix = computedStyle.transform;
if (matrix && matrix !== "none") {
const values = matrix.match(/matrix\((.+)\)/)?.[1].split(", ");
if (values) {
const a = parseFloat(values[0]);
const b = parseFloat(values[1]);
const currentAngle = Math.round(Math.atan2(b, a) * (180 / Math.PI));
setAngle((prev) =>
currentAngle >= 0 ? currentAngle : currentAngle + 360
);
}
}
avatarRef.current.style.animation = "none";
avatarRef.current.style.transform = `rotate(${angle}deg)`;
}
};
return ( return (
<div <div
id="top" id="top"
@ -10,6 +42,14 @@ export default function Top() {
width={200} width={200}
height={200} height={200}
id="avatar" id="avatar"
ref={avatarRef}
style={
{
"--rotate-angle": `${angle}deg`,
} as React.CSSProperties
}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
className="rounded-full" className="rounded-full"
/> />
<div> <div>