Untitled Document
Untitled Document
This guide explains how to integrate the EcoPackGEN Packaging Preview API into your
frontend application step-by-step.
Setup Instructions
1. Prerequisites
your-project/
├── index.html # Main HTML file with form and UI components
├── ecopackgen.js # JavaScript file with API integration
└── styles.css # Optional: Additional styling if needed
3. Adding Dependencies
4. HTML Structure
In your index.html file, set up the basic structure including the form elements and 3D viewer
container:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>EcoPackGEN Integration</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body class="bg-gray-100">
<div class="container mx-auto p-4">
<h1 class="text-2xl font-bold mb-6 text-center">EcoPackGEN Packaging Preview</h1>
<script src="ecopackgen.js"></script>
</body>
</html>
5. JavaScript Implementation
Create the ecopackgen.js file with the following code for API integration:
// API Configuration
const API_ENDPOINT = 'https://api.ecopackgen.com/v1/generate';
const API_KEY = 'YOUR_API_KEY_HERE'; // Replace with your actual API key
// Global variables
let scene, camera, renderer, controls, currentModel;
const previewContainer = document.getElementById('preview-container');
// Initialize 3D scene
function initScene() {
// Create scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0xf0f0f0);
// Set up camera
camera = new THREE.PerspectiveCamera(
45,
previewContainer.clientWidth / previewContainer.clientHeight,
0.1,
1000
);
camera.position.set(0, 0, 50);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(previewContainer.clientWidth, previewContainer.clientHeight);
previewContainer.appendChild(renderer.domElement);
// Controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
// Animation loop
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
// Center model
const box = new THREE.Box3().setFromObject(currentModel);
const center = box.getCenter(new THREE.Vector3());
currentModel.position.sub(center);
// Add to scene
scene.add(currentModel);
if (!response.ok) {
throw new Error(`API error: ${response.status} ${response.statusText}`);
}
} catch (error) {
console.error('Error generating preview:', error);
previewContainer.innerHTML = `<div class="flex justify-center items-center h-full">
<p class="text-red-600">Error: ${error.message}</p>
</div>`;
}
}
// Event Listeners
document.addEventListener('DOMContentLoaded', function() {
// Initialize preview with default cube
initScene();
// Form submission
document.getElementById('packageForm').addEventListener('submit', async function(e) {
e.preventDefault();
const formData = {
width: document.getElementById('width').value,
height: document.getElementById('height').value,
depth: document.getElementById('depth').value,
material: document.getElementById('material').value,
color: document.getElementById('color').value,
logo: await fileToBase64(document.getElementById('logo').files[0])
};
generatePackagePreview(formData);
});
// Rotation controls
document.getElementById('rotate-left').addEventListener('click', function() {
if (currentModel) {
currentModel.rotation.y -= Math.PI / 4;
}
});
document.getElementById('rotate-right').addEventListener('click', function() {
if (currentModel) {
currentModel.rotation.y += Math.PI / 4;
}
});
If you need additional styling beyond Tailwind, add it to your styles.css file:
/* Additional custom styles */
#preview-container canvas {
border-radius: 0.5rem;
}
.loading-spinner {
border: 4px solid rgba(0, 0, 0, 0.1);
width: 36px;
height: 36px;
border-radius: 50%;
border-left-color: #09f;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
8. Troubleshooting
● CORS Issues: If you encounter CORS errors, make sure your API key has proper
permissions
● Loading Errors: Check browser console for specific error messages
● Model Rendering Issues: Ensure Three.js and GLTFLoader are properly loaded
Then update your JavaScript to include the template in the API request:
const apiData = {
// ... existing data
template: formData.template
};
1. Security: Never expose your API key in client-side code. Use a backend proxy to make
API calls.
2. Error Handling: Implement comprehensive error handling and user feedback.
3. Loading States: Add loading indicators for all asynchronous operations.
4. Optimization: Optimize 3D models and textures for web performance.
5. Accessibility: Ensure your UI is accessible to all users.
This completes the basic integration guide for the EcoPackGEN Packaging Preview API. For
more advanced features or customization options, refer to the official API documentation.
Setup Instructions
1. Prerequisites
3. Adding Dependencies
4. HTML Structure
In your index.html file, set up the basic structure including the form elements and 3D viewer
container:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>EcoPackGEN Integration</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body class="bg-gray-100">
<div class="container mx-auto p-4">
<h1 class="text-2xl font-bold mb-6 text-center">EcoPackGEN Packaging Preview</h1>
<script src="ecopackgen.js"></script>
</body>
</html>
5. JavaScript Implementation
Create the ecopackgen.js file with the following code for API integration:
// API Configuration
const API_ENDPOINT = 'https://api.ecopackgen.com/v1/generate';
const API_KEY = 'YOUR_API_KEY_HERE'; // Replace with your actual API key
// Global variables
let scene, camera, renderer, controls, currentModel;
const previewContainer = document.getElementById('preview-container');
// Initialize 3D scene
function initScene() {
// Create scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0xf0f0f0);
// Set up camera
camera = new THREE.PerspectiveCamera(
45,
previewContainer.clientWidth / previewContainer.clientHeight,
0.1,
1000
);
camera.position.set(0, 0, 50);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(previewContainer.clientWidth, previewContainer.clientHeight);
previewContainer.appendChild(renderer.domElement);
// Controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
// Animation loop
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
// Center model
const box = new THREE.Box3().setFromObject(currentModel);
const center = box.getCenter(new THREE.Vector3());
currentModel.position.sub(center);
// Add to scene
scene.add(currentModel);
if (!response.ok) {
throw new Error(`API error: ${response.status} ${response.statusText}`);
}
} catch (error) {
console.error('Error generating preview:', error);
previewContainer.innerHTML = `<div class="flex justify-center items-center h-full">
<p class="text-red-600">Error: ${error.message}</p>
</div>`;
}
}
// Event Listeners
document.addEventListener('DOMContentLoaded', function() {
// Initialize preview with default cube
initScene();
// Form submission
document.getElementById('packageForm').addEventListener('submit', async function(e) {
e.preventDefault();
const formData = {
width: document.getElementById('width').value,
height: document.getElementById('height').value,
depth: document.getElementById('depth').value,
material: document.getElementById('material').value,
color: document.getElementById('color').value,
logo: await fileToBase64(document.getElementById('logo').files[0])
};
generatePackagePreview(formData);
});
// Rotation controls
document.getElementById('rotate-left').addEventListener('click', function() {
if (currentModel) {
currentModel.rotation.y -= Math.PI / 4;
}
});
document.getElementById('rotate-right').addEventListener('click', function() {
if (currentModel) {
currentModel.rotation.y += Math.PI / 4;
}
});
If you need additional styling beyond Tailwind, add it to your styles.css file:
.loading-spinner {
border: 4px solid rgba(0, 0, 0, 0.1);
width: 36px;
height: 36px;
border-radius: 50%;
border-left-color: #09f;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
7. Testing Your Integration
8. Troubleshooting
● CORS Issues: If you encounter CORS errors, make sure your API key has proper
permissions
● Loading Errors: Check browser console for specific error messages
● Model Rendering Issues: Ensure Three.js and GLTFLoader are properly loaded
Then update your JavaScript to include the template in the API request:
const apiData = {
// ... existing data
template: formData.template
};
1. Security: Never expose your API key in client-side code. Use a backend proxy to make
API calls.
2. Error Handling: Implement comprehensive error handling and user feedback.
3. Loading States: Add loading indicators for all asynchronous operations.
4. Optimization: Optimize 3D models and textures for web performance.
5. Accessibility: Ensure your UI is accessible to all users.
This completes the basic integration guide for the EcoPackGEN Packaging Preview API. For
more advanced features or customization options, refer to the official API documentation.