From e2e4ac610f22a82aa4b8093088e2e9097040cefa Mon Sep 17 00:00:00 2001 From: bordun Date: Wed, 2 Sep 2020 16:41:43 +0200 Subject: [PATCH] fader: add knobSize property --- CHANGELOG.md | 1 + src/client/widgets/sliders/fader.js | 29 +++++++++++++++++++---------- src/client/widgets/sliders/range.js | 11 ++++++----- src/scss/widgets/sliders.scss | 1 + 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e00cf2f..7d592a66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - widgets - knob: add `solid` & `line` designs + - fader: add `knobSize` property ## 1.1.0 diff --git a/src/client/widgets/sliders/fader.js b/src/client/widgets/sliders/fader.js index 522a357d..caf12558 100644 --- a/src/client/widgets/sliders/fader.js +++ b/src/client/widgets/sliders/fader.js @@ -1,7 +1,7 @@ var {clip} = require('../utils'), Slider = require('./slider') -module.exports = class Fader extends Slider { +class Fader extends Slider { static description() { @@ -16,6 +16,7 @@ module.exports = class Fader extends Slider { _class_specific: 'fader', design: {type: 'string', value: 'default', choices: ['default', 'round', 'compact'], help: 'Design style'}, + knobSize: {type: 'number', value: 'auto', help: 'Fader knob size'}, horizontal: {type: 'boolean', value: false, help: 'Set to `true` to display the fader horizontally'}, pips: {type: 'boolean', value: false, help: 'Set to `true` to show range breakpoints (ignored if `design` is `compact`)'}, dashed: {type: 'boolean|array', value: false, help: 'Set to `true` to display a dashed gauge. Can be set as an `array` of two numbers : `[dash_size, gap_size]`'}, @@ -180,7 +181,8 @@ module.exports = class Fader extends Slider { o = Math.round(this.percentToCoord(this.valueToPercent(this.originValue))), m = this.getProp('horizontal') ? this.height / 2 : this.width / 2, dashed = this.dashed, - compact = this.getProp('design') === 'compact' + compact = this.getProp('design') === 'compact', + knobHeight = this.cssVars.knobSize * PXSCALE, knobWidth = knobHeight * .6 * PXSCALE this.clear() @@ -265,7 +267,7 @@ module.exports = class Fader extends Slider { this.ctx.fillStyle = this.cssVars.colorBg this.ctx.beginPath() - this.ctx.rect(m - 6 * PXSCALE, d - 10 * PXSCALE, 12 * PXSCALE, 20 * PXSCALE) + this.ctx.rect(m - knobWidth / 2, d - knobHeight / 2, knobWidth, knobHeight) this.ctx.fill() @@ -274,7 +276,7 @@ module.exports = class Fader extends Slider { this.ctx.beginPath() this.ctx.lineWidth = PXSCALE - this.ctx.rect(m - 5.5 * PXSCALE, d - 9.5 * PXSCALE, 11 * PXSCALE, 19 * PXSCALE) + this.ctx.rect(m - knobWidth / 2 + 0.5 * PXSCALE, d - knobHeight / 2 + 0.5 * PXSCALE, knobWidth - PXSCALE, knobHeight - PXSCALE) this.ctx.stroke() } @@ -286,7 +288,7 @@ module.exports = class Fader extends Slider { this.ctx.rect(m - 3 * PXSCALE, d, 6 * PXSCALE, PXSCALE) this.ctx.fill() - this.clearRect = [m - 10 * PXSCALE, this.gaugePadding - 10 * PXSCALE, 20 * PXSCALE, height - 2 * this.gaugePadding + 20 * PXSCALE] + this.clearRect = [m - knobWidth / 2 - PXSCALE, this.gaugePadding - knobHeight / 2 - PXSCALE, knobWidth + 2 * PXSCALE, height - 2 * this.gaugePadding + knobHeight + 2 * PXSCALE] } else { @@ -294,7 +296,7 @@ module.exports = class Fader extends Slider { this.ctx.fillStyle = this.cssVars.colorFill this.ctx.beginPath() - this.ctx.arc(m, d, 3 * PXSCALE, 0, 2 * Math.PI) + this.ctx.arc(m, d, knobHeight / 6, 0, 2 * Math.PI) this.ctx.fill() @@ -303,18 +305,18 @@ module.exports = class Fader extends Slider { this.ctx.fillStyle = this.cssVars.colorFill this.ctx.beginPath() - this.ctx.arc(m, d, 9 * PXSCALE, 0, 2 * Math.PI) + this.ctx.arc(m, d, knobHeight / 2, 0, 2 * Math.PI) this.ctx.fill() } - this.clearRect = [m - 11 * PXSCALE, this.gaugePadding - 11 * PXSCALE, 22 * PXSCALE, height - 2 * this.gaugePadding + 22 * PXSCALE] + this.clearRect = [m - knobHeight / 2 - PXSCALE, this.gaugePadding - knobHeight / 2 - PXSCALE, knobHeight + 2 * PXSCALE, height - 2 * this.gaugePadding + knobHeight + 2 * PXSCALE] } if (this.getProp('pips') && !compact) { this.ctx.globalAlpha = 1 this.ctx.drawImage(this.pips, 0, 0) - if (!compact) this.clearRect = [this.clearRect, [m + 10 * PXSCALE, 0, 10 * PXSCALE + this.pipsTextSize, height]] + if (!compact) this.clearRect = [this.clearRect, [m + knobWidth / 2, 0, 12 * PXSCALE + this.pipsTextSize, height]] } @@ -346,7 +348,7 @@ module.exports = class Fader extends Slider { if (width % 2 && parseInt(m) !== m) m -= 0.5 - if (!compact) m -= 10 * PXSCALE + if (!compact) m -= 16 * PXSCALE - this.cssVars.knobSize * .3 * PXSCALE ctx.lineWidth = PXSCALE @@ -404,3 +406,10 @@ module.exports = class Fader extends Slider { } } + + +Fader.cssVariables = Fader.prototype.constructor.cssVariables.concat( + {js: 'knobSize', css: '--knob-size'} +) + +module.exports = Fader diff --git a/src/client/widgets/sliders/range.js b/src/client/widgets/sliders/range.js index e123d1e2..a0a4a4e9 100644 --- a/src/client/widgets/sliders/range.js +++ b/src/client/widgets/sliders/range.js @@ -214,7 +214,8 @@ class Range extends Fader { d2 = Math.round(fader.percentToCoord(fader.valueToPercent(this.faders[this.getProp('horizontal')?1:0].value))), m = this.getProp('horizontal') ? this.height / 2 : this.width / 2, dashed = this.getProp('dashed'), - compact = this.getProp('design') === 'compact' + compact = this.getProp('design') === 'compact', + knobHeight = this.cssVars.knobSize * PXSCALE, knobWidth = knobHeight * .6 * PXSCALE this.clear() @@ -306,7 +307,7 @@ class Range extends Fader { this.ctx.fillStyle = this.cssVars.colorBg this.ctx.beginPath() - this.ctx.rect(m - 6 * PXSCALE, _d - 10 * PXSCALE, 12 * PXSCALE, 20 * PXSCALE) + this.ctx.rect(m - knobWidth / 2, _d - knobHeight / 2, knobWidth, knobHeight) this.ctx.fill() this.ctx.globalAlpha = this.cssVars.alphaStroke @@ -314,7 +315,7 @@ class Range extends Fader { this.ctx.lineWidth = PXSCALE this.ctx.beginPath() - this.ctx.rect(m - 5.5 * PXSCALE, _d - 9.5 * PXSCALE, 11 * PXSCALE, 19 * PXSCALE) + this.ctx.rect(m - knobWidth / 2 + 0.5 * PXSCALE, _d - knobHeight / 2 + 0.5 * PXSCALE, knobWidth - PXSCALE, knobHeight - PXSCALE) this.ctx.stroke() } @@ -328,7 +329,7 @@ class Range extends Fader { } - this.clearRect = [m - 10 * PXSCALE, this.gaugePadding - 10 * PXSCALE, 20 * PXSCALE, height - 2 * this.gaugePadding + 20 * PXSCALE] + this.clearRect = [m - knobWidth / 2 - PXSCALE, this.gaugePadding - knobHeight / 2 - PXSCALE, knobWidth + 2 * PXSCALE, height - 2 * this.gaugePadding + knobHeight + 2 * PXSCALE] } else { @@ -366,7 +367,7 @@ class Range extends Fader { if (this.getProp('pips')) { this.ctx.globalAlpha = 1 this.ctx.drawImage(this.pips, 0, 0) - if (!compact) this.clearRect = [this.clearRect, [m + 10 * PXSCALE, 0, 10 * PXSCALE + this.pipsTextSize, height]] + if (!compact) this.clearRect = [this.clearRect, [m + knobWidth / 2, 0, 12 * PXSCALE + this.pipsTextSize, height]] } diff --git a/src/scss/widgets/sliders.scss b/src/scss/widgets/sliders.scss index 59e4d029..4f970374 100644 --- a/src/scss/widgets/sliders.scss +++ b/src/scss/widgets/sliders.scss @@ -17,6 +17,7 @@ height: 200rem; --widget-padding: 1rem; + --knob-size: 20; canvas { text-align: left;