From 09aedf526bd412d85f135bcb2d56a7503e576db8 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Tue, 6 Mar 2018 22:32:16 +0100 Subject: [PATCH] meter: add gradient property (#201) --- CHANGELOG.md | 1 + resources/docs/widgets/plots.md | 5 +++++ src/browser/js/app/widgets/plots/meter.js | 1 + src/browser/js/app/widgets/sliders/fader.js | 18 +++++++++++++++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13ea70f7..5fb12426 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - editor: wrap/cut/delete multiple widget not working properly - widgets - text: set line-height relative to font size + - meter: added `gradient` feature ## 0.26.1 diff --git a/resources/docs/widgets/plots.md b/resources/docs/widgets/plots.md index b0465ce3..6253cc4f 100644 --- a/resources/docs/widgets/plots.md +++ b/resources/docs/widgets/plots.md @@ -229,6 +229,11 @@ Meter displays incoming numerical values (via osc) as a gauge level ### `logScale` - see fader's [`logScale`](sliders/#fader) +### `gradient` +- type: `array` +- default: `[]` +- usage: when set, the meter's gauge will be filled with a linear color gradient : each item must be a color string. Example: `["blue", "red"]` + ## Image diff --git a/src/browser/js/app/widgets/plots/meter.js b/src/browser/js/app/widgets/plots/meter.js index b3eaa1fc..dfb92063 100644 --- a/src/browser/js/app/widgets/plots/meter.js +++ b/src/browser/js/app/widgets/plots/meter.js @@ -34,6 +34,7 @@ module.exports = class Meter extends Fader { horizontal:false, pips:false, dashed:false, + gradient:[], _value: 'value', default: '', diff --git a/src/browser/js/app/widgets/sliders/fader.js b/src/browser/js/app/widgets/sliders/fader.js index 99b79899..50b557d6 100644 --- a/src/browser/js/app/widgets/sliders/fader.js +++ b/src/browser/js/app/widgets/sliders/fader.js @@ -191,6 +191,21 @@ module.exports = class Fader extends Slider { if (ratio != 1) this.ctx.scale(ratio, ratio) } + if (this.getProp('gradient')) { + var colors = this.getProp('gradient') + if (Array.isArray(colors) && colors.length > 1) { + 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]) + } + this.colors.gradient = grad + } catch(err) { + this.errors.gradient = err + } + } + } + } @@ -211,7 +226,8 @@ module.exports = class Fader extends Slider { if (this.getProp('compact')) { this.ctx.globalAlpha = (dashed ? .3 : .2) + 0.2 * Math.abs(d-o) / (d