|
| 1 | +/** |
| 2 | + * WebAudio Knob Control |
| 3 | + */ |
| 4 | +class WAKnob extends HTMLElement { |
| 5 | + private _value: number = 0; |
| 6 | + private _min: number = 0; |
| 7 | + private _max: number = 127; |
| 8 | + private _default: number = 0; |
| 9 | + |
| 10 | + static get observedAttributes() { |
| 11 | + return ['value', 'min', 'max', 'default']; |
| 12 | + } |
| 13 | + |
| 14 | + constructor() { |
| 15 | + super(); |
| 16 | + this.attachShadow({ mode: 'open' }); |
| 17 | + this.render(); |
| 18 | + } |
| 19 | + |
| 20 | + get value() { |
| 21 | + return this._value; |
| 22 | + } |
| 23 | + |
| 24 | + set value(val: number) { |
| 25 | + const newValue = this.constrainValue(val); |
| 26 | + if (newValue !== this._value) { |
| 27 | + this._value = newValue; |
| 28 | + this.setAttribute('value', newValue.toString()); |
| 29 | + this.render(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + get min() { |
| 34 | + return this._min; |
| 35 | + } |
| 36 | + |
| 37 | + set min(val: number) { |
| 38 | + this._min = val; |
| 39 | + this.value = this._value; // Recheck constraints |
| 40 | + } |
| 41 | + |
| 42 | + get max() { |
| 43 | + return this._max; |
| 44 | + } |
| 45 | + |
| 46 | + set max(val: number) { |
| 47 | + this._max = val; |
| 48 | + this.value = this._value; // Recheck constraints |
| 49 | + } |
| 50 | + |
| 51 | + get default() { |
| 52 | + return this._default; |
| 53 | + } |
| 54 | + |
| 55 | + set default(val: number) { |
| 56 | + this._default = val; |
| 57 | + if (this._value === 0) { |
| 58 | + this.value = val; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private constrainValue(val: number): number { |
| 63 | + return Math.min(this._max, Math.max(this._min, val)); |
| 64 | + } |
| 65 | + |
| 66 | + private render() { |
| 67 | + if (!this.shadowRoot) return; |
| 68 | + this.shadowRoot.innerHTML = ` |
| 69 | + <div>Value: ${this._value} (Min: ${this._min}, Max: ${this._max})</div> |
| 70 | + `; |
| 71 | + } |
| 72 | + |
| 73 | + attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) { |
| 74 | + if (oldValue === newValue) return; |
| 75 | + |
| 76 | + switch (name) { |
| 77 | + case 'value': { |
| 78 | + const val = parseFloat(newValue || '0'); |
| 79 | + const constrained = this.constrainValue(val); |
| 80 | + this._value = constrained; |
| 81 | + this.setAttribute('value', constrained.toString()); |
| 82 | + this.render(); |
| 83 | + break; |
| 84 | + } |
| 85 | + case 'min': |
| 86 | + this.min = parseFloat(newValue || '0'); |
| 87 | + break; |
| 88 | + case 'max': |
| 89 | + this.max = parseFloat(newValue || '127'); |
| 90 | + break; |
| 91 | + case 'default': |
| 92 | + this.default = parseFloat(newValue || '0'); |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + connectedCallback() { |
| 98 | + // Set to default value if no value was specified |
| 99 | + if (this._value === 0 && this._default !== 0) { |
| 100 | + this.value = this._default; |
| 101 | + } |
| 102 | + this.setAttribute('value', this._value.toString()); |
| 103 | + this.setAttribute('default', this._default.toString()); |
| 104 | + this.setAttribute('min', this._min.toString()); |
| 105 | + this.setAttribute('max', this._max.toString()); |
| 106 | + this.render(); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +customElements.define('wa-knob', WAKnob); |
0 commit comments