meter: allow setting gradient as an object with numeric breakpoints

This commit is contained in:
jean-emmanuel 2018-11-20 09:37:43 +01:00
parent 74810c9c26
commit bae7ce44f9
3 changed files with 35 additions and 6 deletions

View file

@ -17,7 +17,13 @@ module.exports = class Meter extends Fader {
horizontal: {type: 'boolean', value: false, help: 'See fader\'s `horizontal`'},
pips: {type: 'boolean', value: false, help: 'See fader\'s `pips`'},
dashed: {type: 'boolean', value: false, help: 'See fader\'s `dashed`'},
gradient: {type: 'array', value: [], help: 'When set, the meter\'s gauge will be filled with a linear color gradient : each item must be a CSS color string. Example: `[\'blue\', \'red\']`'},
gradient: {type: 'array|object', value: [], help: [
'When set, the meter\'s gauge will be filled with a linear color gradient',
'- each item must be a CSS color string.',
'- as an `object`: each key must be a number between 0 and 1',
'- each item must be a CSS color string.',
'Examples: `[\'blue\', \'red\']`, {\'0\': \'blue\', \'0.9\': \'blue\', \'1\': \'red\'} '
]},
}, ['target', 'precision', 'bypass'], {

View file

@ -204,18 +204,40 @@ module.exports = class Fader extends Slider {
}
if (this.getProp('gradient')) {
var colors = this.getProp('gradient')
if (Array.isArray(colors) && colors.length > 1) {
if (
(Array.isArray(colors) && colors.length > 1) ||
(typeof colors === 'object' && Object.keys(colors).length > 1)
) {
var grad = this.ctx.createLinearGradient(0, this.getProp('horizontal') ? 0 : this.height, 0, this.getProp('horizontal') ? this.width : 0)
try {
var grad = this.ctx.createLinearGradient(0, this.getProp('horizontal') ? 0 : this.height, 0, this.getProp('horizontal') ? this.width : 0)
for (var i in colors) {
grad.addColorStop(i / (colors.length - 1), colors[i])
if (Array.isArray(colors)) {
for (var i in colors) {
grad.addColorStop(i / (colors.length - 1), colors[i])
}
this.colors.gradient = grad
} else {
for (var k in colors) {
if (!isNaN(k)) {
grad.addColorStop(clip(k, [0, 1]), colors[k])
}
}
this.colors.gradient = grad
}
this.colors.gradient = grad
} catch(err) {
this.errors.gradient = err
}
}
}