Files
@ d1f86109e3cc
Branch filter:
Location: light9/web/fade/Light9Fader.ts
d1f86109e3cc
4.1 KiB
video/MP2T
more *value getter variants
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | import debug from "debug";
import { css, html, LitElement, PropertyValueMap } from "lit";
import { customElement, property, query } from "lit/decorators.js";
import { clamp } from "../floating_color_picker";
const log = debug("fade");
class Drag {
constructor(public startDragPxY: number, public startDragValue: number) {}
}
@customElement("light9-fader")
export class Light9Fader extends LitElement {
static styles = [
css`
:host {
display: inline-block;
border: 2px gray inset;
background: #000;
height: 80px;
}
#handle {
background: gray;
border: 3px outset #838499;
position: relative;
left: 0px;
right: -25px;
border-radius: 4px;
margin: 0 1px;
}
`,
];
@property() value: number = 0;
@query("#handle") handleEl!: HTMLElement;
troughHeight = 80 - 2 - 2 - 5 - 5;
handleHeight = 16;
drag?: Drag;
unmutedValue: number = 1;
render() {
return html` <div id="handle"><hr /></div> `;
}
protected update(changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>): void {
super.update(changedProperties);
if (changedProperties.has("value")) {
}
}
valueChangedFromUi() {
this.value = clamp(this.value, 0, 1);
this.dispatchEvent(new CustomEvent("change", { detail: { value: this.value } }));
}
protected updated(_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>): void {
super.updated(_changedProperties);
const y = this.sliderTopY(this.value);
this.handleEl.style.top = y + "px";
}
protected firstUpdated(_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>): void {
super.firstUpdated(_changedProperties);
this.handleEl.style.height = this.handleHeight + "px";
this.events();
}
events() {
const hand = this.handleEl;
hand.addEventListener("mousedown", (ev: MouseEvent) => {
ev.stopPropagation();
if (ev.buttons == 1) {
this.drag = new Drag(ev.clientY, this.value);
} else if (ev.buttons == 2) {
this.onRmb();
}
});
this.addEventListener("mousedown", (ev: MouseEvent) => {
ev.stopPropagation();
if (ev.buttons == 1) {
this.value = this.sliderValue(ev.offsetY);
this.valueChangedFromUi();
this.drag = new Drag(ev.clientY, this.value);
} else if (ev.buttons == 2) {
// RMB in trough
this.onRmb();
}
});
this.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
this.addEventListener("wheel", (ev: WheelEvent) => {
ev.preventDefault();
this.value += (ev.deltaY / this.troughHeight) * -0.03;
this.valueChangedFromUi();
});
const maybeDrag = (ev: MouseEvent) => {
if (ev.buttons != 1) return;
if (this.drag === undefined) return;
ev.stopPropagation();
this.onMouseDrag(ev.clientY - this.drag.startDragPxY!);
};
hand.addEventListener("mousemove", maybeDrag);
this.addEventListener("mousemove", maybeDrag);
window.addEventListener("mousemove", maybeDrag);
hand.addEventListener("mouseup", this.onMouseUpAnywhere.bind(this));
this.addEventListener("mouseup", this.onMouseUpAnywhere.bind(this));
window.addEventListener("mouseup", this.onMouseUpAnywhere.bind(this));
}
onRmb() {
if (this.value > 0.1) {
// mute
this.unmutedValue = this.value;
this.value = 0;
} else {
// unmute
this.value = this.unmutedValue;
}
this.valueChangedFromUi();
}
onMouseDrag(dy: number) {
if (this.drag === undefined) throw "unexpected";
this.value = this.drag.startDragValue - dy / this.troughHeight;
this.valueChangedFromUi();
}
onMouseUpAnywhere() {
this.drag = undefined;
}
sliderTopY(value: number): number {
const usableY = this.troughHeight - this.handleHeight / 2 - 1;
const yAdj = this.handleHeight / 2 - 5 - 2;
return (1 - value) * usableY + yAdj;
}
sliderValue(offsetY: number): number {
const usableY = this.troughHeight - this.handleHeight;
const yAdj = this.handleHeight / 2 - 5 - 2;
return clamp(1 - (offsetY - yAdj) / usableY, 0, 1);
}
}
|