knob: add solid & line designs

This commit is contained in:
bordun 2020-09-02 16:05:52 +02:00
parent c7c39d1f2f
commit 6d4baa38dd
3 changed files with 148 additions and 112 deletions

View file

@ -15,6 +15,10 @@ module.exports = class Knob extends Slider {
_class_specific: 'knob',
design: {type: 'string', value: 'default', choices: ['default', 'solid', 'line'], help: [
'Design style',
'Note: "solid" design uses "colorStroke" for the central knob color.'
]},
mode: {type: 'string', value: 'vertical', choices: ['vertical', 'circular', 'snap'], help: [
'- `circular`: relative move in circular motion',
'- `snap`: snap to touch position and move in vertical motion',
@ -182,126 +186,154 @@ module.exports = class Knob extends Slider {
this.clear()
// if (this.getProp('design') === 'default') {
if (this.getProp('design') === 'default') {
if (this.minDimension < 40) {
minRadius /= 2
gaugeWidth = maxRadius - minRadius
gaugeRadius = maxRadius - gaugeWidth / 2
}
if (this.minDimension < 40) {
minRadius /= 2
gaugeWidth = maxRadius - minRadius
gaugeRadius = maxRadius - gaugeWidth / 2
}
// fill
this.ctx.strokeStyle = this.cssVars.colorFill
this.ctx.lineWidth = gaugeWidth - this.gaugePadding * 2
// fill
this.ctx.strokeStyle = this.cssVars.colorFill
this.ctx.lineWidth = gaugeWidth - this.gaugePadding * 2
if (this.cssVars.alphaFillOff) {
this.ctx.globalAlpha = this.cssVars.alphaFillOff
this.ctx.beginPath()
this.ctx.arc(this.width / 2, this.height / 2, gaugeRadius, min, max)
this.ctx.stroke()
}
if (dashed) this.ctx.setLineDash(dashed)
if (this.cssVars.alphaFillOn) {
this.ctx.globalAlpha = this.cssVars.alphaFillOn
this.ctx.beginPath()
this.ctx.arc(this.width / 2, this.height / 2, gaugeRadius, o, d, o > d)
this.ctx.stroke()
}
if (dashed) this.ctx.setLineDash([])
// stroke
this.ctx.globalAlpha = this.cssVars.alphaStroke
this.ctx.strokeStyle = this.cssVars.colorStroke
this.ctx.lineWidth = PXSCALE
if (this.cssVars.alphaFillOff) {
this.ctx.globalAlpha = this.cssVars.alphaFillOff
this.ctx.beginPath()
this.ctx.arc(this.width / 2, this.height / 2, gaugeRadius, min, max)
this.ctx.arc(this.width / 2, this.height / 2, maxRadius, 0, 2 * Math.PI)
this.ctx.stroke()
}
if (dashed) this.ctx.setLineDash(dashed)
if (this.cssVars.alphaFillOn) {
this.ctx.globalAlpha = this.cssVars.alphaFillOn
// knob
this.ctx.globalAlpha = 1
this.ctx.strokeStyle = this.cssVars.colorFill
this.ctx.lineWidth = 2.5 * PXSCALE
let r1 = minRadius + this.gaugePadding,
r2 = maxRadius - this.gaugePadding,
a = 2 * Math.PI - d
this.ctx.beginPath()
this.ctx.arc(this.width / 2, this.height / 2, gaugeRadius, o, d, o > d)
this.ctx.moveTo(r1 * Math.cos(a) + this.width / 2, this.height / 2 - r1 * Math.sin(a))
this.ctx.lineTo(r2 * Math.cos(a) + this.width / 2, this.height / 2 - r2 * Math.sin(a))
this.ctx.stroke()
} else if (this.getProp('design') === 'solid') {
// center
this.ctx.globalAlpha = this.cssVars.alphaStroke
this.ctx.fillStyle = this.cssVars.colorStroke
this.ctx.beginPath()
this.ctx.arc(this.width / 2, this.height / 2, maxRadius / 1.25, 0, 2 * Math.PI)
this.ctx.fill()
// fill
this.ctx.strokeStyle = this.cssVars.colorFill
this.ctx.lineWidth = 2 * PXSCALE
if (this.cssVars.alphaFillOff) {
this.ctx.globalAlpha = this.cssVars.alphaFillOff
this.ctx.beginPath()
this.ctx.arc(this.width / 2, this.height / 2, maxRadius, min, max)
this.ctx.stroke()
}
if (dashed) this.ctx.setLineDash(dashed)
if (this.cssVars.alphaFillOn) {
this.ctx.globalAlpha = this.cssVars.alphaFillOn
this.ctx.beginPath()
this.ctx.arc(this.width / 2, this.height / 2, maxRadius, o, d, o > d)
this.ctx.stroke()
}
if (dashed) this.ctx.setLineDash([])
// knob
this.ctx.globalAlpha = 1
this.ctx.strokeStyle = this.cssVars.colorBg
this.ctx.lineWidth = 2.5 * PXSCALE
let r1 = minRadius,
r2 = maxRadius / 1.25 + PXSCALE ,
a = 2 * Math.PI - d
this.ctx.beginPath()
this.ctx.moveTo(r1 * Math.cos(a) + this.width / 2, this.height / 2 - r1 * Math.sin(a))
this.ctx.lineTo(r2 * Math.cos(a) + this.width / 2, this.height / 2 - r2 * Math.sin(a))
this.ctx.stroke()
} else {
// fill
this.ctx.strokeStyle = this.cssVars.colorFill
this.ctx.lineWidth = 2 * PXSCALE
if (this.cssVars.alphaFillOff) {
this.ctx.globalAlpha = this.cssVars.alphaFillOff
this.ctx.beginPath()
this.ctx.arc(this.width / 2, this.height / 2, maxRadius, min, max)
this.ctx.stroke()
}
if (dashed) this.ctx.setLineDash(dashed)
if (this.cssVars.alphaFillOn) {
this.ctx.globalAlpha = this.cssVars.alphaFillOn
this.ctx.beginPath()
this.ctx.arc(this.width / 2, this.height / 2, maxRadius, o, d, o > d)
this.ctx.stroke()
}
if (dashed) this.ctx.setLineDash([])
// knob
this.ctx.globalAlpha = 1
this.ctx.strokeStyle = this.cssVars.colorFill
this.ctx.lineWidth = 2.5 * PXSCALE
let r1 = minRadius * 1.5,
r2 = maxRadius + 0.5 * PXSCALE,
a = 2 * Math.PI - d
this.ctx.beginPath()
this.ctx.moveTo(r1 * Math.cos(a) + this.width / 2, this.height / 2 - r1 * Math.sin(a))
this.ctx.lineTo(r2 * Math.cos(a) + this.width / 2, this.height / 2 - r2 * Math.sin(a))
this.ctx.stroke()
}
if (dashed) this.ctx.setLineDash([])
// stroke
this.ctx.globalAlpha = this.cssVars.alphaStroke
this.ctx.strokeStyle = this.cssVars.colorStroke
this.ctx.lineWidth = PXSCALE
//
// if (this.minDimension > 40) {
// this.ctx.beginPath()
// this.ctx.arc(this.width / 2, this.height / 2, minRadius, 0, 2 * Math.PI)
// this.ctx.stroke()
// }
this.ctx.beginPath()
this.ctx.arc(this.width / 2, this.height / 2, maxRadius, 0, 2 * Math.PI)
this.ctx.stroke()
// knob
this.ctx.globalAlpha = 1
this.ctx.strokeStyle = this.cssVars.colorFill
this.ctx.lineWidth = 1.5 * PXSCALE
let r1 = minRadius + this.gaugePadding,
r2 = maxRadius - this.gaugePadding,
a = 2 * Math.PI - d
this.ctx.beginPath()
this.ctx.moveTo(r1 * Math.cos(a) + this.width / 2, this.height / 2 - r1 * Math.sin(a))
this.ctx.lineTo(r2 * Math.cos(a) + this.width / 2, this.height / 2 - r2 * Math.sin(a))
this.ctx.stroke()
// } else {
//
// // fill
// this.ctx.strokeStyle = this.cssVars.colorFill
// this.ctx.lineWidth = 2 * PXSCALE
//
// if (this.cssVars.alphaFillOff) {
// this.ctx.globalAlpha = this.cssVars.alphaFillOff
// this.ctx.beginPath()
// this.ctx.arc(this.width / 2, this.height / 2, maxRadius, min, max)
// this.ctx.stroke()
// }
//
// if (dashed) this.ctx.setLineDash([1.5 * PXSCALE, 1.5 * PXSCALE])
//
// if (this.cssVars.alphaFillOn) {
// this.ctx.globalAlpha = this.cssVars.alphaFillOn
// this.ctx.beginPath()
// this.ctx.arc(this.width / 2, this.height / 2, maxRadius, o, d, o > d)
// this.ctx.stroke()
// }
//
// if (dashed) this.ctx.setLineDash([])
//
//
//
// // center
// this.ctx.fillStyle = this.cssVars.colorBg
// this.ctx.strokeStyle = this.cssVars.colorStroke
// this.ctx.lineWidth = 1 * PXSCALE
//
// this.ctx.beginPath()
// this.ctx.arc(this.width / 2, this.height / 2, maxRadius - (2 + this.cssVars.padding) * PXSCALE, 0, 2 * Math.PI)
// this.ctx.globalAlpha = this.cssVars.alphaStroke
// this.ctx.stroke()
// this.ctx.globalAlpha = 1
// // this.ctx.fill()
//
//
//
// // knob
// this.ctx.globalAlpha = 1
// this.ctx.fillStyle = this.cssVars.colorFill
// this.ctx.lineWidth = 1.5 * PXSCALE
//
// let r1 = maxRadius - (8 + this.cssVars.padding) * PXSCALE,
// a = 2 * Math.PI - d
//
// this.ctx.beginPath()
// this.ctx.arc(r1 * Math.cos(a) + this.width / 2, this.height / 2 - r1 * Math.sin(a), 1.5 * PXSCALE, 0, 2*Math.PI)
// this.ctx.fill()
//
//
// }
// pips
@ -313,7 +345,7 @@ module.exports = class Knob extends Slider {
for (var pip of this.rangeKeys) {
let r1 = maxRadius + 2 * PXSCALE,
let r1 = maxRadius + 3 * PXSCALE,
r2 = r1 + 4 * PXSCALE,
a = 2 * Math.PI - this.percentToAngle(pip)

View file

@ -52,8 +52,7 @@
--widget-padding: 1rem;
--alpha-stroke: 0.75;
&.design-classic {
--alpha-stroke: 0.5;
&.design-solid, &.design-line {
--alpha-fill-off: 0.25;
--alpha-fill-on: 1;
}