Spaces:
Build error
Build error
File size: 4,339 Bytes
75fefa7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
"use client";
import { animate } from "motion";
import { useEffect, useRef } from "react";
import { cn } from "@/utils/cn";
import initCanvas from "@/utils/init-canvas";
export default function EndpointsExtract({
active,
disabledCells,
alwaysHeat = false,
triggerOnHover = false,
size = 20,
}: {
active?: boolean;
disabledCells?: number[];
alwaysHeat?: boolean;
triggerOnHover?: boolean;
size?: number;
}) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const fnRefs = useRef<{
activate: () => void;
deactivate: () => void;
}>({ activate: () => {}, deactivate: () => {} });
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = initCanvas(canvas);
let isRunning = false;
let isActive = false;
let activeCol = 0;
const colAlphas = [1, 0.4, 0.2, 0.12];
const scaler = size / 20;
const render = () => {
ctx.fillStyle = "#FF4C00";
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Extract pattern - represents structured data extraction
// Draw columns to represent data fields
for (let col = 0; col < 4; col++) {
ctx.globalAlpha = colAlphas[col];
// Draw vertical bars of different heights to represent extracted data
const heights = [3, 2, 3, 1];
const startY = [1, 2, 1, 3];
for (let row = 0; row < heights[col]; row++) {
ctx.fillRect(
(3 + col * 4) * scaler,
(3 + startY[col] * 2 + row * 4) * scaler,
2 * scaler,
2 * scaler,
);
}
}
if (isRunning) {
requestAnimationFrame(render);
}
};
const timeouts: number[] = [];
let runCount = 0;
const cycle = () => {
isRunning = true;
activeCol = (activeCol + 1) % 4;
colAlphas.forEach((alpha, index) => {
let targetAlpha = alpha;
if (index === activeCol) targetAlpha = 1;
else if (index === (activeCol + 1) % 4) targetAlpha = 0.12;
else if (index === (activeCol + 2) % 4) targetAlpha = 0.2;
else if (index === (activeCol + 3) % 4) targetAlpha = 0.4;
animate(alpha, targetAlpha, {
duration: 0.05,
onUpdate: (value) => {
colAlphas[index] = value;
},
});
});
timeouts.forEach((timeout) => {
window.clearTimeout(timeout);
});
timeouts.push(
window.setTimeout(() => {
isRunning = false;
}, 400),
);
if (activeCol === 3) runCount += 1;
if ((runCount === 2 || !isActive) && activeCol === 0) return;
timeouts.push(
window.setTimeout(() => {
cycle();
}, 50),
);
};
fnRefs.current = {
activate: () => {
if (isActive) return;
isActive = true;
runCount = 0;
cycle();
render();
},
deactivate: () => {
if (!isActive) return;
isActive = false;
},
};
render();
canvas.addEventListener("resize", render);
if (triggerOnHover) {
const group = canvasRef.current!.closest(".group");
if (group) {
group.addEventListener("mouseenter", fnRefs.current.activate);
group.addEventListener("mouseleave", fnRefs.current.deactivate);
return () => {
group.removeEventListener("mouseenter", fnRefs.current.activate);
group.removeEventListener("mouseleave", fnRefs.current.deactivate);
};
}
}
}, [disabledCells, size, triggerOnHover]);
useEffect(() => {
if (triggerOnHover) return;
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting && active) {
fnRefs.current.activate();
} else {
fnRefs.current.deactivate();
}
},
{ threshold: 0.5 },
);
observer.observe(canvasRef.current!);
return () => {
observer.disconnect();
};
}, [active, triggerOnHover]);
return (
<canvas
className={cn(
alwaysHeat
? ""
: [
"[&.grayscale]:opacity-60 transition-[filter,opacity]",
!active && "grayscale",
],
)}
ref={canvasRef}
style={{ width: size, height: size }}
/>
);
}
|