File size: 5,184 Bytes
c004381 a5d8f3d 444dd08 a5d8f3d c004381 a5d8f3d 444dd08 a5d8f3d c004381 a5d8f3d c004381 a5d8f3d c004381 a5d8f3d 1657b45 a5d8f3d c004381 a5d8f3d c004381 a5d8f3d c004381 a5d8f3d 1657b45 c004381 a5d8f3d 1657b45 e3a3e87 a5d8f3d c004381 a5d8f3d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
document.addEventListener('DOMContentLoaded', () => {
// Material options mapping
const materials = {
'cr39': { name: 'CR-39', index: 1.5 },
'policarbonato': { name: 'Policarbonato', index: 1.586 },
'trivex': { name: 'Trivex', index: 1.53 },
'alto_indice_1_6': { name: 'Alto Índice 1.6', index: 1.6 },
'alto_indice_1_67': { name: 'Alto Índice 1.67', index: 1.67 },
'alto_indice_1_74': { name: 'Alto Índice 1.74', index: 1.74 }
};
// Setup material selector
const materialSelect = document.createElement('select');
materialSelect.id = 'materialSelect';
materialSelect.className = 'w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500';
materialSelect.innerHTML = `
<option value="">Selecione um material...</option>
${Object.entries(materials).map(([key, mat]) =>
`<option value="${key}" ${key === 'cr39' ? 'selected' : ''}>${mat.name} (n = ${mat.index})</option>`
).join('')}
`;
materialSelect.addEventListener('change', function() {
const selected = materials[this.value];
if (selected) {
document.getElementById('indice_n').value = selected.index;
}
});
// Insert material selector before index input
const indexInput = document.getElementById('indice_n');
indexInput.parentNode.insertBefore(materialSelect, indexInput);
// Form submission handler
const lensForm = document.getElementById('lensForm');
lensForm.addEventListener('submit', function(event) {
event.preventDefault();
calcularEspessura();
});
function calcularEspessura() {
// 1. Collect and convert form data
const de = parseFloat(document.getElementById('esferico').value) || 0;
const dc = parseFloat(document.getElementById('cilindrico').value) || 0;
const eixo = parseFloat(document.getElementById('eixo').value) || 0;
const indice_n = parseFloat(document.getElementById('indice_n').value);
const diametro = parseFloat(document.getElementById('diametro').value);
const maior_diagonal_ed = parseFloat(document.getElementById('maior_diagonal_ed').value);
const dnp = parseFloat(document.getElementById('dnp').value);
let espessura_centro_ec;
// Auto-determine center thickness based on prescription
if (de < 0) { // Negative lenses (myopia)
espessura_centro_ec = 2.0;
} else { // Positive lenses (hyperopia)
// For positive lenses, calculate edge thickness first
const decentracao = Math.abs((maior_diagonal_ed / 2) - dnp);
const raio_efetivo = (maior_diagonal_ed / 2) + decentracao;
const angulo_rad = Math.PI * (180 - eixo) / 180;
const potencia_efetiva_meridiano = de + (dc * (Math.sin(angulo_rad)**2));
const sagita = (potencia_efetiva_meridiano * raio_efetivo**2) / (2000 * (indice_n - 1));
espessura_centro_ec = sagita + 1.5; // Edge thickness of 1.5mm for positive lenses
}
// 2. Data validation
const resultadoDiv = document.getElementById('resultado');
const resultContent = document.getElementById('resultContent');
if (isNaN(indice_n) || isNaN(diametro) || isNaN(maior_diagonal_ed) || isNaN(dnp)) {
resultadoDiv.classList.remove('hidden');
resultContent.innerHTML = `<p class="font-bold text-red-600">Erro: Preencha todos os campos obrigatórios com valores numéricos.</p>`;
return;
}
if (indice_n <= 1) {
resultadoDiv.classList.remove('hidden');
resultContent.innerHTML = `<p class="font-bold text-red-600">Erro: O Índice de Refração deve ser maior que 1.</p>`;
return;
}
// 3. Main calculations (corrected logic)
const decentracao = Math.abs((maior_diagonal_ed / 2) - dnp);
const raio_efetivo = (maior_diagonal_ed / 2) + decentracao;
const angulo_rad = Math.PI * (180 - eixo) / 180;
const potencia_efetiva_meridiano = de + (dc * (Math.sin(angulo_rad)**2));
const sagita = (potencia_efetiva_meridiano * raio_efetivo**2) / (2000 * (indice_n - 1));
const espessura_borda = de < 0 ? (espessura_centro_ec - sagita) : 1.5;
// 4. Display results
resultadoDiv.classList.remove('hidden');
resultContent.innerHTML = `
<p><strong>Material:</strong> ${materialSelect.options[materialSelect.selectedIndex].text || 'Personalizado'}</p>
<p><strong>Espessura no Centro:</strong> ${espessura_centro_ec.toFixed(2)} mm</p>
<p class="mt-2 text-xl font-bold text-blue-800">
<strong>Espessura Máxima Estimada na Borda:</strong>
<span class="text-blue-600 font-extrabold">${Math.abs(espessura_borda).toFixed(2)} mm</span>
</p>
${espessura_borda < 0 ? '<p class="text-yellow-600 font-semibold">Atenção: Valor negativo indica que a lente pode ficar muito fina!</p>' : ''}
`;
resultadoDiv.scrollIntoView({ behavior: 'smooth' });
feather.replace();
}
});
|