-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
103 lines (85 loc) · 3.88 KB
/
main.js
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
document.getElementById('calculate-btn').addEventListener('click', function () {
const projectName = document.getElementById('project-name').value;
const bridgeType = document.getElementById('bridge-type').value;
const carbonFootprint = parseFloat(document.getElementById('carbon-footprint').value);
const functionalArea = parseFloat(document.getElementById('functional-area').value);
document.getElementById('project-title').textContent = projectName || 'Project name';
document.getElementById('bridge-type-display').textContent = bridgeType ? bridgeType : 'Bridge type';
if (!isNaN(carbonFootprint) && !isNaN(functionalArea) && functionalArea > 0) {
const carbonImpact = Math.round(carbonFootprint / functionalArea);
let carbonClass = '';
let shapeTopPosition = 0;
if (carbonImpact < 250) {
carbonClass = 'A++';
shapeTopPosition = 2.5;
} else if (carbonImpact >= 250 && carbonImpact < 500) {
carbonClass = 'A+';
shapeTopPosition = 2.5;
} else if (carbonImpact >= 500 && carbonImpact < 1000) {
carbonClass = 'A';
shapeTopPosition = 37.5;
} else if (carbonImpact >= 1000 && carbonImpact < 1500) {
carbonClass = 'B';
shapeTopPosition = 72.5;
} else if (carbonImpact >= 1500 && carbonImpact < 2000) {
carbonClass = 'C';
shapeTopPosition = 107.5;
} else if (carbonImpact >= 2000 && carbonImpact < 2500) {
carbonClass = 'D';
shapeTopPosition = 142.5;
} else if (carbonImpact >= 2500 && carbonImpact < 3000) {
carbonClass = 'E';
shapeTopPosition = 177.5;
} else if (carbonImpact >= 3000 && carbonImpact < 3500) {
carbonClass = 'F';
shapeTopPosition = 212.5;
} else {
carbonClass = 'G';
shapeTopPosition = 247.5;
}
const existingShape = document.getElementById('carbon-class-shape');
const existingResult = document.getElementById('calculation-result-text');
if (existingShape) {
existingShape.remove();
}
if (existingResult) {
existingResult.remove();
}
const newShape = document.createElement('div');
newShape.id = 'carbon-class-shape';
newShape.className = 'bar black-shape';
newShape.style.top = `${shapeTopPosition}px`;
newShape.textContent = carbonClass;
const resultText = document.createElement('div');
resultText.id = 'calculation-result-text';
resultText.className = 'calculation-result';
resultText.textContent = `${carbonImpact} kgCO₂e/m²`;
const blackShapeHeight = 30;
const gap = 5;
resultText.style.top = `${shapeTopPosition + blackShapeHeight + gap}px`;
const carbonRatingContainer = document.querySelector('.carbon-rating');
carbonRatingContainer.appendChild(newShape);
carbonRatingContainer.appendChild(resultText);
}
});
document.getElementById('download-btn').addEventListener('click', function () {
html2canvas(document.querySelector('.container')).then(canvas => {
const img = canvas.toDataURL("image/png");
const link = document.createElement('a');
link.href = img;
link.download = 'scorecard.png';
link.click();
});
});
document.getElementById('print-btn').addEventListener('click', function () {
html2canvas(document.querySelector('.container')).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const pdf = new jsPDF();
const imgWidth = 210;
const pageHeight = 297;
const imgHeight = canvas.height * imgWidth / canvas.width;
pdf.addImage(imgData, 'PNG', 0, 10, imgWidth, imgHeight);
pdf.save('scorecard.pdf');
});
});