Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
126 lines (106 loc) · 5.2 KB
/
Copy pathapp.js
File metadata and controls
126 lines (106 loc) · 5.2 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('carbon-form');
const resultCard = document.getElementById('result-card');
const closeResultBtn = document.getElementById('close-result');
const totalScoreEl = document.getElementById('total-score');
const progressCircle = document.getElementById('score-progress');
const resultTitle = document.getElementById('result-title');
const resultMessage = document.getElementById('result-message');
// Breakdowns
const elElectricity = document.getElementById('breakdown-electricity');
const elTransport = document.getElementById('breakdown-transport');
const elDiet = document.getElementById('breakdown-diet');
// Indian Emission Factors
// Electricity: Grid average in India is roughly 0.82 kg CO2 per kWh
const ELECTRICITY_FACTOR = 0.82;
// Transport (kg CO2 per km)
const TRANSPORT_FACTORS = {
'2-wheeler': 0.04, // highly efficient Indian scooters
'4-wheeler-petrol': 0.14,
'4-wheeler-diesel': 0.16,
'public-bus': 0.02, // per passenger km
'metro': 0.01,
'ev': 0.01 // Assuming grid electricity emissions are accounted separately or negligible per km
};
// Diet (kg CO2 per year)
// Very rough estimations tailored for India
const DIET_FACTORS = {
'vegan': 600,
'vegetarian': 900, // dairy is significant in India
'non-veg-rare': 1200, // mostly chicken/fish
'non-veg-frequent': 1800 // mutton/beef/frequent chicken
};
form.addEventListener('submit', (e) => {
e.preventDefault();
// 1. Gather Inputs
const electricityKwh = parseFloat(document.getElementById('electricity').value) || 0;
const transportKm = parseFloat(document.getElementById('transport').value) || 0;
const vehicleType = document.getElementById('vehicle').value;
const dietType = document.getElementById('diet').value;
// 2. Calculate Annual Emissions
// Electricity (monthly to annual)
const electricityAnnualCO2 = (electricityKwh * 12) * ELECTRICITY_FACTOR;
// Transport (daily to annual, assuming 300 days of travel to be realistic)
const transportAnnualCO2 = (transportKm * 300) * (TRANSPORT_FACTORS[vehicleType] || 0.1);
// Diet (already annual)
const dietAnnualCO2 = DIET_FACTORS[dietType] || 1000;
// Total in kg
const totalKgCO2 = electricityAnnualCO2 + transportAnnualCO2 + dietAnnualCO2;
// Convert to Tons
const totalTonsCO2 = totalKgCO2 / 1000;
// 3. Update UI
updateSummaryCard(totalTonsCO2, electricityAnnualCO2, transportAnnualCO2, dietAnnualCO2);
});
closeResultBtn.addEventListener('click', () => {
resultCard.classList.remove('result-visible');
});
function updateSummaryCard(tons, elecKg, transKg, dietKg) {
// Show card
resultCard.classList.add('result-visible');
// Update breakdowns
elElectricity.textContent = Math.round(elecKg).toLocaleString() + ' kg';
elTransport.textContent = Math.round(transKg).toLocaleString() + ' kg';
elDiet.textContent = Math.round(dietKg).toLocaleString() + ' kg';
// Animate counter
animateValue(totalScoreEl, 0, tons, 1000);
// Animate SVG Ring
// Circumference is 2 * pi * r = 2 * 3.1415 * 88 = ~553
// Max sensible score for visual scale: 10 tons
const maxScale = 10;
const percentage = Math.min(tons / maxScale, 1);
const dashoffset = 553 - (553 * percentage);
setTimeout(() => {
progressCircle.style.strokeDashoffset = dashoffset;
// Color logic based on Indian average (1.9 tons)
if (tons < 1.5) {
progressCircle.style.stroke = '#10b981'; // Emerald
resultTitle.textContent = "Excellent!";
resultMessage.textContent = "Your footprint is well below the national average. Great job!";
} else if (tons < 2.5) {
progressCircle.style.stroke = '#eab308'; // Yellow
resultTitle.textContent = "Average";
resultMessage.textContent = "You are around the Indian national average.";
} else {
progressCircle.style.stroke = '#ef4444'; // Red
resultTitle.textContent = "Above Average";
resultMessage.textContent = "Your footprint is higher than the national average. Consider reducing.";
}
}, 100);
}
function animateValue(obj, start, end, duration) {
let startTimestamp = null;
const step = (timestamp) => {
if (!startTimestamp) startTimestamp = timestamp;
const progress = Math.min((timestamp - startTimestamp) / duration, 1);
// easing
const easeOutQuart = 1 - Math.pow(1 - progress, 4);
obj.innerHTML = (easeOutQuart * (end - start) + start).toFixed(2);
if (progress < 1) {
window.requestAnimationFrame(step);
} else {
obj.innerHTML = end.toFixed(2);
}
};
window.requestAnimationFrame(step);
}
});