Compare commits
2 Commits
c6dfed9bed
...
522fdf106e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
522fdf106e | ||
|
|
d531a9bd6a |
@@ -1,9 +1,30 @@
|
|||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { useAppContext } from '@/app/AppContext';
|
import { useAppContext } from '@/app/AppContext';
|
||||||
|
import throttle from '@/tools/throttle';
|
||||||
|
|
||||||
|
import { thirtyFPS } from '@/constants/time';
|
||||||
import type { Tilt } from '@/types';
|
import type { Tilt } from '@/types';
|
||||||
|
|
||||||
const ZERO_ROTATION = 'rotateX(0deg) rotateY(0deg)';
|
const ZERO_ROTATION = 'rotateX(0deg) rotateY(0deg)';
|
||||||
|
|
||||||
|
const tiltSheen = (sheen: HTMLDivElement, tiltX: number, tiltY: number) => {
|
||||||
|
const rect = sheen.getBoundingClientRect();
|
||||||
|
const centerX = rect.width / 2;
|
||||||
|
const centerY = rect.height / 2;
|
||||||
|
const sheenX = centerX + (tiltY / -20) * centerX;
|
||||||
|
const sheenY = centerY + (tiltX / 20) * centerY;
|
||||||
|
|
||||||
|
sheen.style.opacity = '1';
|
||||||
|
sheen.style.backgroundImage = `
|
||||||
|
radial-gradient(
|
||||||
|
circle at
|
||||||
|
${sheenX}px ${sheenY}px,
|
||||||
|
#ffffff44,
|
||||||
|
#0000000f
|
||||||
|
)
|
||||||
|
`;
|
||||||
|
};
|
||||||
|
|
||||||
export default function TiltCard({
|
export default function TiltCard({
|
||||||
children,
|
children,
|
||||||
cardIndex,
|
cardIndex,
|
||||||
@@ -14,6 +35,7 @@ export default function TiltCard({
|
|||||||
className?: string;
|
className?: string;
|
||||||
}) {
|
}) {
|
||||||
const cardRef = useRef<HTMLDivElement>(null);
|
const cardRef = useRef<HTMLDivElement>(null);
|
||||||
|
const sheenRef = useRef<HTMLDivElement>(null);
|
||||||
const [untilt, setUntilt] = useState(false);
|
const [untilt, setUntilt] = useState(false);
|
||||||
const {
|
const {
|
||||||
gameData,
|
gameData,
|
||||||
@@ -24,7 +46,8 @@ export default function TiltCard({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const card = cardRef.current;
|
const card = cardRef.current;
|
||||||
if (!card) return;
|
const sheen = sheenRef.current;
|
||||||
|
if (!card || !sheen) return;
|
||||||
|
|
||||||
if (tilt) {
|
if (tilt) {
|
||||||
const rotateX = localTilts[cardIndex]?.rotateX || 0;
|
const rotateX = localTilts[cardIndex]?.rotateX || 0;
|
||||||
@@ -47,7 +70,12 @@ export default function TiltCard({
|
|||||||
|
|
||||||
if (count && (totalX || totalY)) {
|
if (count && (totalX || totalY)) {
|
||||||
setUntilt(false);
|
setUntilt(false);
|
||||||
card.style.transform = `rotateX(${totalX / count}deg) rotateY(${totalY / count}deg)`;
|
|
||||||
|
const x = totalX / count;
|
||||||
|
const y = totalY / count;
|
||||||
|
|
||||||
|
card.style.transform = `rotateX(${x}deg) rotateY(${y}deg)`;
|
||||||
|
tiltSheen(sheen, x, y);
|
||||||
} else {
|
} else {
|
||||||
setUntilt(true);
|
setUntilt(true);
|
||||||
}
|
}
|
||||||
@@ -58,12 +86,14 @@ export default function TiltCard({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const card = cardRef.current;
|
const card = cardRef.current;
|
||||||
if (!card || !untilt) return;
|
const sheen = sheenRef.current;
|
||||||
|
if (!card || !sheen || !untilt) return;
|
||||||
|
|
||||||
card.style.transform = ZERO_ROTATION;
|
card.style.transform = ZERO_ROTATION;
|
||||||
|
sheen.style.opacity = '0';
|
||||||
}, [untilt]);
|
}, [untilt]);
|
||||||
|
|
||||||
const handleMouseMove = (e: React.MouseEvent) => {
|
const handleMouseMove = throttle((e: React.MouseEvent) => {
|
||||||
const card = cardRef.current;
|
const card = cardRef.current;
|
||||||
if (!card) return;
|
if (!card) return;
|
||||||
|
|
||||||
@@ -80,7 +110,7 @@ export default function TiltCard({
|
|||||||
newTilt[cardIndex] = { rotateX, rotateY };
|
newTilt[cardIndex] = { rotateX, rotateY };
|
||||||
|
|
||||||
setTilt(newTilt);
|
setTilt(newTilt);
|
||||||
};
|
}, thirtyFPS);
|
||||||
|
|
||||||
const handleMouseLeave = () => {
|
const handleMouseLeave = () => {
|
||||||
setTilt([]);
|
setTilt([]);
|
||||||
@@ -88,7 +118,7 @@ export default function TiltCard({
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={`${className}`}
|
className={`group ${className}`}
|
||||||
onMouseMove={tilt ? handleMouseMove : undefined}
|
onMouseMove={tilt ? handleMouseMove : undefined}
|
||||||
onMouseLeave={handleMouseLeave}
|
onMouseLeave={handleMouseLeave}
|
||||||
>
|
>
|
||||||
@@ -98,6 +128,10 @@ export default function TiltCard({
|
|||||||
className={`h-full w-full transition-transform ${untilt ? 'duration-500' : 'duration-0'}`}
|
className={`h-full w-full transition-transform ${untilt ? 'duration-500' : 'duration-0'}`}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
<div
|
||||||
|
ref={sheenRef}
|
||||||
|
className="pointer-events-none absolute inset-0 rounded-lg bg-gradient-to-tr from-transparent via-white/20 to-transparent mix-blend-screen opacity-0 transition-opacity duration-500"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { socket } from '@/socket';
|
import { socket } from '@/socket';
|
||||||
import throttle from '@/tools/throttle';
|
|
||||||
|
|
||||||
import { thirtyFPS } from '@/constants/time';
|
|
||||||
import type { GameUpdate, Tilt } from '@/types';
|
import type { GameUpdate, Tilt } from '@/types';
|
||||||
|
|
||||||
interface UseSocketProps {
|
interface UseSocketProps {
|
||||||
@@ -70,12 +67,12 @@ export default function useSocket({ gameID, setGameData, setNoGame }: UseSocketP
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const emitTilt = throttle((cardIndex: number, tilt: Tilt) => {
|
const emitTilt = (cardIndex: number, tilt: Tilt) => {
|
||||||
socket.emit('tilt', {
|
socket.emit('tilt', {
|
||||||
cardIndex,
|
cardIndex,
|
||||||
tilt,
|
tilt,
|
||||||
});
|
});
|
||||||
}, thirtyFPS);
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
emitFlip,
|
emitFlip,
|
||||||
|
|||||||
Reference in New Issue
Block a user