mirror of
https://github.com/runyanjake/jakesphotos.git
synced 2025-10-04 23:47:30 -07:00
Shuffle image
This commit is contained in:
parent
429110ce61
commit
8843034a15
@ -3,14 +3,26 @@ import Masonry from 'masonry-layout';
|
|||||||
import images from '../data/Images'; // Import the image URLs
|
import images from '../data/Images'; // Import the image URLs
|
||||||
import './Home.css'; // Ensure you have your CSS for styling
|
import './Home.css'; // Ensure you have your CSS for styling
|
||||||
|
|
||||||
|
// Shuffle function to randomize the array
|
||||||
|
const shuffleArray = (array) => {
|
||||||
|
for (let i = array.length - 1; i > 0; i--) {
|
||||||
|
const j = Math.floor(Math.random() * (i + 1));
|
||||||
|
[array[i], array[j]] = [array[j], array[i]]; // Swap elements
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
};
|
||||||
|
|
||||||
const Home = () => {
|
const Home = () => {
|
||||||
const masonryRef = useRef(null);
|
const masonryRef = useRef(null);
|
||||||
|
|
||||||
|
// Shuffle images on component mount
|
||||||
|
const shuffledImages = shuffleArray([...images]); // Create a copy and shuffle
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const masonry = new Masonry(masonryRef.current, {
|
const masonry = new Masonry(masonryRef.current, {
|
||||||
itemSelector: '.gallery-photo',
|
itemSelector: '.gallery-photo',
|
||||||
columnWidth: '.gallery-photo',
|
columnWidth: '.gallery-photo',
|
||||||
percentPosition: true
|
percentPosition: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Layout Masonry after images have loaded
|
// Layout Masonry after images have loaded
|
||||||
@ -33,8 +45,8 @@ const Home = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="gallery" ref={masonryRef}>
|
<div className="gallery" ref={masonryRef}>
|
||||||
{images.map((image, index) => (
|
{shuffledImages.map((image, index) => (
|
||||||
<img key={index} src={image} alt={`Jake Runyan ${index + 1}`} className="gallery-photo" />
|
<img key={index} src={image} alt={`© Jake Runyan ${index + 1}`} className="gallery-photo" />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user