This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// _onPointerdown(e) | |
this._initialTouchAngle = Math.atan2( | |
this._touchY - this._centerY, | |
this._touchX - this._centerX | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// _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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TWO_PI = 2 * Math.PI; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
host { | |
display: inline-block; | |
user-select: none; | |
-webkit-user-select: none; | |
touch-action: none; | |
} | |
#container { | |
--angle: 0rad; | |
transform: rotate(var(--angle)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://unpkg.com/@webcomponents/webcomponentsjs"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// class InputKnob | |
connectedCallback() { | |
this._drawState(); | |
} | |
attributeChangedCallback(attrName, oldVal, newVal) { | |
this._drawState(); | |
} |