-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main 🧊 add use parallax, add use device orientation, add use screen o…
…rientation
- Loading branch information
Showing
10 changed files
with
508 additions
and
12 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
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
14 changes: 14 additions & 0 deletions
14
src/hooks/useDeviceOrientation/useDeviceOrientation.demo.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,14 @@ | ||
import { useDeviceOrientation } from './useDeviceOrientation'; | ||
|
||
const Demo = () => { | ||
const deviceOrientation = useDeviceOrientation(); | ||
|
||
return ( | ||
<pre lang='json'> | ||
<b>Device orientation data:</b> | ||
<p>{JSON.stringify(deviceOrientation.value, null, 2)}</p> | ||
</pre> | ||
); | ||
}; | ||
|
||
export default Demo; |
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,64 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
/* The use device orientation value type */ | ||
export interface UseDeviceOrientationValue { | ||
/** A number representing the motion of the device around the z axis, express in degrees with values ranging from 0 to 360 */ | ||
alpha: number | null | ||
/** A number representing the motion of the device around the x axis, express in degrees with values ranging from -180 to 180 */ | ||
beta: number | null | ||
/** A number representing the motion of the device around the y axis, express in degrees with values ranging from -90 to 90 */ | ||
gamma: number | null | ||
/** The current absolute value */ | ||
absolute: boolean | ||
} | ||
|
||
/* The use device orientation return type */ | ||
export interface UseDeviceOrientationReturn { | ||
/** Whether the device orientation is supported */ | ||
supported: boolean | ||
/** The current device orientation value */ | ||
value: UseDeviceOrientationValue | ||
} | ||
|
||
/** | ||
* @name useDeviceOrientation | ||
* @description - Hook that provides the current device orientation | ||
* @category Sensors | ||
* | ||
* @returns {UseDeviceOrientationReturn} The current device orientation | ||
* | ||
* @example | ||
* const { supported, value } = useDeviceOrientation(); | ||
*/ | ||
export const useDeviceOrientation = (): UseDeviceOrientationReturn => { | ||
const supported = window && 'DeviceOrientationEvent' in window; | ||
|
||
const [value, setValue] = useState<UseDeviceOrientationValue>({ | ||
alpha: null, | ||
beta: null, | ||
gamma: null, | ||
absolute: false | ||
}); | ||
|
||
useEffect(() => { | ||
if (!supported) return; | ||
|
||
const onDeviceOrientation = (event: DeviceOrientationEvent) => | ||
setValue({ | ||
alpha: event.alpha, | ||
beta: event.beta, | ||
gamma: event.gamma, | ||
absolute: event.absolute | ||
}); | ||
|
||
window.addEventListener('deviceorientation', onDeviceOrientation); | ||
return () => { | ||
window.removeEventListener('deviceorientation', onDeviceOrientation); | ||
}; | ||
}, []); | ||
|
||
return { | ||
supported, | ||
value | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import type { CSSProperties } from 'react'; | ||
|
||
import { useParallax } from './useParallax'; | ||
|
||
const Demo = () => { | ||
const parallax = useParallax<HTMLDivElement>(); | ||
|
||
const layerBase: CSSProperties = { | ||
position: 'absolute', | ||
height: '100%', | ||
width: '100%', | ||
transition: '.3s ease-out all' | ||
}; | ||
|
||
const layer0 = { | ||
...layerBase, | ||
transform: `translateX(${parallax.value.tilt * 10}px) translateY(${ | ||
parallax.value.roll * 10 | ||
}px)` | ||
}; | ||
|
||
const layer1 = { | ||
...layerBase, | ||
transform: `translateX(${parallax.value.tilt * 20}px) translateY(${ | ||
parallax.value.roll * 20 | ||
}px)` | ||
}; | ||
|
||
const layer2 = { | ||
...layerBase, | ||
transform: `translateX(${parallax.value.tilt * 30}px) translateY(${ | ||
parallax.value.roll * 30 | ||
}px)` | ||
}; | ||
|
||
const layer3 = { | ||
...layerBase, | ||
transform: `translateX(${parallax.value.tilt * 40}px) translateY(${ | ||
parallax.value.roll * 40 | ||
}px)` | ||
}; | ||
|
||
const cardStyle = { | ||
background: '#fff', | ||
height: '18rem', | ||
width: '14rem', | ||
borderRadius: '5px', | ||
border: '1px solid #cdcdcd', | ||
overflow: 'hidden', | ||
transition: '.3s ease-out all', | ||
boxShadow: '0 0 20px 0 rgba(255, 255, 255, 0.25)', | ||
transform: `rotateX(${parallax.value.roll * 20}deg) rotateY(${ | ||
parallax.value.tilt * 20 | ||
}deg)` | ||
}; | ||
|
||
const containerStyle: CSSProperties = { | ||
margin: '3em auto', | ||
perspective: '200px' | ||
}; | ||
|
||
const cardContentStyle: CSSProperties = { | ||
overflow: 'hidden', | ||
fontSize: '6rem', | ||
position: 'absolute', | ||
top: 'calc(50% - 1em)', | ||
left: 'calc(50% - 1em)', | ||
height: '2em', | ||
width: '2em', | ||
margin: 'auto' | ||
}; | ||
|
||
const targetStyle: CSSProperties = { | ||
display: 'flex', | ||
flexDirection: 'column', | ||
justifyContent: 'center', | ||
minHeight: '500px', | ||
transition: '.3s ease-out all' | ||
}; | ||
|
||
return ( | ||
<div ref={parallax.ref} style={targetStyle}> | ||
<pre lang='json'> | ||
<b>Parallax data:</b> | ||
<p>{JSON.stringify(parallax.value, null, 2)}</p> | ||
</pre> | ||
<div style={containerStyle}> | ||
<div style={cardStyle}> | ||
<div style={cardContentStyle}> | ||
<img | ||
style={layer0} | ||
src='https://jaromvogel.com/images/design/tiger_hunt_parallax/Tiger_hunt_3.png' | ||
alt='layer0' | ||
/> | ||
<img | ||
style={layer1} | ||
src='https://jaromvogel.com/images/design/tiger_hunt_parallax/Tiger_hunt_2.png' | ||
alt='layer1' | ||
/> | ||
<img | ||
style={layer2} | ||
src='https://jaromvogel.com/images/design/tiger_hunt_parallax/Tiger_hunt_1.png' | ||
alt='layer2' | ||
/> | ||
<img | ||
style={layer3} | ||
src='https://jaromvogel.com/images/design/tiger_hunt_parallax/Tiger_hunt_0.png' | ||
alt='layer3' | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
<div> | ||
Credit of images to{' '} | ||
<a | ||
href='https://codepen.io/jaromvogel' | ||
target='__blank' | ||
>Jarom Vogel | ||
</a> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Demo; |
Oops, something went wrong.