-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add parallax animation in device screen (#34)
* initial commit
- Loading branch information
1 parent
8240754
commit 5ba4899
Showing
10 changed files
with
259 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
src/components/Graphics/PaxPairingSvg/ParallaxComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
interface ParallaxComponentProps { | ||
children: React.ReactNode; | ||
multiplier?: number; | ||
} | ||
|
||
interface MouseCoordnates { | ||
x: { | ||
current: number; | ||
max: number; | ||
min: number; | ||
perc: number; | ||
}; | ||
y: { | ||
current: number; | ||
max: number; | ||
min: number; | ||
perc: number; | ||
}; | ||
} | ||
|
||
const mouseCoordnatesInitialValue: MouseCoordnates = { | ||
x: { | ||
current: 0, | ||
max: 0, | ||
min: 0, | ||
perc: 0, | ||
}, | ||
y: { | ||
current: 0, | ||
max: 0, | ||
min: 0, | ||
perc: 0, | ||
}, | ||
}; | ||
|
||
/** | ||
* Calculate the y-coordinate in a two-dimensional coordinate system based on the given | ||
* x-coordinate and a percentage between initial and final x-coordinates. | ||
* | ||
* @param {Object} coordinates - An object containing initial and final x and y coordinates | ||
* @param {number} coordinates.x.initial - The initial x-coordinate | ||
* @param {number} coordinates.x.final - The final x-coordinate | ||
* @param {number} coordinates.y.initial - The initial y-coordinate | ||
* @param {number} coordinates.y.final - The final y-coordinate | ||
* @param {number} currentPercentage - The percentage between initial and final x-coordinates | ||
* @return {number} The calculated y-coordinate | ||
*/ | ||
const centerCoordinateSystem = ( | ||
coordinates: { | ||
x: { initial: number; final: number }; | ||
y: { initial: number; final: number }; | ||
}, | ||
currentPercentage: number, | ||
) => { | ||
const m = | ||
(coordinates.y.final - coordinates.y.initial) / | ||
(coordinates.x.final - coordinates.x.initial); | ||
const y = | ||
m * (currentPercentage - coordinates.x.initial) + coordinates.y.initial; | ||
return y; | ||
}; | ||
|
||
/** | ||
* Translate the current percentage to center coordinate system. | ||
* | ||
* @param {number} currentPercentage - the current percentage value to be translated | ||
* @return {number} the calculated value in the center coordinate system | ||
*/ | ||
const translate = (currentPercentage: number) => { | ||
if (currentPercentage > 0.5) { | ||
// 0.51 to 1 | ||
const calculated = centerCoordinateSystem( | ||
{ | ||
x: { | ||
initial: 0.51, | ||
final: 1, | ||
}, | ||
y: { | ||
initial: 0, | ||
final: 1, | ||
}, | ||
}, | ||
currentPercentage, | ||
); | ||
|
||
return calculated; | ||
} else { | ||
// 0 to 0.5 | ||
const calculated = centerCoordinateSystem( | ||
{ | ||
x: { | ||
initial: 0, | ||
final: 0.5, | ||
}, | ||
y: { | ||
initial: -1, | ||
final: 0, | ||
}, | ||
}, | ||
currentPercentage, | ||
); | ||
return calculated; | ||
} | ||
}; | ||
|
||
const ParallaxComponent = (props: ParallaxComponentProps) => { | ||
const { children, multiplier = 10 } = props; | ||
const [mouseCoord, setMouseCoord] = useState<MouseCoordnates>( | ||
mouseCoordnatesInitialValue, | ||
); | ||
|
||
useEffect(() => { | ||
const handleMouseMove = (event: MouseEvent) => { | ||
setMouseCoord({ | ||
x: { | ||
current: event.clientX, | ||
max: window.innerWidth - 1, | ||
min: 0, | ||
perc: event.clientX / window.innerWidth, | ||
}, | ||
y: { | ||
current: event.clientY, | ||
max: window.innerHeight - 1, | ||
min: 0, | ||
perc: event.clientY / window.innerHeight, | ||
}, | ||
}); | ||
}; | ||
|
||
window.addEventListener('mousemove', handleMouseMove); | ||
|
||
return () => { | ||
window.removeEventListener('mousemove', handleMouseMove); | ||
}; | ||
}, []); | ||
|
||
const parallaxStyle: React.CSSProperties = { | ||
transform: `translate(${-translate(mouseCoord.x.perc) * multiplier}px, | ||
${-translate(mouseCoord.y.perc) * multiplier}px)`, | ||
}; | ||
|
||
return ( | ||
<div className="h-full w-full" style={parallaxStyle}> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
|
||
export default ParallaxComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
import { useIsMobile } from '@/hooks'; | ||
import { useState } from 'react'; | ||
|
||
import AddDeviceFooter from '../AddDeviceFooter'; | ||
import { PaxPairing } from '../Graphics'; | ||
|
||
export const NoSelectedDevice = () => { | ||
const isMobile = useIsMobile(); | ||
const [isInputOnFocus, setIsInputOnFocus] = useState(false); | ||
return ( | ||
<div className="mx-auto flex flex-col gap-6 self-center"> | ||
<PaxPairing /> | ||
<AddDeviceFooter /> | ||
<PaxPairing | ||
parallax={!isMobile} | ||
pulsatingLightSpeed={isInputOnFocus ? 'fast' : 'slow'} | ||
/> | ||
<AddDeviceFooter | ||
onFocus={() => setIsInputOnFocus(true)} | ||
onBlur={() => setIsInputOnFocus(false)} | ||
/> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.