Skip to content

Commit e90679c

Browse files
committed
proximity slider
1 parent ee05d68 commit e90679c

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

proximity_slider.html

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<!-- vim: sw=2 ts=2 expandtab smartindent ft=javascript
2+
-->
3+
<!DOCTYPE html>
4+
<html lang="en">
5+
<head>
6+
<meta charset="utf-8" />
7+
<title>Proximity Slider Demo</title>
8+
<style>
9+
document, body { margin: 2rem; }
10+
11+
#sorted_region {
12+
display: flex;
13+
flex-direction: row;
14+
gap: 0.3rem;
15+
flex-wrap: wrap;
16+
width: 100%;
17+
}
18+
.box {
19+
width: 5rem;
20+
height: 5rem;
21+
border-radius: 1.5rem;
22+
background-color: navy;
23+
aspect-ratio: 1;
24+
color: white;
25+
font-family: monospace;
26+
27+
font-size: 1rem;
28+
29+
display: flex;
30+
justify-content: center;
31+
align-items: center;
32+
}
33+
34+
#proximity_slider {
35+
width: 50rem;
36+
height: 0.2rem;
37+
margin: 2rem;
38+
background-color: red;
39+
40+
position: relative;
41+
42+
#proximity_slider_handle {
43+
position: relative;
44+
height: 2rem;
45+
translate: 0rem -1rem;
46+
width: 2rem;
47+
background-color: black;
48+
49+
color: white;
50+
font-family: monospace;
51+
font-size: 1.8rem;
52+
padding: 0.2rem;
53+
54+
display: flex;
55+
justify-content: center;
56+
align-items: center;
57+
58+
&:hover {
59+
scale: 1.2;
60+
cursor: pointer;
61+
}
62+
}
63+
}
64+
65+
</style>
66+
</head>
67+
68+
<body>
69+
<div id="proximity_slider">
70+
<div id="proximity_slider_handle"> 0 </div>
71+
</div>
72+
73+
<div id="sorted_region"></div>
74+
<script>"use strict";
75+
76+
const BOX_COUNT = 50;
77+
const entries = Array.from({ length: BOX_COUNT }, (_, i) => BOX_COUNT * Math.random());
78+
79+
let mouse_down = false;
80+
proximity_slider_handle.onmousedown = () => mouse_down = true;
81+
window.onmouseup = () => mouse_down = false;
82+
window.onmousedown = ev => ev.preventDefault();
83+
window.onmousemove = ev => {
84+
ev.preventDefault();
85+
86+
if (mouse_down) {
87+
const bounds = proximity_slider.getBoundingClientRect();
88+
const x = Math.max(bounds.left, Math.min(bounds.right, ev.clientX));
89+
const t = (x - bounds.x) / bounds.width;
90+
const i = Math.round(t * BOX_COUNT);
91+
proximity_slider_handle.textContent = i;
92+
proximity_slider_handle.style.translate = `calc(${x - bounds.x}px - 50%) -1rem`;
93+
94+
render_sorted(i);
95+
}
96+
};
97+
98+
render_sorted(0);
99+
100+
function render_sorted(goal) {
101+
sorted_region.innerHTML = '';
102+
entries.sort((a, b) => Math.abs(a - goal) - Math.abs(b - goal));
103+
for (let i = 0; i < BOX_COUNT; i++) {
104+
const div = document.createElement('div');
105+
div.className = 'box'
106+
div.textContent = entries[i].toFixed(1);
107+
sorted_region.appendChild(div);
108+
}
109+
}
110+
</script>
111+
</body>
112+
</html>

0 commit comments

Comments
 (0)