fader: add knobSize property

This commit is contained in:
bordun 2020-09-02 16:41:43 +02:00
parent 6d4baa38dd
commit e2e4ac610f
4 changed files with 27 additions and 15 deletions

View file

@ -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

View file

@ -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]]
}

View file

@ -17,6 +17,7 @@
height: 200rem;
--widget-padding: 1rem;
--knob-size: 20;
canvas {
text-align: left;