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ewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Dynamic Sliders with Editable Values and Array Upscaler</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
10 |
+
background-color: #f4f4f4;
|
11 |
+
margin: 0;
|
12 |
+
padding: 20px;
|
13 |
+
display: flex;
|
14 |
+
justify-content: center;
|
15 |
+
align-items: center;
|
16 |
+
min-height: 100vh;
|
17 |
+
}
|
18 |
+
.container {
|
19 |
+
background-color: #fff;
|
20 |
+
padding: 20px;
|
21 |
+
border-radius: 8px;
|
22 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
23 |
+
width: 100%;
|
24 |
+
max-width: 800px;
|
25 |
+
}
|
26 |
+
.input-section {
|
27 |
+
margin-bottom: 20px;
|
28 |
+
display: grid;
|
29 |
+
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
30 |
+
gap: 10px;
|
31 |
+
justify-content: center;
|
32 |
+
}
|
33 |
+
|
34 |
+
.input-section label {
|
35 |
+
font-weight: bold;
|
36 |
+
margin-bottom: 5px;
|
37 |
+
display: block;
|
38 |
+
}
|
39 |
+
|
40 |
+
.input-section input[type="number"] {
|
41 |
+
padding: 8px;
|
42 |
+
border: 1px solid #ccc;
|
43 |
+
border-radius: 4px;
|
44 |
+
width: 100%;
|
45 |
+
box-sizing: border-box;
|
46 |
+
}
|
47 |
+
|
48 |
+
/* Add media query to adjust grid template columns on smaller screens */
|
49 |
+
@media (max-width: 768px) {
|
50 |
+
.input-section {
|
51 |
+
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
|
52 |
+
}
|
53 |
+
}
|
54 |
+
|
55 |
+
@media (max-width: 480px) {
|
56 |
+
.input-section {
|
57 |
+
grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
|
58 |
+
}
|
59 |
+
}
|
60 |
+
}
|
61 |
+
#sliders {
|
62 |
+
display: flex;
|
63 |
+
flex-direction: column;
|
64 |
+
}
|
65 |
+
.slider-container {
|
66 |
+
display: flex;
|
67 |
+
align-items: center;
|
68 |
+
width: 100%;
|
69 |
+
}
|
70 |
+
.slider-index {
|
71 |
+
width: 30px;
|
72 |
+
text-align: right;
|
73 |
+
font-weight: bold;
|
74 |
+
color: #333;
|
75 |
+
flex-shrink: 0;
|
76 |
+
}
|
77 |
+
.slider-wrapper {
|
78 |
+
flex-grow: 1;
|
79 |
+
display: flex;
|
80 |
+
align-items: center;
|
81 |
+
gap: 10px;
|
82 |
+
}
|
83 |
+
.slider-wrapper input[type="range"] {
|
84 |
+
width: 100%;
|
85 |
+
box-sizing: border-box;
|
86 |
+
}
|
87 |
+
.slider-number {
|
88 |
+
width: 70px;
|
89 |
+
text-align: left;
|
90 |
+
font-weight: bold;
|
91 |
+
color: #333;
|
92 |
+
flex-shrink: 0;
|
93 |
+
cursor: pointer;
|
94 |
+
}
|
95 |
+
.slider-number input {
|
96 |
+
width: 100%;
|
97 |
+
box-sizing: border-box;
|
98 |
+
}
|
99 |
+
.middle-slider .slider-index,
|
100 |
+
.middle-slider .slider-wrapper input[type="range"] {
|
101 |
+
background-color: #ffcccb;
|
102 |
+
}
|
103 |
+
.output {
|
104 |
+
margin-top: 20px;
|
105 |
+
font-weight: bold;
|
106 |
+
color: #333;
|
107 |
+
}
|
108 |
+
.output span {
|
109 |
+
color: #007bff;
|
110 |
+
}
|
111 |
+
.error-message {
|
112 |
+
color: red;
|
113 |
+
font-weight: bold;
|
114 |
+
margin-top: 10px;
|
115 |
+
}
|
116 |
+
.close-button {
|
117 |
+
cursor: pointer;
|
118 |
+
color: red;
|
119 |
+
font-weight: bold;
|
120 |
+
margin-left: 5px;
|
121 |
+
}
|
122 |
+
.array-upscaler {
|
123 |
+
margin-top: 40px;
|
124 |
+
}
|
125 |
+
.array-upscaler h2 {
|
126 |
+
margin-bottom: 10px;
|
127 |
+
}
|
128 |
+
.array-upscaler p {
|
129 |
+
margin: 10px 0;
|
130 |
+
}
|
131 |
+
.array-upscaler button {
|
132 |
+
padding: 10px 20px;
|
133 |
+
background-color: #007bff;
|
134 |
+
color: white;
|
135 |
+
border: none;
|
136 |
+
border-radius: 4px;
|
137 |
+
cursor: pointer;
|
138 |
+
}
|
139 |
+
.array-upscaler button:hover {
|
140 |
+
background-color: #0056b3;
|
141 |
+
}
|
142 |
+
.array-upscaler #result {
|
143 |
+
margin-top: 20px;
|
144 |
+
font-weight: bold;
|
145 |
+
color: #333;
|
146 |
+
}
|
147 |
+
</style>
|
148 |
+
</head>
|
149 |
+
<body>
|
150 |
+
<div class="container">
|
151 |
+
<div class="input-section">
|
152 |
+
<div>
|
153 |
+
<label for="numSliders">Number of Sliders:</label>
|
154 |
+
<input type="number" id="numSliders" min="1" value="1" oninput="updateSliders()">
|
155 |
+
</div>
|
156 |
+
<div>
|
157 |
+
<label for="minValue">Minimum Value:</label>
|
158 |
+
<input type="number" id="minValue" step="0.01" value="0.00" oninput="updateSliders()">
|
159 |
+
</div>
|
160 |
+
<div>
|
161 |
+
<label for="maxValue">Maximum Value:</label>
|
162 |
+
<input type="number" id="maxValue" step="0.01" value="100.00" oninput="updateSliders()">
|
163 |
+
</div>
|
164 |
+
<div>
|
165 |
+
<label for="sliderWidth">Slider Width (%):</label>
|
166 |
+
<input type="number" id="sliderWidth" min="10" max="100" value="100" oninput="updateSliderStyles()">
|
167 |
+
</div>
|
168 |
+
<div>
|
169 |
+
<label for="sliderMargin">Slider Margin (px):</label>
|
170 |
+
<input type="number" id="sliderMargin" min="0" value="10" oninput="updateSliderStyles()">
|
171 |
+
</div>
|
172 |
+
<div>
|
173 |
+
<label for="sliderStep">Slider Step:</label>
|
174 |
+
<input type="number" id="sliderStep" min="0.01" step="0.01" value="0.01" oninput="updateSliderStep()">
|
175 |
+
</div>
|
176 |
+
</div>
|
177 |
+
<div id="sliders"></div>
|
178 |
+
<div class="output">
|
179 |
+
<label>Slider Values:</label>
|
180 |
+
<span id="sliderValues">[]</span>
|
181 |
+
</div>
|
182 |
+
<div id="errorMessage" class="error-message"></div>
|
183 |
+
|
184 |
+
<!-- Array Upscaler Section -->
|
185 |
+
<div class="array-upscaler">
|
186 |
+
<h2>Array Upscaler</h2>
|
187 |
+
<p>
|
188 |
+
Upscaled Length:
|
189 |
+
<input type="number" id="newLength" min="1" value="5">
|
190 |
+
</p>
|
191 |
+
<button onclick="upscaleArray()">Upscale Array</button>
|
192 |
+
<h3>Upscaled Array:</h3>
|
193 |
+
<div id="result"></div>
|
194 |
+
</div>
|
195 |
+
</div>
|
196 |
+
|
197 |
+
<script>
|
198 |
+
function updateSliderStyles() {
|
199 |
+
const sliderWidth = document.getElementById('sliderWidth').value;
|
200 |
+
const sliderMargin = document.getElementById('sliderMargin').value;
|
201 |
+
const sliders = document.querySelectorAll('.slider-wrapper input[type="range"]');
|
202 |
+
|
203 |
+
sliders.forEach(slider => {
|
204 |
+
slider.style.width = `${sliderWidth}%`;
|
205 |
+
});
|
206 |
+
|
207 |
+
const sliderContainers = document.querySelectorAll('.slider-container');
|
208 |
+
|
209 |
+
sliderContainers.forEach(sliderContainer => {
|
210 |
+
sliderContainer.style.margin = `${sliderMargin}px 0`;
|
211 |
+
});
|
212 |
+
}
|
213 |
+
|
214 |
+
function updateSliderStep() {
|
215 |
+
const step = document.getElementById('sliderStep').value;
|
216 |
+
const sliders = document.querySelectorAll('#sliders input[type="range"]');
|
217 |
+
|
218 |
+
sliders.forEach(slider => {
|
219 |
+
slider.step = step;
|
220 |
+
});
|
221 |
+
}
|
222 |
+
|
223 |
+
function createSliders() {
|
224 |
+
const numSliders = parseInt(document.getElementById('numSliders').value);
|
225 |
+
const minValue = parseFloat(document.getElementById('minValue').value);
|
226 |
+
const maxValue = parseFloat(document.getElementById('maxValue').value);
|
227 |
+
const sliderStep = parseFloat(document.getElementById('sliderStep').value);
|
228 |
+
const slidersContainer = document.getElementById('sliders');
|
229 |
+
slidersContainer.innerHTML = ''; // Clear previous sliders
|
230 |
+
|
231 |
+
for (let i = 0; i < numSliders; i++) {
|
232 |
+
const sliderContainer = document.createElement('div');
|
233 |
+
sliderContainer.className = 'slider-container';
|
234 |
+
|
235 |
+
const sliderIndex = document.createElement('span');
|
236 |
+
sliderIndex.className = 'slider-index';
|
237 |
+
sliderIndex.textContent = i + 1;
|
238 |
+
|
239 |
+
const sliderWrapper = document.createElement('div');
|
240 |
+
sliderWrapper.className = 'slider-wrapper';
|
241 |
+
|
242 |
+
const slider = document.createElement('input');
|
243 |
+
slider.type = 'range';
|
244 |
+
slider.min = minValue;
|
245 |
+
slider.max = maxValue;
|
246 |
+
slider.value = minValue;
|
247 |
+
slider.step = sliderStep;
|
248 |
+
slider.oninput = updateValues;
|
249 |
+
|
250 |
+
const sliderNumber = document.createElement('span');
|
251 |
+
sliderNumber.className = 'slider-number';
|
252 |
+
sliderNumber.textContent = minValue.toFixed(2);
|
253 |
+
sliderNumber.onclick = function() {
|
254 |
+
makeEditable(sliderNumber, slider);
|
255 |
+
};
|
256 |
+
|
257 |
+
sliderWrapper.appendChild(slider);
|
258 |
+
sliderWrapper.appendChild(sliderNumber);
|
259 |
+
|
260 |
+
sliderContainer.appendChild(sliderIndex);
|
261 |
+
sliderContainer.appendChild(sliderWrapper);
|
262 |
+
slidersContainer.appendChild(sliderContainer);
|
263 |
+
|
264 |
+
// Highlight the middle slider if the total number of sliders is odd
|
265 |
+
if (numSliders % 2 !== 0 && i === Math.floor(numSliders / 2)) {
|
266 |
+
sliderContainer.classList.add('middle-slider');
|
267 |
+
}
|
268 |
+
}
|
269 |
+
updateValues(); // Initial update
|
270 |
+
updateSliderStyles(); // Apply initial styles
|
271 |
+
}
|
272 |
+
|
273 |
+
function updateSliders() {
|
274 |
+
const numSlidersInput = document.getElementById('numSliders').value;
|
275 |
+
const minValue = parseFloat(document.getElementById('minValue').value);
|
276 |
+
const maxValue = parseFloat(document.getElementById('maxValue').value);
|
277 |
+
const sliderStep = parseFloat(document.getElementById('sliderStep').value);
|
278 |
+
const slidersContainer = document.getElementById('sliders');
|
279 |
+
const errorMessage = document.getElementById('errorMessage');
|
280 |
+
|
281 |
+
// Validate the number of sliders input
|
282 |
+
if (!numSlidersInput || isNaN(numSlidersInput) || parseInt(numSlidersInput) < 1) {
|
283 |
+
errorMessage.textContent = 'Please enter a valid number of sliders (greater than 0).';
|
284 |
+
slidersContainer.innerHTML = ''; // Clear sliders if input is invalid
|
285 |
+
return;
|
286 |
+
} else {
|
287 |
+
errorMessage.textContent = ''; // Clear error message if input is valid
|
288 |
+
}
|
289 |
+
|
290 |
+
const numSliders = parseInt(numSlidersInput);
|
291 |
+
const currentSliders = document.querySelectorAll('#sliders input[type="range"]');
|
292 |
+
|
293 |
+
// Preserve existing slider values when reducing number of sliders
|
294 |
+
const existingValues = Array.from(currentSliders).map(slider => parseFloat(slider.value));
|
295 |
+
|
296 |
+
slidersContainer.innerHTML = ''; // Clear existing sliders
|
297 |
+
|
298 |
+
for (let i = 0; i < numSliders; i++) {
|
299 |
+
const sliderContainer = document.createElement('div');
|
300 |
+
sliderContainer.className = 'slider-container';
|
301 |
+
|
302 |
+
const sliderIndex = document.createElement('span');
|
303 |
+
sliderIndex.className = 'slider-index';
|
304 |
+
sliderIndex.textContent = i + 1;
|
305 |
+
|
306 |
+
const sliderWrapper = document.createElement('div');
|
307 |
+
sliderWrapper.className = 'slider-wrapper';
|
308 |
+
|
309 |
+
const slider = document.createElement('input');
|
310 |
+
slider.type = 'range';
|
311 |
+
slider.min = minValue;
|
312 |
+
slider.max = maxValue;
|
313 |
+
slider.step = sliderStep;
|
314 |
+
slider.oninput = updateValues;
|
315 |
+
|
316 |
+
const sliderNumber = document.createElement('span');
|
317 |
+
sliderNumber.className = 'slider-number';
|
318 |
+
sliderNumber.onclick = function() {
|
319 |
+
makeEditable(sliderNumber, slider);
|
320 |
+
};
|
321 |
+
|
322 |
+
// Use preserved value or default to minValue
|
323 |
+
const preservedValue = existingValues[i] !== undefined
|
324 |
+
? Math.max(minValue, Math.min(maxValue, existingValues[i]))
|
325 |
+
: minValue;
|
326 |
+
|
327 |
+
slider.value = preservedValue;
|
328 |
+
sliderNumber.textContent = preservedValue.toFixed(2);
|
329 |
+
|
330 |
+
sliderWrapper.appendChild(slider);
|
331 |
+
sliderWrapper.appendChild(sliderNumber);
|
332 |
+
|
333 |
+
sliderContainer.appendChild(sliderIndex);
|
334 |
+
sliderContainer.appendChild(sliderWrapper);
|
335 |
+
slidersContainer.appendChild(sliderContainer);
|
336 |
+
|
337 |
+
// Highlight the middle slider if the total number of sliders is odd
|
338 |
+
if (numSliders % 2 !== 0 && i === Math.floor(numSliders / 2)) {
|
339 |
+
sliderContainer.classList.add('middle-slider');
|
340 |
+
}
|
341 |
+
}
|
342 |
+
|
343 |
+
updateValues(); // Update the displayed values
|
344 |
+
updateSliderStyles(); // Apply styles
|
345 |
+
}
|
346 |
+
|
347 |
+
function updateValues() {
|
348 |
+
const sliders = document.querySelectorAll('#sliders input[type="range"]');
|
349 |
+
const values = Array.from(sliders).map(slider => {
|
350 |
+
const value = parseFloat(slider.value);
|
351 |
+
slider.nextElementSibling.textContent = value.toFixed(2); // Update the number next to the slider
|
352 |
+
return value;
|
353 |
+
});
|
354 |
+
document.getElementById('sliderValues').textContent = JSON.stringify(values);
|
355 |
+
}
|
356 |
+
|
357 |
+
function makeEditable(sliderNumber, slider) {
|
358 |
+
const currentValue = sliderNumber.textContent;
|
359 |
+
const container = document.createElement('div'); // Create a container to hold input and button
|
360 |
+
const input = document.createElement('input');
|
361 |
+
input.type = 'number';
|
362 |
+
input.value = currentValue;
|
363 |
+
input.min = slider.min;
|
364 |
+
input.max = slider.max;
|
365 |
+
input.step = slider.step;
|
366 |
+
input.style.width = '100%';
|
367 |
+
input.style.boxSizing = 'border-box';
|
368 |
+
|
369 |
+
const closeButton = document.createElement('span');
|
370 |
+
closeButton.className = 'close-button';
|
371 |
+
closeButton.textContent = 'X';
|
372 |
+
closeButton.onclick = function() {
|
373 |
+
input.blur(); // Trigger blur event to revert to text
|
374 |
+
};
|
375 |
+
|
376 |
+
container.appendChild(input); // Append input and button to container
|
377 |
+
container.appendChild(closeButton);
|
378 |
+
|
379 |
+
const originalSliderNumber = sliderNumber.cloneNode(true); // Clone original sliderNumber for later use
|
380 |
+
originalSliderNumber.onclick = function() { makeEditable(originalSliderNumber, slider); }; // Reattach onclick event
|
381 |
+
|
382 |
+
sliderNumber.replaceWith(container); // Replace sliderNumber with the container
|
383 |
+
|
384 |
+
input.onblur = function() {
|
385 |
+
const newValue = parseFloat(input.value);
|
386 |
+
if (!isNaN(newValue) && newValue >= parseFloat(slider.min) && newValue <= parseFloat(slider.max)) {
|
387 |
+
slider.value = newValue;
|
388 |
+
originalSliderNumber.textContent = newValue.toFixed(2); // Update sliderNumber text
|
389 |
+
} else {
|
390 |
+
originalSliderNumber.textContent = currentValue; // Revert to original value if invalid
|
391 |
+
}
|
392 |
+
container.replaceWith(originalSliderNumber); // Replace container with original sliderNumber
|
393 |
+
updateValues(); // Update displayed values
|
394 |
+
};
|
395 |
+
|
396 |
+
input.onkeydown = function(event) {
|
397 |
+
if (event.key === 'Enter') {
|
398 |
+
input.blur(); // Trigger blur event on Enter key press
|
399 |
+
}
|
400 |
+
};
|
401 |
+
|
402 |
+
input.focus(); // Focus on the input field
|
403 |
+
}
|
404 |
+
|
405 |
+
// Initial call to create sliders
|
406 |
+
createSliders();
|
407 |
+
|
408 |
+
// Function to upscale the array
|
409 |
+
function upscaleArray() {
|
410 |
+
// Get slider values
|
411 |
+
const sliders = document.querySelectorAll('#sliders input[type="range"]');
|
412 |
+
const originalArray = Array.from(sliders).map(slider => parseFloat(slider.value));
|
413 |
+
const newLengthInput = document.getElementById('newLength').value;
|
414 |
+
const newLength = parseInt(newLengthInput, 10);
|
415 |
+
|
416 |
+
// Validate inputs
|
417 |
+
if (originalArray.length === 0 || isNaN(newLength) || newLength < 1) {
|
418 |
+
document.getElementById('result').innerHTML = 'Invalid input.';
|
419 |
+
return;
|
420 |
+
}
|
421 |
+
|
422 |
+
// If new length is less than or equal to original, return original array
|
423 |
+
if (newLength <= originalArray.length) {
|
424 |
+
document.getElementById('result').innerHTML = originalArray.join(', ');
|
425 |
+
return;
|
426 |
+
}
|
427 |
+
|
428 |
+
// Calculate the step size
|
429 |
+
const step = (originalArray.length - 1) / (newLength - 1);
|
430 |
+
|
431 |
+
// Create the upscaled array
|
432 |
+
const upscaledArray = [];
|
433 |
+
for (let j = 0; j < newLength; j++) {
|
434 |
+
const i = j * step;
|
435 |
+
const lower = Math.floor(i);
|
436 |
+
const upper = Math.ceil(i);
|
437 |
+
const fraction = i - lower;
|
438 |
+
|
439 |
+
// Handle edge cases where upper is out of bounds
|
440 |
+
if (upper >= originalArray.length) {
|
441 |
+
upper = originalArray.length - 1;
|
442 |
+
fraction = 0;
|
443 |
+
}
|
444 |
+
|
445 |
+
// Linear interpolation
|
446 |
+
const value = originalArray[lower] + fraction * (originalArray[upper] - originalArray[lower]);
|
447 |
+
upscaledArray.push(value.toFixed(3)); // Display one decimal place
|
448 |
+
}
|
449 |
+
|
450 |
+
// Display the result
|
451 |
+
document.getElementById('result').innerHTML = upscaledArray.join(', ');
|
452 |
+
}
|
453 |
+
</script>
|
454 |
+
</body>
|
455 |
+
</html>
|