forked from dli/vortexspheres
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slider.js
54 lines (39 loc) · 1.29 KB
/
slider.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
'use strict'
var Slider = function (element, min, max, initialValue, changeCallback) {
var div = element;
var innerDiv = document.createElement('div');
innerDiv.style.position = 'absolute';
innerDiv.style.height = div.offsetHeight + 'px';
div.appendChild(innerDiv);
var color = 'rgba(255, 255, 255, 1.0)';
var value = initialValue;
this.getValue = function () {
return value;
};
var mousePressed = false;
var redraw = function () {
var fraction = (value - min) / (max - min);
innerDiv.style.background = color;
innerDiv.style.width = fraction * div.offsetWidth + 'px';
innerDiv.style.height = div.offsetHeight + 'px';
};
redraw();
div.addEventListener('mousedown', function (event) {
mousePressed = true;
onChange(event);
});
document.addEventListener('mouseup', function (event) {
mousePressed = false;
});
document.addEventListener('mousemove', function (event) {
if (mousePressed) {
onChange(event);
}
});
var onChange = function (event) {
var mouseX = getMousePosition(event, div).x;
value = clamp((mouseX / div.offsetWidth) * (max - min) + min, min, max);
changeCallback(value);
redraw();
};
};