sliders: add steps option (#412)

This commit is contained in:
jean-emmanuel 2018-11-03 09:24:24 +01:00
parent d3ba773726
commit 12b7620751
4 changed files with 39 additions and 5 deletions

View file

@ -4,6 +4,7 @@
- widgets
- multixy: added `.pad-N` class to inner xy pads to help with css customization
- sliders: new `steps` option
## 0.39.3

View file

@ -34,7 +34,13 @@ module.exports = class Fader extends Slider {
'- values can be `number` or `object` if a custom label is needed',
'Example: (`{min:{"-inf": 0}, "50%": 0.25, max: {"+inf": 1}}`)'
]},
logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale for the y axis'},
steps: {type: 'boolean|number|array', value: '', help: [
'Restricts the widget\'s value:',
'- `true`: use values defined in `range`',
'- `number`: define a number of evenly spaced steps',
'- `array`: use arbitrary values',
]},
logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale'},
unit: {type: 'string', value: '', help: 'Unit will be appended to the displayed widget\'s value (it doesn\'t affect osc messages)'},
origin: {type: 'number', value: 'auto', help: 'Defines the starting point\'s value of the fader\'s gauge'},
@ -221,7 +227,8 @@ module.exports = class Fader extends Slider {
var width = this.getProp('horizontal') ? this.height : this.width,
height = !this.getProp('horizontal') ? this.height : this.width
var d = Math.round(this.percentToCoord(this.percent)),
var percent = this.getProp('steps') ? this.valueToPercent(this.value) : this.percent,
d = Math.round(this.percentToCoord(percent)),
o = Math.round(this.percentToCoord(this.valueToPercent(this.originValue))),
m = Math.round(this.getProp('horizontal') ? this.height / 2 : this.width / 2)

View file

@ -25,7 +25,13 @@ module.exports = class Knob extends Slider {
'- values can be `number` or `object` if a custom label is needed',
'Example: (`{min:{"-inf": 0}, "50%": 0.25, max: {"+inf": 1}}`)'
]},
logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale for the y axis'},
steps: {type: 'boolean|array', value: false, help: [
'Restricts the widget\'s value:',
'- `true`: use values defined in `range`',
'- `number`: define a number of evenly spaced steps',
'- `array`: use arbitrary values',
]},
logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale'},
unit: {type: 'string', value: '', help: 'Unit will be appended to the displayed widget\'s value (it doesn\'t affect osc messages)'},
origin: {type: 'number', value: 'auto', help: 'Defines the starting point\'s value of the knob\'s gauge'},
@ -135,8 +141,9 @@ module.exports = class Knob extends Slider {
draw() {
var o = this.percentToAngle(this.valueToPercent(this.originValue)),
d = this.percentToAngle(this.percent),
var percent = this.getProp('steps') ? this.valueToPercent(this.value) : this.percent,
o = this.percentToAngle(this.valueToPercent(this.originValue)),
d = this.percentToAngle(percent),
min = this.percentToAngle(0),
max = this.percentToAngle(100),
dashed = this.getProp('dashed'),

View file

@ -100,6 +100,15 @@ class Slider extends Canvas {
}
if (this.getProp('steps')) {
var steps = this.getProp('steps')
this.steps = Array.isArray(steps) ?
steps : typeof steps === 'number' ?
Array(steps).fill(0).map((x, i) => i / (steps - 1) * this.rangeValsMax + this.rangeValsMin)
: this.rangeVals
}
this.setValue(this.springValue)
}
@ -210,6 +219,16 @@ class Slider extends Canvas {
this.value = value
if (this.getProp('steps')) {
var diff = this.steps.map(x => Math.abs(x - this.value)),
index = diff.indexOf(Math.min(...diff)),
val = this.steps[index]
if (!isNaN(val)) this.value = clip(val, [this.rangeValsMin,this.rangeValsMax])
}
if (!options.dragged) this.percent = this.valueToPercent(this.value)
if (!this.noDraw) this.batchDraw()