Skip to content

Commit

Permalink
animated Pipi in exercise3 using transition
Browse files Browse the repository at this point in the history
  • Loading branch information
lil authored and lil committed Feb 26, 2024
1 parent c711fcb commit 6974405
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
37 changes: 32 additions & 5 deletions src/pages/Exercise3.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FC, useState, useEffect } from 'react';
import { FC, useState, useEffect,useRef } from 'react';
import '../styles/Exercise3.scss';
import Pipi from '../../public/Pipi.svg';
import PipiPointRight from '../../public/PipiPointRight.svg';
import AppWrapper from '../components/AppWrapper';
import Box from '../components/Box';
import Grid from '../components/Grid';
Expand Down Expand Up @@ -40,14 +41,18 @@ const question = [
];

const Exercise3: FC = () => {
const [enableTransition, setEnableTransition] = useState(false)
const [animatedPipi, setAnimatedPipi] = useState(Pipi)
const [confetti, setConfetti] = useState(false);
const [leftOffset, setLeftOffset] = useState(0);
const [topOffset, setTopOffset] = useState(0);

const [clickedCorrectAddress, setClickedCorrectAddress] = useState(false);
const [clickedIncorrectAddress, setClickedIncorrectAddress] = useState(false);
const [selectionMade, setSelectionMade] = useState(false);

const timeForTransition = useRef(false);

const nums = Array.from({ length: 24 }, (_, index) => index + 1);
const itemSpace = [1, 3, 1, 3, 2, 4, 2, 1, 1, 2, 1, 2, 1];

Expand All @@ -63,12 +68,33 @@ const Exercise3: FC = () => {
window.addEventListener('resize', fixPipiPosition);
}, []);

useEffect(() => {
if (enableTransition){
setTimeout(() => {
setAnimatedPipi(PipiPointRight);
setConfetti(true);
}, 1500);
}
}, [enableTransition])



const handleCorrectAddressClick = () => {
setClickedCorrectAddress(true);
setClickedIncorrectAddress(false);
setConfetti(true);
setSelectionMade(true);
};
const addressWidth = 40; // Replace with actual width if different
const newLeftOffset = addressWidth * (9- 1) + leftOffset;
setLeftOffset(newLeftOffset);
timeForTransition.current=true;
}

useEffect(() => {
if (timeForTransition.current) {
setEnableTransition(true);
}
}, [leftOffset]); // Depend on `state1` to trigger this effect

const handleIncorrectAddressClick = () => {
setClickedIncorrectAddress(true);
Expand Down Expand Up @@ -133,13 +159,14 @@ const Exercise3: FC = () => {
<Box letter="m" num={3} conf={false}></Box>
</Grid>
</div>
<img
<img
className="exercise3-pipi"
src={Pipi}
src={animatedPipi}
alt="Pipi"
style={{
left: leftOffset,
top: topOffset,
transition: clickedCorrectAddress? 'left 1.3s' :'none',
}}
/>
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/styles/Exercise3.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

.exercise3-pipi {
position: absolute;
z-index: 2;
z-index: 3;

}

.exercise3-box {
Expand Down

0 comments on commit 6974405

Please sign in to comment.