flippable sheen
This commit is contained in:
@@ -5,6 +5,7 @@ import { useAppContext } from '@/app/AppContext';
|
|||||||
import TiltCard from '@/components/TiltCard';
|
import TiltCard from '@/components/TiltCard';
|
||||||
import ToolTip from '@/components/ToolTip';
|
import ToolTip from '@/components/ToolTip';
|
||||||
import StackTheDeck from '@/components/StackTheDeck';
|
import StackTheDeck from '@/components/StackTheDeck';
|
||||||
|
import Sheen from '@/components/Sheen';
|
||||||
import getCardInfo from '@/tools/getCardInfo';
|
import getCardInfo from '@/tools/getCardInfo';
|
||||||
import getURL from '@/tools/getURL';
|
import getURL from '@/tools/getURL';
|
||||||
|
|
||||||
@@ -84,6 +85,7 @@ export default function Card({ card, cardIndex }: CardProps) {
|
|||||||
onHover={setTooltip}
|
onHover={setTooltip}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
<Sheen cardIndex={cardIndex} />
|
||||||
</div>
|
</div>
|
||||||
<div className="absolute inset-0 backface-hidden rotate-y-180">
|
<div className="absolute inset-0 backface-hidden rotate-y-180">
|
||||||
<img
|
<img
|
||||||
@@ -91,6 +93,7 @@ export default function Card({ card, cardIndex }: CardProps) {
|
|||||||
alt={aria}
|
alt={aria}
|
||||||
className="rounded-lg border border-yellow-500/25 hover:drop-shadow-[0_0_3px_#ffd700/50]"
|
className="rounded-lg border border-yellow-500/25 hover:drop-shadow-[0_0_3px_#ffd700/50]"
|
||||||
/>
|
/>
|
||||||
|
<Sheen cardIndex={cardIndex} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</TiltCard>
|
</TiltCard>
|
||||||
|
|||||||
88
components/Sheen.tsx
Normal file
88
components/Sheen.tsx
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
import { useAppContext } from '@/app/AppContext';
|
||||||
|
|
||||||
|
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 Sheen({ cardIndex, className }: { cardIndex: number; className?: string }) {
|
||||||
|
const sheenRef = useRef<HTMLDivElement>(null);
|
||||||
|
const [untilt, setUntilt] = useState(false);
|
||||||
|
const {
|
||||||
|
gameData,
|
||||||
|
settings: { tilt, remoteTilt },
|
||||||
|
tilt: localTilts,
|
||||||
|
} = useAppContext();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const sheen = sheenRef.current;
|
||||||
|
if (!sheen) return;
|
||||||
|
|
||||||
|
if (tilt) {
|
||||||
|
const rotateX = localTilts[cardIndex]?.rotateX || 0;
|
||||||
|
const rotateY = localTilts[cardIndex]?.rotateY || 0;
|
||||||
|
|
||||||
|
const tilts = remoteTilt
|
||||||
|
? [...gameData.tilts[cardIndex], { rotateX, rotateY }]
|
||||||
|
: [{ rotateX, rotateY }];
|
||||||
|
|
||||||
|
const { totalX, totalY, count } = tilts
|
||||||
|
.filter(({ rotateX, rotateY }) => !!rotateX && !!rotateY)
|
||||||
|
.reduce(
|
||||||
|
({ totalX, totalY, count }, { rotateX, rotateY }) => ({
|
||||||
|
totalX: totalX + rotateX,
|
||||||
|
totalY: totalY + rotateY,
|
||||||
|
count: ++count,
|
||||||
|
}),
|
||||||
|
{ totalX: 0, totalY: 0, count: 0 },
|
||||||
|
);
|
||||||
|
|
||||||
|
if (count && (totalX || totalY)) {
|
||||||
|
setUntilt(false);
|
||||||
|
|
||||||
|
const x = totalX / count;
|
||||||
|
const y = totalY / count;
|
||||||
|
|
||||||
|
tiltSheen(sheen, x, y);
|
||||||
|
} else {
|
||||||
|
setUntilt(true);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setUntilt(true);
|
||||||
|
}
|
||||||
|
}, [tilt, localTilts, gameData]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const sheen = sheenRef.current;
|
||||||
|
if (!sheen || !untilt) return;
|
||||||
|
|
||||||
|
sheen.style.opacity = '0';
|
||||||
|
}, [untilt]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
ref={sheenRef}
|
||||||
|
className={`
|
||||||
|
absolute inset-0
|
||||||
|
rounded-lg pointer-events-none
|
||||||
|
transition-opacity duration-500
|
||||||
|
bg-gradient-to-tr from-transparent via-white/20 to-transparent mix-blend-screen opacity-0
|
||||||
|
${className}
|
||||||
|
`}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -7,24 +7,6 @@ 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,
|
||||||
@@ -35,7 +17,6 @@ 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,
|
||||||
@@ -46,8 +27,7 @@ export default function TiltCard({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const card = cardRef.current;
|
const card = cardRef.current;
|
||||||
const sheen = sheenRef.current;
|
if (!card) return;
|
||||||
if (!card || !sheen) return;
|
|
||||||
|
|
||||||
if (tilt) {
|
if (tilt) {
|
||||||
const rotateX = localTilts[cardIndex]?.rotateX || 0;
|
const rotateX = localTilts[cardIndex]?.rotateX || 0;
|
||||||
@@ -75,7 +55,6 @@ export default function TiltCard({
|
|||||||
const y = totalY / count;
|
const y = totalY / count;
|
||||||
|
|
||||||
card.style.transform = `rotateX(${x}deg) rotateY(${y}deg)`;
|
card.style.transform = `rotateX(${x}deg) rotateY(${y}deg)`;
|
||||||
tiltSheen(sheen, x, y);
|
|
||||||
} else {
|
} else {
|
||||||
setUntilt(true);
|
setUntilt(true);
|
||||||
}
|
}
|
||||||
@@ -86,11 +65,9 @@ export default function TiltCard({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const card = cardRef.current;
|
const card = cardRef.current;
|
||||||
const sheen = sheenRef.current;
|
if (!card || !untilt) return;
|
||||||
if (!card || !sheen || !untilt) return;
|
|
||||||
|
|
||||||
card.style.transform = ZERO_ROTATION;
|
card.style.transform = ZERO_ROTATION;
|
||||||
sheen.style.opacity = '0';
|
|
||||||
}, [untilt]);
|
}, [untilt]);
|
||||||
|
|
||||||
const handleMouseMove = throttle((e: React.MouseEvent) => {
|
const handleMouseMove = throttle((e: React.MouseEvent) => {
|
||||||
@@ -128,10 +105,6 @@ 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>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user