-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
207 lines (182 loc) · 5.95 KB
/
script.js
File metadata and controls
207 lines (182 loc) · 5.95 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
199
200
201
202
203
204
205
206
207
// Get DOM elements
const inputValue = document.getElementById('inputValue');
const fromUnit = document.getElementById('fromUnit');
const toUnit = document.getElementById('toUnit');
const resultValue = document.getElementById('resultValue');
const rootFontSize = document.getElementById('rootFontSize');
const viewportWidth = document.getElementById('viewportWidth');
const viewportHeight = document.getElementById('viewportHeight');
const parentFontSize = document.getElementById('parentFontSize');
const conversionTable = document.getElementById('conversionTable');
// Constants
const PT_TO_PX_RATIO = 4 / 3; // 1pt = 4/3px
/**
* Convert any unit to pixels first (base unit)
*/
function toPixels(value, unit, settings) {
const val = parseFloat(value);
if (isNaN(val)) return 0;
switch (unit) {
case 'px':
return val;
case 'rem':
return val * settings.rootFontSize;
case 'em':
return val * settings.parentFontSize;
case 'vw':
return (val * settings.viewportWidth) / 100;
case 'vh':
return (val * settings.viewportHeight) / 100;
case 'percent':
// For percentage, we assume it's relative to parent font size
return (val * settings.parentFontSize) / 100;
case 'pt':
return val * PT_TO_PX_RATIO;
default:
return val;
}
}
/**
* Convert from pixels to target unit
*/
function fromPixels(pixels, unit, settings) {
const px = parseFloat(pixels);
if (isNaN(px)) return 0;
switch (unit) {
case 'px':
return px;
case 'rem':
return px / settings.rootFontSize;
case 'em':
return px / settings.parentFontSize;
case 'vw':
return (px * 100) / settings.viewportWidth;
case 'vh':
return (px * 100) / settings.viewportHeight;
case 'percent':
// For percentage, we assume it's relative to parent font size
return (px * 100) / settings.parentFontSize;
case 'pt':
return px / PT_TO_PX_RATIO;
default:
return px;
}
}
/**
* Convert between any two units
*/
function convertUnits(value, from, to, settings) {
const pixels = toPixels(value, from, settings);
return fromPixels(pixels, to, settings);
}
/**
* Format number for display
*/
function formatNumber(num) {
if (isNaN(num)) return '0';
// Round to 4 decimal places and remove trailing zeros
const rounded = Math.round(num * 10000) / 10000;
return rounded.toString().replace(/\.?0+$/, '');
}
/**
* Get current settings from inputs
*/
function getSettings() {
return {
rootFontSize: parseFloat(rootFontSize.value) || 16,
viewportWidth: parseFloat(viewportWidth.value) || 1920,
viewportHeight: parseFloat(viewportHeight.value) || 1080,
parentFontSize: parseFloat(parentFontSize.value) || 16
};
}
/**
* Get unit display name
*/
function getUnitDisplayName(unit) {
const unitNames = {
'px': 'px',
'rem': 'rem',
'em': 'em',
'vw': 'vw',
'vh': 'vh',
'percent': '%',
'pt': 'pt'
};
return unitNames[unit] || unit;
}
/**
* Update the main conversion result
*/
function updateResult() {
const value = parseFloat(inputValue.value);
if (isNaN(value)) {
resultValue.textContent = '0 ' + getUnitDisplayName(toUnit.value);
return;
}
const settings = getSettings();
const result = convertUnits(value, fromUnit.value, toUnit.value, settings);
const formattedResult = formatNumber(result);
resultValue.textContent = formattedResult + ' ' + getUnitDisplayName(toUnit.value);
}
/**
* Update the conversion table showing all unit conversions
*/
function updateConversionTable() {
const value = parseFloat(inputValue.value);
if (isNaN(value)) {
conversionTable.innerHTML = '<div style="text-align: center; color: #999;">Enter a value to see conversions</div>';
return;
}
const settings = getSettings();
const from = fromUnit.value;
const units = ['px', 'rem', 'em', 'vw', 'vh', 'percent', 'pt'];
let tableHTML = '';
units.forEach(unit => {
if (unit === from) return; // Skip the source unit
const result = convertUnits(value, from, unit, settings);
const formattedResult = formatNumber(result);
const displayUnit = getUnitDisplayName(unit);
tableHTML += `
<div class="conversion-row">
<span class="unit-name">${displayUnit}</span>
<span class="unit-value">${formattedResult} ${displayUnit}</span>
</div>
`;
});
conversionTable.innerHTML = tableHTML;
}
/**
* Update all conversions
*/
function updateAll() {
updateResult();
updateConversionTable();
}
// Event listeners for instant feedback
inputValue.addEventListener('input', updateAll);
fromUnit.addEventListener('change', updateAll);
toUnit.addEventListener('change', updateAll);
rootFontSize.addEventListener('input', updateAll);
viewportWidth.addEventListener('input', updateAll);
viewportHeight.addEventListener('input', updateAll);
parentFontSize.addEventListener('input', updateAll);
// Initialize on page load
document.addEventListener('DOMContentLoaded', () => {
// Set viewport dimensions to actual viewport if possible
if (window.innerWidth && window.innerHeight) {
viewportWidth.value = window.innerWidth;
viewportHeight.value = window.innerHeight;
}
// Detect root font size
const computedRootFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize);
if (computedRootFontSize) {
rootFontSize.value = computedRootFontSize;
}
updateAll();
});
// Update viewport dimensions on window resize
window.addEventListener('resize', () => {
viewportWidth.value = window.innerWidth;
viewportHeight.value = window.innerHeight;
updateAll();
});