From 9c48d638e018f8b071f2399a9d495234e0aa4413 Mon Sep 17 00:00:00 2001 From: Saidev Dhal Date: Sat, 5 Oct 2024 17:35:35 +0530 Subject: [PATCH] update the hero section --- .../ui/placeholders-and-vanish-input.tsx | 65 ++++++++++--------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/components/ui/placeholders-and-vanish-input.tsx b/components/ui/placeholders-and-vanish-input.tsx index 32ce735..74c3972 100644 --- a/components/ui/placeholders-and-vanish-input.tsx +++ b/components/ui/placeholders-and-vanish-input.tsx @@ -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, @@ -18,19 +25,20 @@ export function PlaceholdersAndVanishInput({ onSubmit: (e: React.FormEvent) => void; }) { const [currentPlaceholder, setCurrentPlaceholder] = useState(0); - const intervalRef = useRef(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(); } }; @@ -47,7 +55,7 @@ export function PlaceholdersAndVanishInput({ }, [placeholders]); const canvasRef = useRef(null); - const newDataRef = useRef([]); + const newDataRef = useRef([]); const inputRef = useRef(null); const [value, setValue] = useState(""); const [animating, setAnimating] = useState(false); @@ -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 && @@ -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(); @@ -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) { @@ -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); @@ -176,8 +176,11 @@ export function PlaceholdersAndVanishInput({ const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); vanishAndSubmit(); - onSubmit && onSubmit(e); + if (onSubmit) { + onSubmit(e); + } }; + return (
{ if (!animating) { setValue(e.target.value); - onChange && onChange(e); + if (onChange) { + onChange(e); + } } }} onKeyDown={handleKeyDown} @@ -279,4 +284,4 @@ export function PlaceholdersAndVanishInput({ ); -} \ No newline at end of file +}