-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.js
59 lines (41 loc) · 1.65 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
55
56
57
58
59
'use strict'
var Slider = (function () {
//changeCallback is called with the new value
var Slider = function (element, initial, min, max, changeCallback) {
this.value = initial;
this.min = min;
this.max = max;
this.div = element;
this.innerDiv = document.createElement('div');
this.innerDiv.style.position = 'absolute';
this.innerDiv.style.height = this.div.offsetHeight + 'px';
this.div.appendChild(this.innerDiv);
this.changeCallback = changeCallback;
this.mousePressed = false;
this.redraw();
this.div.addEventListener('mousedown', (function (event) {
this.mousePressed = true;
this.onChange(event);
}).bind(this));
document.addEventListener('mouseup', (function (event) {
this.mousePressed = false;
}).bind(this));
document.addEventListener('mousemove', (function (event) {
if (this.mousePressed) {
this.onChange(event);
}
}).bind(this));
};
Slider.prototype.redraw = function () {
var fraction = (this.value - this.min) / (this.max - this.min);
this.innerDiv.style.width = fraction * this.div.offsetWidth + 'px';
this.innerDiv.style.height = this.div.offsetHeight + 'px';
}
Slider.prototype.onChange = function (event) {
var mouseX = Utilities.getMousePosition(event, this.div).x;
this.value = Utilities.clamp((mouseX / this.div.offsetWidth) * (this.max - this.min) + this.min, this.min, this.max);
this.redraw();
this.changeCallback(this.value);
}
return Slider;
}());