-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
slider.js
145 lines (130 loc) · 4 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
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
import BaseEvent from "@pencil.js/base-event";
import Circle from "@pencil.js/circle";
import Rectangle from "@pencil.js/rectangle";
import Input from "@pencil.js/input";
import MouseEvent from "@pencil.js/mouse-event";
import Position from "@pencil.js/position";
import Vector from "@pencil.js/vector";
import { constrain, map } from "@pencil.js/math";
import "@pencil.js/draggable";
/**
* @module Slider
*/
const constrainerKey = Symbol("_constrainer");
const moveHandleKey = Symbol("_moveHandle");
/**
* Slider class
* <br><img src="./media/examples/slider.png" alt="slider demo"/>
* @class
* @extends {module:Input}
*/
export default class Slider extends Input {
/**
* Slider constructor
* @param {PositionDefinition} positionDefinition - Top-left corner
* @param {SliderOptions} [options] - Specific options
*/
constructor (positionDefinition, options) {
super(positionDefinition, Rectangle, options);
const radius = (this.height - 2) / 2;
this.handle = new Circle([0, this.height / 2], radius, {
fill: this.options.foreground,
cursor: Rectangle.cursors.ewResize,
origin: this.getOrigin(),
});
this.add(this.handle);
this[constrainerKey] = new Vector([radius + 1, this.height / 2], [this.width - radius - 1, this.height / 2]);
this.handle.draggable({
constrain: this[constrainerKey],
});
this.handle.on(MouseEvent.events.drag, () => this.fire(new BaseEvent(Slider.events.change, this)), true);
}
/**
* @inheritDoc
*/
click (position) {
const origin = this.getOrigin();
this.handle.position.set(position.x - origin.x, 0)
.constrain(this[constrainerKey].start, this[constrainerKey].end);
super.click();
}
/**
* Change this slider's size
* @param {Number} newWidth - A new size in pixels
*/
set width (newWidth) {
if (newWidth < Slider.HEIGHT) {
throw new RangeError(`Slider is too small, minimum is ${Slider.HEIGHT}px.`);
}
this.options.width = newWidth;
this[constrainerKey].end = new Position(this.width - Slider.HEIGHT, 0);
this[moveHandleKey](this.value);
}
/**
* Return this slider's width
* @return {Number}
*/
get width () {
return this.options.width;
}
/**
* Return this slider's height
* @return {Number}
*/
get height () { // eslint-disable-line class-methods-use-this
return Slider.HEIGHT;
}
/**
* Returns this current value
* @return {Number}
*/
get value () {
const { min, max } = this.options;
const { start, end } = this[constrainerKey];
return map(this.handle.position.x, start.x, end.x, min, max);
}
/**
* Change this current value
* @param {Number} newValue - A new value to set
*/
set value (newValue) {
this[moveHandleKey](constrain(newValue, this.options.min, this.options.max));
}
/**
* @typedef {Object} SliderOptions
* @extends InputOptions
* @prop {Number} [min=0] - Minimum value when the slider is at lowest
* @prop {Number} [max=10] - Maximum value when the slider is at highest
* @prop {Number} [value=0] - Initial value
* @prop {Number} [width=200] - Size of the slider
*/
/**
* @type {SliderOptions}
*/
static get defaultOptions () {
return {
...super.defaultOptions,
min: 0,
max: 1,
value: 0,
width: 200,
};
}
/**
* Height of sliders
* @type {Number}
*/
static get HEIGHT () {
return 20;
}
}
/**
* Move the handle to a specified value
* @param {Number} value - New value to use
* @memberOf Slider#
*/
Slider.prototype[moveHandleKey] = function moveHandle (value) {
const { min, max } = this.options;
const { start, end } = this[constrainerKey];
this.handle.position.x = map(value, min, max, start.x, end.x);
};