Skip to content

Instantly share code, notes, and snippets.

View rowan-m's full-sized avatar
🐢
I'll get to it eventually.

Rowan Merewood rowan-m

🐢
I'll get to it eventually.
View GitHub Profile
// _onPointerdown(e)
this._initialTouchAngle = Math.atan2(
this._touchY - this._centerY,
this._touchX - this._centerX
);
// _onPointerdown(e)
this._touchX = e.clientX;
this._touchY = e.clientY;
this._centerX =
this.offsetLeft - this.scrollLeft + this.clientLeft + this.offsetWidth / 2;
this._centerY =
this.offsetTop - this.scrollTop + this.clientTop + this.offsetHeight / 2;
this._initialAngle = this._angle;
const TWO_PI = 2 * Math.PI;
host {
display: inline-block;
user-select: none;
-webkit-user-select: none;
touch-action: none;
}
#container {
--angle: 0rad;
transform: rotate(var(--angle));
}
class InputKnob extends HTMLElement {
// ✂️ existing code has been removed
constructor() {
// Capture the pointer so that the user can still rotate the element even
// if they leave the bounding area of it.
this.setPointerCapture(e.pointerId);
// Re-bind this for listeners so that this == instance of this class
this._onPointerdown = this._onPointerdown.bind(this);
this._onPointermove = this._onPointermove.bind(this);
this._onPointerup = this._onPointerup.bind(this);
// connectedCallback()
if (!this._container.part) {
// create a <span>
const wrapper = document.createElement('span');
// move this element's child nodes into it
wrapper.append(...this.childNodes);
// add the classes
wrapper.classList.add('fallback');
this.classList.add('fallback');
// and add the wrapper into this element
// connectedCallback()
if (!this._container.part) {
// create a <span>
const wrapper = document.createElement('span');
// move this element's child nodes into it
wrapper.append(...this.childNodes);
// add the classes
wrapper.classList.add('fallback');
this.classList.add('fallback');
// and add the wrapper into this element
<script src="https://unpkg.com/@webcomponents/webcomponentsjs"></script>
<input-knob value="2.5"></input-knob>
<input type="range" name="angle" min="0" max="6.28" step="0.1" value="1" />
<script>
const knob = document.querySelector('input-knob');
const slider = document.querySelector('input[type=range]');
// Just take the value from the slider and set it straight on the knob
slider.addEventListener('input', () => { knob.value = slider.value; });
</script>
// class InputKnob
connectedCallback() {
this._drawState();
}
attributeChangedCallback(attrName, oldVal, newVal) {
this._drawState();
}