add spinning animation to card

This commit is contained in:
Triston Armstrong 2024-02-11 16:37:22 -06:00
parent 6a4b047420
commit c3ded3cf43

View File

@ -1,11 +1,31 @@
import Head from 'next/head' import Head from 'next/head'
import { useState } from 'react'; import { useRef, useState } from 'react';
const cardSpinning = [
{ transform: "rotate3D(0, 1, 0, 360deg)" },
]
const cardSpinningTimer = {
duration: 500,
iterations: 1,
};
export default function Home() { export default function Home() {
const [randomeLetter, setRandomLetter] = useState(_getRandomLetter()) const cardRef = useRef<HTMLDivElement>(null)
const [randomeLetter, setRandomLetter] = useState<LetterType>({
letter: 'ฮ',
phono: 'ho ho',
})
async function _getRandomLetter() {
cardRef.current?.animate(cardSpinning, cardSpinningTimer);
await (new Promise((res, rej) => {
setTimeout(() => {
res(true)
}, 100);
}))
function _getRandomLetter() {
return thaiConsonantsArray[Math.floor(Math.random() * thaiConsonantsArray.length)] return thaiConsonantsArray[Math.floor(Math.random() * thaiConsonantsArray.length)]
} }
@ -15,7 +35,7 @@ export default function Home() {
<title>Thai Stuff</title> <title>Thai Stuff</title>
</Head> </Head>
<div className='card'> <div className='card' ref={cardRef}>
<div> <div>
<p>{randomeLetter.letter}</p> <p>{randomeLetter.letter}</p>
<p>{randomeLetter.phono}</p> <p>{randomeLetter.phono}</p>
@ -25,9 +45,9 @@ export default function Home() {
console.log("pressed listen") console.log("pressed listen")
}}>Listen</button> }}>Listen</button>
<button onClick={async () => { <button onClick={async () => {
let k = _getRandomLetter() let k = await _getRandomLetter()
while (k == randomeLetter) { while (k == randomeLetter) {
k = _getRandomLetter() k = await _getRandomLetter()
} }
setRandomLetter(k) setRandomLetter(k)
}}>Next</button> }}>Next</button>
@ -38,7 +58,7 @@ export default function Home() {
} }
const thaiConsonantsArray = [ const thaiConsonantsArray: LetterType[] = [
{ {
letter: 'ก', letter: 'ก',
phono: 'ko kai', phono: 'ko kai',
@ -48,7 +68,7 @@ const thaiConsonantsArray = [
phono: 'kho khai', phono: 'kho khai',
}, },
{ {
letter: 'ค', letter: 'ค',
phono: 'kho khon', phono: 'kho khon',
}, },
{ {
@ -164,3 +184,8 @@ const thaiConsonantsArray = [
phono: 'ho ho', phono: 'ho ho',
}, },
]; ];
interface LetterType {
letter: string,
phono: string,
}