Advertisement
Guest User

Honeycombs

a guest
Dec 19th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Fetch Images
  2. import fs from 'fs';
  3. import path from 'path';
  4.  
  5. export default function handler(req, res) {
  6.   const folderPath = path.join(process.cwd(), 'public/images/');
  7.  
  8.   try {
  9.     const files = fs.readdirSync(folderPath);
  10.     const fileData = files.map((fileName) => {
  11.       const filePath = `/images/${fileName}`;
  12.       const alt = path.parse(fileName).name; // Get the filename without extension
  13.       return { imageUrl: filePath, alt };
  14.     });
  15.     res.status(200).json(fileData);
  16.   } catch (error) {file
  17.     console.error(error);
  18.     res.status(500).json({ error: 'An error occurred while reading files.' });
  19.   }
  20. }
  21. //Honeycomb builder
  22. import React, {useEffect , useState} from 'react';
  23. import Image from 'next/image';
  24. import { getColorSimple } from './layouts/stackColor';
  25. const Honeycomb = ({ imageUrl, alt, width, height ,maxWidth}) => {
  26.     const [HexColor, setHexColor] = useState('');
  27.  
  28.     useEffect(() => {
  29.       // Call the function with the image path and handle the Promise
  30.       getColorSimple(imageUrl)
  31.         .then((result) => {
  32.           const hexColor = result.rgb;
  33.           setHexColor(hexColor);
  34.         })
  35.         .catch((error) => {
  36.           console.error('Error:', error);
  37.         });
  38.     }, []);
  39.   const hexagonStyles = {
  40.     hexagon: {
  41.       width: width,
  42.       maxWidth: maxWidth,
  43.       height: height,
  44.       position: 'relative',
  45.       margin: '0 10px',
  46.       padding:'1.2%',
  47.       clipPath: 'polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%)',
  48.       display: 'flex',
  49.       alignItems: 'center', // Center vertically
  50.       justifyContent: 'center', // Center horizontally
  51.       backgroundColor: HexColor,
  52.       objectFit: 'contain'
  53.     }
  54.   };
  55.   // Conditionally apply the 'javascript' class if 'alt' has a specific value
  56.  
  57.     return (
  58.      
  59.         <Image src={imageUrl} alt={alt} width={100} height={100} style={hexagonStyles.hexagon}/>
  60.      
  61.     );  
  62. };
  63.  
  64. export default Honeycomb;
  65.  
  66. //Honeycomb pattern
  67. import React from 'react';
  68. import Honeycomb from '../honeycomb';
  69.  
  70. const HoneycombGrid = ({maxHeight, evenRows, oddRows, paddingVal}) => {
  71.   const gridStyles = {
  72.     display: 'grid',
  73.     justifyContent: 'center',
  74.   };
  75.   var paddingVar = paddingVal + '%';
  76.   const [honeycombs, setFileData] = React.useState([]);
  77.  
  78.   React.useEffect(() => {
  79.     fetch('/api/stack')
  80.       .then((response) => response.json())
  81.       .then((data) => setFileData(data))
  82.       .catch((error) => console.error(error));
  83.   }, []);
  84.  
  85. var hexagonsPerRow = evenRows;
  86. const rows = [];
  87. for (let i = 0; i < honeycombs.length; i += hexagonsPerRow) {
  88.   hexagonsPerRow = i % (evenRows + oddRows) === 0 ? evenRows : oddRows;
  89.   const rowHoneycombs =
  90.     i === 0
  91.       ? honeycombs
  92. .slice(i, i + hexagonsPerRow)
  93.       : i % (hexagonsPerRow * 2) === 0
  94.       ? honeycombs.slice(i - 1, i + hexagonsPerRow)
  95.       : honeycombs.slice(i, i + hexagonsPerRow);
  96.   const rowStyles =
  97.     hexagonsPerRow - rowHoneycombs.length > 0
  98.       ?
  99.       rowHoneycombs.length % 2 === 0 ?
  100.       //Alignment For Stragglers Based On Size (Even Or Odd)
  101.       {
  102.         maxHeight: maxHeight,
  103.         display: 'flex',
  104.         justifyContent: 'center',
  105.         paddingRight: paddingVar,
  106.       } :
  107.       {
  108.         maxHeight: maxHeight,
  109.         display: 'flex',
  110.         justifyContent: 'center',
  111.         paddingRight: paddingVar,
  112.       }
  113.       :
  114.       //Natural Alignment Through Flex Center
  115.       {
  116.         maxHeight: maxHeight,
  117.         display: 'flex',
  118.         justifyContent: 'center'
  119.       }
  120.     rows.push(
  121.       <div key={`row-${i}`} style={rowStyles}>
  122.         {rowHoneycombs.map((honeycomb, index) => (
  123.           <Honeycomb
  124.             key={index}
  125.             imageUrl={honeycomb.imageUrl}
  126.             alt={honeycomb.alt}
  127.             width={'auto'}
  128.             height={'100%'}
  129.             maxWidth={maxHeight}
  130.           />
  131.           ))}
  132.       </div>
  133.     );
  134.   }
  135.   return <div style={gridStyles}>{rows}</div>;
  136. };
  137.  
  138. export default HoneycombGrid;
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement