-
-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance Coffee Cup Image with Parallax Effect #54
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,45 @@ | ||
import { useState, useEffect } from 'react'; | ||
import heropic from "../../assets/landing/hero pic.jpg"; | ||
import coffecup from "../../assets/landing/coffecup.png"; | ||
import { motion } from 'framer-motion'; | ||
import AOS from 'aos' | ||
import 'aos/dist/aos.css' | ||
|
||
const parallaxVariants = { | ||
initial: { scale: 1 }, | ||
animate: { scale: 1.05 }, | ||
}; | ||
|
||
const transition = { | ||
type: "spring", | ||
stiffness: 200, | ||
damping: 10, | ||
}; | ||
|
||
export default function Landing() { | ||
useEffect(() => { | ||
AOS.init({ | ||
duration: 1000, | ||
once: true, | ||
}) | ||
}, []) | ||
|
||
const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 }); | ||
|
||
const handleMouseMove = (e) => { | ||
const { clientX, clientY } = e; | ||
setMousePosition({ x: clientX, y: clientY }); | ||
}; | ||
|
||
const parallaxEffect = { | ||
x: mousePosition.x / 100, | ||
y: mousePosition.y / 100, | ||
|
||
|
||
}; | ||
Comment on lines
+34
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adjust The Consider using motion values or updating the +import { useMotionValue, useTransform } from 'framer-motion';
...
+const x = useMotionValue(0);
+const y = useMotionValue(0);
...
const handleMouseMove = (e) => {
- const { clientX, clientY } = e;
- setMousePosition({ x: clientX, y: clientY });
+ const { clientX, clientY } = e;
+ // Calculate the offset from the center of the screen
+ const offsetX = (clientX - window.innerWidth / 2) / 20;
+ const offsetY = (clientY - window.innerHeight / 2) / 20;
+ x.set(offsetX);
+ y.set(offsetY);
};
-const parallaxEffect = {
- x: mousePosition.x / 100,
- y: mousePosition.y / 100,
-};
...
<motion.img
src={coffecup}
alt="Coffee Cup"
className="cursor-pointer"
variants={parallaxVariants}
initial="initial"
animate="animate"
whileHover={{ scale: 1.1 }}
transition={transition}
- style={parallaxEffect }
+ style={{ x, y }}
/> This approach uses Also applies to: 89-91 |
||
return ( | ||
<div> | ||
<section className="relative pb-24 h-screen-dvh w-screen bg-cover bg-center overflow-hidden"> | ||
<section className="relative pb-24 mb-36 h-screen-dvh w-screen bg-cover bg-center overflow-hidden" > | ||
<div className="flex-col md:flex pt-20 z-1"> | ||
{/* Text Content */} | ||
<div className="w-screen p-4 md:w-1/2 mb-6 md:mb-0 text-center md:text-left z-10 pt-14"> | ||
|
@@ -27,7 +61,7 @@ export default function Landing() { | |
</section> | ||
|
||
|
||
<section className="flex flex-row justify-center items-center p-32"> | ||
<section className="flex flex-row justify-center items-center p-32" > | ||
{/* <div className="w-3/5 p-28 mt-20"> | ||
<h1 className="text-8xl font-bold text-black "> | ||
Our name says it all! | ||
|
@@ -50,12 +84,37 @@ export default function Landing() { | |
<button className="p-2 border-2 border-slate-500">Learn more↗️</button> | ||
</div> */} | ||
|
||
<div className="w-1/2 absolute p-4 md:right-40 m-auto"> | ||
<img src={coffecup} alt="" className="w-96 rotate-12"/> | ||
<div className="w-1/2 absolute p-4 md:right-40 m-auto rotate-12" onMouseMove={handleMouseMove}> | ||
<motion.img | ||
src={coffecup} | ||
alt="Coffee Cup" | ||
className="cursor-pointer" | ||
variants={parallaxVariants} | ||
initial="initial" | ||
animate="animate" | ||
whileHover={{ scale: 1.1 }} | ||
transition={transition} | ||
style={parallaxEffect } | ||
|
||
/> | ||
</div> | ||
|
||
<div className="py-28 z-10"> | ||
<h1 className="text-[4rem] md:text-[18rem] font-bold text-black"> | ||
PLAYCAFE | ||
<span | ||
data-aos="fade-left" | ||
data-aos-delay="600" | ||
className="" | ||
> | ||
PLAY | ||
</span> | ||
<span | ||
data-aos="fade-left" | ||
data-aos-delay="1000" | ||
className="" | ||
> | ||
CAFE | ||
</span> | ||
</h1> | ||
</div> | ||
</section> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Throttle
handleMouseMove
for Improved PerformanceUpdating state on every mouse move event can lead to performance issues due to excessive re-renders. To optimize performance, consider throttling the
handleMouseMove
function.You can use
requestAnimationFrame
to limit updates to once per animation frame:This ensures that state updates occur at most once per frame, reducing the render load.
📝 Committable suggestion