fifty-shades-of-bully/src/pages/components/testAnimation_onefile.jsx

82 lines
2.1 KiB
React
Raw Normal View History

import React, { useEffect, useState } from 'react';
import JSZip from 'jszip';
2024-12-30 01:19:31 +07:00
const TestAnimation = () => {
const [images, setImages] = useState([]);
const [currentFrame, setCurrentFrame] = useState(0);
const [loading, setLoading] = useState(true);
2024-12-17 17:36:53 +07:00
const frameRate = 60;
useEffect(() => {
const loadImages = async () => {
const zip = new JSZip();
try {
2024-12-30 01:19:31 +07:00
const response = await fetch('src/assets/mainCharacters/M_Porsche_cross_arm.zip');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.arrayBuffer();
const zipContent = await zip.loadAsync(data);
const imgPromises = [];
zipContent.forEach((relativePath, file) => {
if (file.name.endsWith('.webp')) {
2024-12-17 17:36:53 +07:00
imgPromises.push(
file.async('base64').then(base64 => {
return `data:image/webp;base64,${base64}`;
})
);
}
});
2024-12-17 17:36:53 +07:00
const imgUrls = await Promise.all(imgPromises);
2024-12-17 17:36:53 +07:00
// console.log('Loaded images:', imgUrls);
if (imgUrls.length === 0) {
console.error('No images found in the ZIP file.');
}
setImages(imgUrls);
} catch (error) {
console.error('Error loading images:', error);
} finally {
2024-12-17 17:36:53 +07:00
setLoading(false);
}
};
loadImages();
}, []);
useEffect(() => {
2024-12-17 17:36:53 +07:00
if (images.length > 0) {
const interval = setInterval(() => {
setCurrentFrame((prevFrame) => (prevFrame + 1) % images.length);
}, frameRate);
2024-12-17 17:36:53 +07:00
return () => clearInterval(interval);
}
}, [images]);
if (loading) {
2024-12-17 17:36:53 +07:00
return <div>Loading...</div>;
} else {
return (
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
2024-12-17 17:36:53 +07:00
{images.length > 0 ? (
<img
src={images[currentFrame]}
2024-12-17 17:36:53 +07:00
alt={`Animation frame ${currentFrame + 1}`}
style={{ maxWidth: '500px', height: 'auto' }}
/>
2024-12-17 17:36:53 +07:00
) : (
<div>No images to display.</div>
)}
</div>
);
}
};
2024-12-30 01:19:31 +07:00
export default TestAnimation;