Spaces:
Running
Running
Download script.js from masterseb/glasspyramid-planner: direct link, hf CLI and curl.
- Browser
- Download file 7.26 kB
-
https://huggingface.co/spaces/masterseb/glasspyramid-planner/resolve/main/script.js
- Command line
-
hf download hf://spaces/masterseb/glasspyramid-planner/script.js
-
curl -L -o script.js https://huggingface.co/spaces/masterseb/glasspyramid-planner/resolve/main/script.js
7.26 kB
| document.addEventListener('DOMContentLoaded', function() { | |
| // DOM elements | |
| const glassTypeSelect = document.getElementById('glass-type'); | |
| const customGlassDiv = document.getElementById('custom-glass'); | |
| const tableShapeSelect = document.getElementById('table-shape'); | |
| const roundTableDiv = document.getElementById('round-table'); | |
| const rectTableDiv = document.getElementById('rect-table'); | |
| const calculateBtn = document.getElementById('calculate-btn'); | |
| const resultsDiv = document.getElementById('results'); | |
| const noResultsDiv = document.getElementById('no-results'); | |
| const exportBtn = document.getElementById('export-btn'); | |
| // Glass type presets | |
| const glassPresets = { | |
| champagne: { height: 18, diameter: 6 }, | |
| wine: { height: 15, diameter: 6.5 }, | |
| water: { height: 12, diameter: 7 }, | |
| cocktail: { height: 14, diameter: 8 } | |
| }; | |
| // Arrangement pattern - only triangle | |
| const arrangementPattern = (count) => { | |
| // Classic triangle formation | |
| const base = Math.ceil((Math.sqrt(8 * count + 1) - 1) / 2); | |
| return Array.from({length: base}, (_, i) => i + 1); | |
| }; | |
| // Event listeners | |
| glassTypeSelect.addEventListener('change', function() { | |
| if (this.value === 'custom') { | |
| customGlassDiv.classList.remove('hidden'); | |
| } else { | |
| customGlassDiv.classList.add('hidden'); | |
| } | |
| }); | |
| tableShapeSelect.addEventListener('change', function() { | |
| if (this.value === 'round') { | |
| roundTableDiv.classList.remove('hidden'); | |
| rectTableDiv.classList.add('hidden'); | |
| } else { | |
| roundTableDiv.classList.add('hidden'); | |
| rectTableDiv.classList.remove('hidden'); | |
| } | |
| }); | |
| calculateBtn.addEventListener('click', calculatePyramid); | |
| exportBtn.addEventListener('click', exportToPDF); | |
| // Initial setup | |
| feather.replace(); | |
| function calculatePyramid() { | |
| // Get input values | |
| const guests = parseInt(document.getElementById('guests').value) || 0; | |
| if (guests <= 0) { | |
| alert('Veuillez entrer un nombre valide de convives'); | |
| return; | |
| } | |
| // Get glass dimensions | |
| let glassHeight, glassDiameter; | |
| if (glassTypeSelect.value === 'custom') { | |
| glassHeight = parseFloat(document.getElementById('glass-height').value) || 0; | |
| glassDiameter = parseFloat(document.getElementById('glass-diameter').value) || 0; | |
| } else { | |
| const preset = glassPresets[glassTypeSelect.value]; | |
| glassHeight = preset.height; | |
| glassDiameter = preset.diameter; | |
| } | |
| // Get table dimensions | |
| let tableWidth, tableLength; | |
| if (tableShapeSelect.value === 'round') { | |
| const diameter = parseFloat(document.getElementById('table-diameter').value) || 0; | |
| tableWidth = diameter; | |
| tableLength = diameter; | |
| } else { | |
| tableLength = parseFloat(document.getElementById('table-length').value) || 0; | |
| tableWidth = parseFloat(document.getElementById('table-width').value) || 0; | |
| } | |
| // Calculate arrangement | |
| const pattern = arrangementPattern(guests); | |
| const totalGlasses = pattern.reduce((a, b) => a + b, 0); | |
| const maxRow = Math.max(...pattern); | |
| // Calculate pyramid dimensions | |
| const baseGlasses = pattern[pattern.length - 1]; | |
| const pyramidHeight = pattern.length * glassHeight; | |
| const baseArea = maxRow * glassDiameter * pattern.length * glassDiameter * 0.9; | |
| // Check feasibility | |
| const feasible = maxRow * glassDiameter * 0.9 <= Math.min(tableWidth, tableLength); | |
| // Display results | |
| document.getElementById('total-glasses').textContent = totalGlasses; | |
| document.getElementById('base-glasses').textContent = baseGlasses; | |
| document.getElementById('pyramid-height').textContent = pyramidHeight.toFixed(1); | |
| document.getElementById('base-area').textContent = baseArea.toFixed(1) + ' cm²'; | |
| const feasibilityResult = document.getElementById('feasibility-result'); | |
| const feasibilityDiv = document.getElementById('feasibility'); | |
| if (feasible) { | |
| feasibilityResult.textContent = '✓ Disposition possible'; | |
| feasibilityDiv.className = 'bg-white p-4 rounded-lg shadow valid'; | |
| } else { | |
| feasibilityResult.textContent = '✗ Table trop petite'; | |
| feasibilityDiv.className = 'bg-white p-4 rounded-lg shadow invalid'; | |
| } | |
| // Show visualization | |
| visualizeArrangement(pattern, glassDiameter * 10, tableWidth, tableLength); | |
| // Show results | |
| noResultsDiv.classList.add('hidden'); | |
| resultsDiv.classList.remove('hidden'); | |
| } | |
| function visualizeArrangement(pattern, glassSize, tableWidth, tableHeight) { | |
| const container = document.getElementById('visualization'); | |
| container.innerHTML = ''; | |
| // Create canvas | |
| const canvas = document.createElement('canvas'); | |
| canvas.width = container.clientWidth; | |
| canvas.height = container.clientHeight; | |
| container.appendChild(canvas); | |
| const ctx = canvas.getContext('2d'); | |
| const scale = Math.min(canvas.width / (tableWidth * 1.2), canvas.height / (tableHeight * 1.2)); | |
| // Draw table | |
| ctx.fillStyle = '#e2e8f0'; | |
| if (tableShapeSelect.value === 'round') { | |
| const radius = (tableWidth / 2) * scale; | |
| ctx.beginPath(); | |
| ctx.arc(canvas.width / 2, canvas.height / 2, radius, 0, Math.PI * 2); | |
| ctx.fill(); | |
| ctx.strokeStyle = '#94a3b8'; | |
| ctx.stroke(); | |
| } else { | |
| const width = tableWidth * scale; | |
| const height = tableHeight * scale; | |
| ctx.fillRect((canvas.width - width) / 2, (canvas.height - height) / 2, width, height); | |
| ctx.strokeStyle = '#94a3b8'; | |
| ctx.strokeRect((canvas.width - width) / 2, (canvas.height - height) / 2, width, height); | |
| } | |
| // Draw glasses in triangle formation | |
| const glassRadius = (glassSize / 2) * scale; | |
| const centerX = canvas.width / 2; | |
| const centerY = canvas.height / 2; | |
| ctx.fillStyle = 'rgba(147, 51, 234, 0.3)'; | |
| ctx.strokeStyle = 'rgba(147, 51, 234, 0.7)'; | |
| ctx.lineWidth = 1; | |
| // Triangle arrangement | |
| for (let row = 0; row < pattern.length; row++) { | |
| const glassesInRow = pattern[row]; | |
| const y = centerY - (row * glassRadius * 1.5) + (pattern.length * glassRadius * 0.7); | |
| for (let col = 0; col < glassesInRow; col++) { | |
| const x = centerX - ((glassesInRow - 1) * glassRadius) + (col * glassRadius * 2); | |
| ctx.beginPath(); | |
| ctx.arc(x, y, glassRadius, 0, Math.PI * 2); | |
| ctx.fill(); | |
| ctx.stroke(); | |
| } | |
| } | |
| } | |
| function exportToPDF() { | |
| alert('Fonctionnalité PDF en cours de développement'); | |
| // In a real implementation, we would use jsPDF or similar library | |
| } | |
| }); |