To incorporate the provided code into a larger context such as a React component, ensure that you have the necessary packages installed and set up. Here’s how you can integrate the code within a React component called Hero.
1. Setup your project dependencies
Make sure you have gsap and @gsap/react installed. You can do this by running:
npm install gsap @gsap/react
2. Ensure File Structure and Assets
Ensure that you have a folder named frames in your public folder containing all the images (1.jpg, 2.jpg, ..., 120.jpg).
3. Integrate the Code
We'll utilize the provided code and set up a React component:
import React, { useEffect, useRef } from 'react';
import { gsap } from 'gsap';
import { ScrollTrigger } from 'gsap/ScrollTrigger';
gsap.registerPlugin(ScrollTrigger);
const Hero = () => {
const container = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
if (!container.current || !canvasRef.current) return;
const canvas = canvasRef.current!;
const context = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const frameCount = 120;
const currentFrame = (index: number) => `/frames/${index + 1}.jpg`;
const images: HTMLImageElement[] = [];
const airpods = { frame: 0 };
for (let i = 0; i < frameCount; i++) {
const img = new Image();
img.src = currentFrame(i);
images.push(img);
}
gsap.timeline({
scrollTrigger: {
trigger: container.current!,
scrub: 1,
pin: true,
pinSpacing: true,
end: "bottom+=2000 bottom",
},
})
.to(".content", { yPercent: -150 })
.to(airpods, {
frame: frameCount - 1,
snap: "frame",
ease: "none",
onUpdate: render,
})
.to(".showoff", { opacity: 1 });
images[0].onload = render;
function render() {
if (context) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(
images[airpods.frame],
0,
0,
canvas.width,
canvas.height
);
}
}
const handleResize = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
render();
};
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
return (
<section ref={container} className="flex relative h-screen overflow-hidden">
<canvas ref={canvasRef} className="absolute h-full w-full inset-0" />
<div className="absolute opacity-0 showoff h-full w-full z-10 inset-0 pt-[72px]">
<div className="container flex justify-between mx-auto">
<div className="flex flex-col gap-12 max-w-[558px]">
<div className="flex flex-col gap-4">
<h4 className="text-primary/50 font-mono uppercase">
the problem
</h4>
<h1 className="text-primary font-display text-5xl">
The Radiology Crisis
</h1>
</div>
<p className="text-primary/70 font-mono text-2xl">
132M medical images are taken each year. But with only a limited
number of radiologists, misdiagnosis rates have reached 5%. The
pressure is mounting, and radiologists are burning out.
</p>
</div>
<div className="flex flex-col gap-24">
<div className="flex flex-col gap-6 max-w-[500px]">
<h2 className="font-display text-9xl text-primary">
50<span className="text-6xl">%</span>
</h2>
<p className="text-2xl font-mono text-primary py-3 border-t-2 border-primary">
of radiologists in the U.S. are over 55.
</p>
</div>
<div className="flex flex-col gap-6 max-w-[500px]">
<h2 className="font-display text-9xl text-primary">2000</h2>
<p className="text-2xl font-mono text-primary py-3 border-t-2 border-primary">
There are 2,000 open radiologist positions and growing.
</p>
</div>
</div>
</div>
</div>
<div className="content container mx-auto w-full flex justify-between">
<div className="flex flex-col relative z-10 gap-8 pb-16 pt-[72px] self-end">
<div className="flex flex-col gap-7 max-w-[460px]">
<h1 className="text-6xl font-bold font-display text-primary">
Radiology with Epsilon Health
</h1>
<p className="font-mono text-primary/60 max-w-[380px]">
Few technologies can directly impact the quality of care on day
one. The right AI can.
</p>
</div>
<button className="bg-light-primary snow text-white flex items-center gap-3 justify-center rounded-full p-1 w-fit">
<span className="inline-block h-8 w-8 bg-white rounded-full" />
<span className="h-fit leading-none translate-y-[2px] mr-4">
Talk to Us
</span>
</button>
</div>
<div className="flex flex-col relative z-10 gap-16 pt-[72px] mt-16">
{[...Array(3)].map((e, i) => (
<div key={i} className="flex w-[330px] min-h-[205px]">
<div className="flex justify-between flex-col w-1/2 p-3.5 h-full relative z-10 bg-light-primary rounded-2xl">
<h4 className="text-white font-mono text-xs">
Sep 05, 2024 03:07 PM
</h4>
<div className="flex flex-col gap-2 text-white font-mono">
<h4 className="text-sm">Clay Douthitt</h4>
<h3 className="text-lg">ID 3218762234</h3>
<p className="rounded-full p-1 w-fit">56 files</p>
</div>
</div>
<div className="flex items-center justify-center rounded-r-2xl flex-col h-full bg-white w-1/2 -ml-3">
<h3>AI analyzing</h3>
<button>Start</button>
</div>
</div>
))}
</div>
</div>
</section>
);
};
export default Hero;
Note
- Ensure you have the correct paths for your image resources.
- Modify styles and class names according to your application’s CSS setup.
- Adjust image paths or effect settings to fit your exact setup and requirements.
This structure includes a useEffect hook to handle component side-effects such as setting up scroll-based animations and handling window resize events for responsive behavior.