Skip to content

Commit

Permalink
update the hero section
Browse files Browse the repository at this point in the history
  • Loading branch information
SkidGod4444 committed Oct 5, 2024
1 parent 06bfd02 commit 9c48d63
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions components/ui/placeholders-and-vanish-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { AnimatePresence, motion } from "framer-motion";
import { useCallback, useEffect, useRef, useState } from "react";
import { cn } from "@/lib/utils";

interface PixelData {
x: number;
y: number;
r: number;
color: string;
}

export function PlaceholdersAndVanishInput({
placeholders,
onChange,
Expand All @@ -18,19 +25,20 @@ export function PlaceholdersAndVanishInput({
onSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
}) {
const [currentPlaceholder, setCurrentPlaceholder] = useState(0);

const intervalRef = useRef<NodeJS.Timeout | null>(null);

const startAnimation = () => {
intervalRef.current = setInterval(() => {
setCurrentPlaceholder((prev) => (prev + 1) % placeholders.length);
}, 3000);
};

const handleVisibilityChange = () => {
if (document.visibilityState !== "visible" && intervalRef.current) {
clearInterval(intervalRef.current); // Clear the interval when the tab is not visible
clearInterval(intervalRef.current);
intervalRef.current = null;
} else if (document.visibilityState === "visible") {
startAnimation(); // Restart the interval when the tab becomes visible
startAnimation();
}
};

Expand All @@ -47,7 +55,7 @@ export function PlaceholdersAndVanishInput({
}, [placeholders]);

const canvasRef = useRef<HTMLCanvasElement>(null);
const newDataRef = useRef<any[]>([]);
const newDataRef = useRef<PixelData[]>([]);
const inputRef = useRef<HTMLInputElement>(null);
const [value, setValue] = useState("");
const [animating, setAnimating] = useState(false);
Expand All @@ -58,25 +66,25 @@ export function PlaceholdersAndVanishInput({
if (!canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;

canvas.width = 800;
canvas.height = 800;
ctx.clearRect(0, 0, 800, 800);
const computedStyles = getComputedStyle(inputRef.current);

const fontSize = parseFloat(computedStyles.getPropertyValue("font-size"));
ctx.font = `${fontSize * 2}px ${computedStyles.fontFamily}`;
ctx.fillStyle = "#FFF";
ctx.fillText(value, 16, 40);

const imageData = ctx.getImageData(0, 0, 800, 800);
const pixelData = imageData.data;
const newData: any[] = [];

const newData: PixelData[] = [];
for (let t = 0; t < 800; t++) {
let i = 4 * t * 800;
const i = 4 * t * 800;
for (let n = 0; n < 800; n++) {
let e = i + 4 * n;
const e = i + 4 * n;
if (
pixelData[e] !== 0 &&
pixelData[e + 1] !== 0 &&
Expand All @@ -85,24 +93,16 @@ export function PlaceholdersAndVanishInput({
newData.push({
x: n,
y: t,
color: [
pixelData[e],
pixelData[e + 1],
pixelData[e + 2],
pixelData[e + 3],
],
r: 1,
color: `rgba(${pixelData[e]}, ${pixelData[e + 1]}, ${pixelData[e + 2]}, ${pixelData[e + 3] / 255})`, // Corrected here
});
}
}
}

newDataRef.current = newData.map(({ x, y, color }) => ({
x,
y,
r: 1,
color: `rgba(${color[0]}, ${color[1]}, ${color[2]}, ${color[3]})`,
}));

newDataRef.current = newData;
}, [value]);


useEffect(() => {
draw();
Expand All @@ -111,7 +111,7 @@ export function PlaceholdersAndVanishInput({
const animate = (start: number) => {
const animateFrame = (pos: number = 0) => {
requestAnimationFrame(() => {
const newArr = [];
const newArr: PixelData[] = [];
for (let i = 0; i < newDataRef.current.length; i++) {
const current = newDataRef.current[i];
if (current.x < pos) {
Expand All @@ -132,7 +132,7 @@ export function PlaceholdersAndVanishInput({
if (ctx) {
ctx.clearRect(pos, 0, 800, 800);
newDataRef.current.forEach((t) => {
const { x: n, y: i, r: s, color: color } = t;
const { x: n, y: i, r: s, color } = t;
if (n > pos) {
ctx.beginPath();
ctx.rect(n, i, s, s);
Expand Down Expand Up @@ -176,8 +176,11 @@ export function PlaceholdersAndVanishInput({
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
vanishAndSubmit();
onSubmit && onSubmit(e);
if (onSubmit) {
onSubmit(e);
}
};

return (
<form
className={cn(
Expand All @@ -188,7 +191,7 @@ export function PlaceholdersAndVanishInput({
>
<canvas
className={cn(
"absolute pointer-events-none text-base transform scale-50 top-[20%] left-2 sm:left-8 origin-top-left filter invert dark:invert-0 pr-20",
"absolute pointer-events-none text-base transform scale-50 top-[20%] left-2 sm:left-8 origin-top-left filter invert dark:invert-0 pr-20",
!animating ? "opacity-0" : "opacity-100",
)}
ref={canvasRef}
Expand All @@ -197,7 +200,9 @@ export function PlaceholdersAndVanishInput({
onChange={(e) => {
if (!animating) {
setValue(e.target.value);
onChange && onChange(e);
if (onChange) {
onChange(e);
}
}
}}
onKeyDown={handleKeyDown}
Expand Down Expand Up @@ -279,4 +284,4 @@ export function PlaceholdersAndVanishInput({
</div>
</form>
);
}
}

0 comments on commit 9c48d63

Please sign in to comment.