-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAspectRatioCalculator.html
More file actions
198 lines (171 loc) · 6.94 KB
/
AspectRatioCalculator.html
File metadata and controls
198 lines (171 loc) · 6.94 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Aspect Ratio Calculator</title>
<!-- Bootstrap 5 -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet" />
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
background: linear-gradient(135deg, #1e3c72, #2a5298);
/* Dark blue gradient */
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.container {
background-color: #282828;
padding: 40px;
border-radius: 12px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
width: 100%;
max-width: 600px;
}
h2 {
text-align: center;
font-size: 36px;
font-weight: 600;
margin-bottom: 30px;
color: #fff;
}
h2 span {
color: #00c6ff;
/* Soft cyan color */
}
.form-control {
font-size: 18px;
padding: 15px;
border-radius: 8px;
border: 2px solid #444;
background-color: #333;
color: #fff;
transition: border 0.3s, box-shadow 0.3s;
}
.form-control:focus {
border-color: #00c6ff;
box-shadow: 0 0 5px rgba(0, 198, 255, 0.5);
}
.btn-custom {
background-color: #00c6ff;
color: white;
padding: 12px;
font-size: 18px;
width: 100%;
border-radius: 8px;
transition: background-color 0.3s;
border: none;
}
.btn-custom:hover {
background-color: #009cb8;
}
.btn-reset {
background-color: #444;
color: white;
margin-top: 10px;
border-radius: 8px;
border: none;
}
.btn-reset:hover {
background-color: #666;
}
.tooltip-inner {
background-color: #00c6ff;
color: white;
}
.row label {
color: #fff;
font-weight: 600;
}
</style>
</head>
<body>
<div class="container">
<h2><span>Aspect Ratio</span> Calculator</h2>
<!-- Ratio Inputs -->
<div class="mb-4 row">
<label for="ratio-width" class="col-sm-3 col-form-label">Aspect Ratio:</label>
<div class="col-sm-9 d-flex gap-3">
<input type="number" id="ratio-width" value="16" class="form-control" placeholder="Width" />
<input type="number" id="ratio-height" value="4" class="form-control" placeholder="Height" />
</div>
</div>
<!-- Dimensions Inputs -->
<div class="mb-4 row">
<div class="col-md-6">
<label for="width" class="form-label" data-bs-toggle="tooltip" data-bs-placement="top"
title="Enter the width of your image/video">Width:</label>
<input type="number" id="width" value="1280" class="form-control" placeholder="Enter width" />
</div>
<div class="col-md-6">
<label for="height" class="form-label" data-bs-toggle="tooltip" data-bs-placement="top"
title="Enter the height of your image/video">Height:</label>
<input type="number" id="height" value="720" class="form-control" placeholder="Enter height" />
</div>
</div>
<!-- Calculate & Reset Buttons -->
<div class="d-grid gap-2">
<button class="btn btn-custom" onclick="calculateAspectRatio()">Calculate</button>
<button class="btn btn-reset" onclick="resetValues()">Reset</button>
</div>
</div>
<!-- Bootstrap JS and Popper.js (For tooltips) -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.min.js"></script>
<script>
let ratioWidth = document.getElementById("ratio-width");
let ratioHeight = document.getElementById("ratio-height");
let width = document.getElementById("width");
let height = document.getElementById("height");
// Tooltips Initialization
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
});
// Function to calculate width based on height and aspect ratio
const calculateWidth = () => {
let aspectRatio = ratioWidth.value / ratioHeight.value;
// Correct formula: width = (height * ratioWidth) / ratioHeight
width.value = parseFloat(((height.value * ratioWidth.value) / ratioHeight.value).toFixed(2));
};
// Function to calculate height based on width and aspect ratio
const calculateHeight = () => {
let aspectRatio = ratioWidth.value / ratioHeight.value;
// Correct formula: height = (width * ratioHeight) / ratioWidth
height.value = parseFloat(((width.value * ratioHeight.value) / ratioWidth.value).toFixed(2));
};
// Function to recalculate both width and height based on aspect ratio
const recalculateDimensions = () => {
// If either ratio width or ratio height changes, recalculate both width and height
if (ratioWidth.value && ratioHeight.value) {
// Recalculate width and height based on aspect ratio
calculateWidth();
calculateHeight();
}
};
// Event listeners to ensure that changes to width, height, or aspect ratio trigger recalculation
ratioWidth.addEventListener("input", recalculateDimensions);
ratioHeight.addEventListener("input", recalculateDimensions);
width.addEventListener("input", recalculateDimensions);
height.addEventListener("input", recalculateDimensions);
function calculateAspectRatio() {
recalculateDimensions();
}
function resetValues() {
if (confirm("Are you sure you want to reset all values?")) {
ratioWidth.value = 16; // Set default aspect ratio width to 16
ratioHeight.value = 4; // Set default aspect ratio height to 4
width.value = 1280; // Set default width to 1280
height.value = 720; // Set default height to 720
}
}
</script>
</body>
</html>