From c1d692ffba0c15b77bfc61270e9aebba7e1e0b01 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 20 Jun 2026 19:20:14 +0200 Subject: [PATCH] knob: add encoder design (& DRY) --- CHANGELOG.md | 1 + src/client/widgets/sliders/encoder.mjs | 62 +----- src/client/widgets/sliders/knob.mjs | 290 ++++++++++++++----------- 3 files changed, 170 insertions(+), 183 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73e58ba5..834048cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - widgets - encoder: add `horizontal` mode + - knob: add `encoder` design ## 1.30.3 diff --git a/src/client/widgets/sliders/encoder.mjs b/src/client/widgets/sliders/encoder.mjs index 1e94c58f..5fe30ae6 100644 --- a/src/client/widgets/sliders/encoder.mjs +++ b/src/client/widgets/sliders/encoder.mjs @@ -1,7 +1,7 @@ import Knob from './knob.mjs' import StaticProperties from '../mixins/static_properties.mjs' -export default class Encoder extends StaticProperties(Knob, {angle: 360, range: {min: 0, max: 1}}) { +export default class Encoder extends StaticProperties(Knob, {angle: 360, range: {min: 0, max: 1}, design: 'encoder'}) { static description() { @@ -263,64 +263,4 @@ export default class Encoder extends StaticProperties(Knob, {angle: 360, range: } - draw() { - - var percent = this.percent, - d = this.percentToAngle(percent), - angle = 2 * Math.PI / 16, - min = this.percentToAngle(0), - max = this.percentToAngle(100), - minRadius = this.minDimension / 6, - maxRadius = this.minDimension / 2 - this.cssVars.lineWidth, - gaugeWidth = maxRadius - minRadius, - gaugeRadius = maxRadius - gaugeWidth / 2 - - this.clear() - - if (this.minDimension < 40) { - minRadius /= 2 - gaugeWidth = maxRadius - minRadius - gaugeRadius = maxRadius - gaugeWidth / 2 - } - - // fill - this.ctx.globalAlpha = this.cssVars.alphaFill - 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 (this.cssVars.alphaFillOn) { - this.ctx.globalAlpha = this.cssVars.alphaFillOn - this.ctx.beginPath() - this.ctx.arc(this.width / 2, this.height / 2, gaugeRadius, d - angle, d + angle) - this.ctx.stroke() - } - - // stroke - if (this.cssVars.lineWidth) { - - this.ctx.globalAlpha = this.cssVars.alphaStroke - this.ctx.strokeStyle = this.cssVars.colorStroke - this.ctx.lineWidth = this.cssVars.lineWidth - - 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() - - } - - } - } diff --git a/src/client/widgets/sliders/knob.mjs b/src/client/widgets/sliders/knob.mjs index 0e4355e2..a364e1ff 100644 --- a/src/client/widgets/sliders/knob.mjs +++ b/src/client/widgets/sliders/knob.mjs @@ -15,7 +15,7 @@ class Knob extends Slider { var defaults = super.defaults(Knob).extend({ style: { _separator_knob_style: 'Knob style', - design: {type: 'string', value: 'default', choices: ['default', 'solid', 'line'], help: [ + design: {type: 'string', value: 'default', choices: ['default', 'solid', 'line', 'encoder'], help: [ 'Design style', 'Note: "solid" design uses "colorStroke" for the central knob color.' ]}, @@ -193,153 +193,199 @@ class Knob extends Slider { this.clear() - if (this.getProp('design') === 'default') { + switch (this.getProp('design')) { + case '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 (dashed) this.ctx.setLineDash(dashed) - if (dashed) this.ctx.setLineDash(dashed) + 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 (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 + if (this.cssVars.lineWidth) { + this.ctx.globalAlpha = this.cssVars.alphaStroke + this.ctx.strokeStyle = this.cssVars.colorStroke + this.ctx.lineWidth = this.cssVars.lineWidth + + 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.colorKnob + this.ctx.lineWidth = this.cssVars.lineWidth + .5 * PXSCALE + + let r1 = minRadius + this.gaugePadding, + r2 = maxRadius - this.gaugePadding, + a = 2 * Math.PI - d - 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.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() + break } + case 'solid': + { + // center - 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 - if (this.cssVars.lineWidth) { this.ctx.globalAlpha = this.cssVars.alphaStroke - this.ctx.strokeStyle = this.cssVars.colorStroke + this.ctx.fillStyle = this.cssVars.colorKnob + + 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 = this.cssVars.lineWidth + if (dashed) this.ctx.setLineDash(dashed) + + 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 (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 = this.cssVars.lineWidth + .5 * PXSCALE + + let r1 = minRadius, + r2 = maxRadius / 1.25 + PXSCALE , + a = 2 * Math.PI - d + this.ctx.beginPath() - this.ctx.arc(this.width / 2, this.height / 2, maxRadius, 0, 2 * Math.PI) + 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() + break } + case 'line': + { + // fill + this.ctx.strokeStyle = this.cssVars.colorFill + this.ctx.lineWidth = this.cssVars.lineWidth + if (dashed) this.ctx.setLineDash(dashed) - // knob + 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() + } - this.ctx.globalAlpha = 1 - this.ctx.strokeStyle = this.cssVars.colorKnob - this.ctx.lineWidth = this.cssVars.lineWidth + .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() + } - let r1 = minRadius + this.gaugePadding, - r2 = maxRadius - this.gaugePadding, - a = 2 * Math.PI - d + if (dashed) this.ctx.setLineDash([]) - 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() + // knob - } else if (this.getProp('design') === 'solid') { + this.ctx.globalAlpha = 1 + this.ctx.strokeStyle = this.cssVars.colorKnob + this.ctx.lineWidth = this.cssVars.lineWidth - // center + let r1 = minRadius * 1.5, + r2 = maxRadius + this.cssVars.lineWidth / 2 - 0.5 * PXSCALE, + a = 2 * Math.PI - d - this.ctx.globalAlpha = this.cssVars.alphaStroke - this.ctx.fillStyle = this.cssVars.colorKnob - - 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 = this.cssVars.lineWidth - - if (dashed) this.ctx.setLineDash(dashed) - - 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.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() + break } + case 'encoder': + { + let angle = 2 * Math.PI / 16 - 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() + // fill + this.ctx.globalAlpha = this.cssVars.alphaFill + 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 - angle, max + angle) + this.ctx.stroke() + } + + if (this.cssVars.alphaFillOn) { + this.ctx.globalAlpha = this.cssVars.alphaFillOn + this.ctx.beginPath() + this.ctx.arc(this.width / 2, this.height / 2, gaugeRadius, d - angle, d + angle) + this.ctx.stroke() + } + + // stroke + if (this.cssVars.lineWidth) { + + this.ctx.globalAlpha = this.cssVars.alphaStroke + this.ctx.strokeStyle = this.cssVars.colorStroke + this.ctx.lineWidth = this.cssVars.lineWidth + + 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() + + } + + break } - - if (dashed) this.ctx.setLineDash([]) - - // knob - - this.ctx.globalAlpha = 1 - this.ctx.strokeStyle = this.cssVars.colorBg - this.ctx.lineWidth = this.cssVars.lineWidth + .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 = this.cssVars.lineWidth - - if (dashed) this.ctx.setLineDash(dashed) - - 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 (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.colorKnob - this.ctx.lineWidth = this.cssVars.lineWidth - - let r1 = minRadius * 1.5, - r2 = maxRadius + this.cssVars.lineWidth / 2 - 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() - } @@ -369,7 +415,7 @@ class Knob extends Slider { this.ctx.globalAlpha = this.cssVars.alphaPipsText for (var p in this.pipTexts) { if (this.pipTexts[p] === undefined || (this.maxAngle === 360 && p === '100')) continue - var angle = this.percentToAngle(p), + let angle = this.percentToAngle(p), size = this.ctx.measureText(this.pipTexts[p]), x = this.width / 2 + radius * Math.cos(angle) - size.width / 2, y = this.height / 2 + radius * Math.sin(angle)