-
Notifications
You must be signed in to change notification settings - Fork 54
Slide Potentiometer #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
urish
merged 24 commits into
wokwi:master
from
Spinkelben:spinkelben/slide-potentiometer
May 24, 2021
Merged
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ea1b98a
Initial commit and art
Spinkelben d9d7217
Improved visual design. Cleaned up svg
Spinkelben 163d187
Add pins to svg
Spinkelben ae294b9
Add some interaction
Spinkelben 71792bc
Move tip based on value property
Spinkelben 8337474
WIP on moving tip with mouse
Spinkelben 5d55f55
Implement touch and keyboard actions
Spinkelben 8757130
Add pins and highlight
Spinkelben 2953368
Improved mouse handling
Spinkelben 1a109d7
Fix pin numbers
Spinkelben 7a52041
Add mouse handling outside element
Spinkelben e501d39
Change pin names
Spinkelben e97d70f
Remove commented out code
Spinkelben 6c32435
Remove events with no purpose
Spinkelben 00bf779
Fix slider movement when potentiometer is rotated
Spinkelben a612951
Merge branch 'master' of https://github.com/wokwi/wokwi-elements into…
Spinkelben 727da9d
Fix pin coordinates
Spinkelben 4a1ce69
PR Comments: Remove extra properties, add portrait story
Spinkelben 37e730a
Attempt at story where you can change the minValue and maxValue
Spinkelben 4646419
Make the rotation rotated story configurable
Spinkelben 8e026c2
Changed range and step
Spinkelben 0079a50
Clamp function, PR comments
Spinkelben 526d323
Use clamp module in potentiometer element
Spinkelben d97990b
Hookup the input events in the actions tab
Spinkelben File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { html } from 'lit-html'; | ||
| import { action } from '@storybook/addon-actions'; | ||
| import './slide-potentiometer-element'; | ||
|
|
||
| export default { | ||
| title: 'Slide Potentiometer', | ||
| component: 'wokwi-slide-potentiometer', | ||
| argTypes: { | ||
Spinkelben marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| value: { control: { type: 'number', min: 1, max: 10, step: 1 } }, | ||
| }, | ||
| args: { | ||
| value: 5, | ||
| }, | ||
| }; | ||
|
|
||
| export const Default = () => html`<wokwi-slide-potentiometer />`; | ||
|
|
||
| export const Portrait = () => | ||
| html` | ||
| <div style="transform: rotate(90deg) translate(50%, 50%);"> | ||
| <wokwi-slide-potentiometer /> | ||
| </div> | ||
| `; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,247 @@ | ||
| import { css, customElement, html, LitElement, property, svg } from 'lit-element'; | ||
| import { analog, ElementPin } from './pin'; | ||
|
|
||
| @customElement('wokwi-slide-potentiometer') | ||
| export class SlidePotentiometerElement extends LitElement { | ||
| @property() value = 0; | ||
| @property() minValue = 0; | ||
| @property() maxValue = 50; | ||
Spinkelben marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| readonly pinInfo: ElementPin[] = [ | ||
| { name: 'VCC', x: 1, y: 43, number: 1, signals: [{ type: 'power', signal: 'VCC' }] }, | ||
| { name: 'SIG', x: 1, y: 66.5, number: 2, signals: [analog(0)] }, | ||
| { name: 'GND', x: 207, y: 43, number: 3, signals: [{ type: 'power', signal: 'GND' }] }, | ||
| ]; | ||
| private isPressed = false; | ||
| private zoom = 1; | ||
| private pageToLocalTransformationMatrix: DOMMatrix | null = null; | ||
|
|
||
| static get styles() { | ||
| return css` | ||
| .hide-input { | ||
| position: absolute; | ||
| clip: rect(0 0 0 0); | ||
| width: 1px; | ||
| height: 1px; | ||
| margin: -1px; | ||
| } | ||
| input:focus + svg #tip { | ||
| /* some style to add when the element has focus */ | ||
| filter: url(#outline); | ||
| } | ||
| `; | ||
| } | ||
|
|
||
| render() { | ||
| const { value, minValue, maxValue } = this; | ||
| const tipTravelInMM = 30; | ||
| // Tip is centered by default | ||
| const tipBaseOffsetX = -(tipTravelInMM / 2); | ||
| const tipMovementX = (value / (maxValue - minValue)) * tipTravelInMM; | ||
| const tipOffSetX = tipMovementX + tipBaseOffsetX; | ||
| return html` | ||
| <input | ||
| tabindex="0" | ||
| type="range" | ||
| min="${this.minValue}" | ||
| max="${this.maxValue}" | ||
| value="${this.value}" | ||
| step="1" | ||
| aria-valuemin="${this.minValue}" | ||
| aria-valuenow="${this.value}" | ||
| aria-valuemax="${this.maxValue}" | ||
| @input="${this.onInputValueChange}" | ||
| class="hide-input" | ||
| /> | ||
| <svg | ||
| width="55mm" | ||
| height="29mm" | ||
| version="1.1" | ||
| viewBox="0 0 55 29" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| xmlns:osb="http://www.openswatchbook.org/uri/2009/osb" | ||
Spinkelben marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| xmlns:xlink="http://www.w3.org/1999/xlink" | ||
| > | ||
| <defs> | ||
| <filter id="outline"> | ||
| <feDropShadow dx="0" dy="0" stdDeviation="1" flood-color="#4faaff" /> | ||
| </filter> | ||
| <linearGradient | ||
| id="tipGradient" | ||
| x1="36.482" | ||
| x2="50.447" | ||
| y1="91.25" | ||
| y2="91.25" | ||
| gradientTransform="matrix(.8593 0 0 1.1151 -14.849 -92.256)" | ||
| gradientUnits="userSpaceOnUse" | ||
| > | ||
| <stop stop-color="#1a1a1a" offset="0" /> | ||
| <stop stop-color="#595959" offset=".4" /> | ||
| <stop stop-color="#595959" offset=".6" /> | ||
| <stop stop-color="#1a1a1a" offset="1" /> | ||
| </linearGradient> | ||
| <radialGradient | ||
| id="bodyGradient" | ||
| cx="62.59" | ||
| cy="65.437" | ||
| r="22.5" | ||
| gradientTransform="matrix(1.9295 3.7154e-8 0 .49697 -98.268 -23.02)" | ||
| gradientUnits="userSpaceOnUse" | ||
| > | ||
| <stop stop-color="#d2d2d2" offset="0" /> | ||
| <stop stop-color="#2a2a2a" offset="1" /> | ||
| </radialGradient> | ||
| <g id="screw"> | ||
| <circle cx="0" cy="0" r="1" fill="#858585" stroke="#000" stroke-width=".05" /> | ||
| <path d="m0 1 0-2" fill="none" stroke="#000" stroke-width=".151" /> | ||
| </g> | ||
| </defs> | ||
| <!-- pins --> | ||
| <g fill="#ccc"> | ||
| <rect x="0" y="11" width="5" height="0.75" /> | ||
| <rect x="50" y="11" width="5" height="0.75" /> | ||
| <rect x="0" y="17.25" width="5" height="0.75" /> | ||
| </g> | ||
| <g transform="translate(5 5)"> | ||
| <!-- Body --> | ||
| <rect | ||
| id="sliderCase" | ||
| x="0" | ||
| y="5" | ||
| width="45" | ||
| height="9" | ||
| rx=".2" | ||
| ry=".2" | ||
| fill="url(#bodyGradient)" | ||
| fill-rule="evenodd" | ||
| /> | ||
| <rect x="3.25" y="8" width="38.5" height="3" rx=".1" ry=".1" fill="#3f1e1e" /> | ||
| <!-- Screw Left --> | ||
| <g transform="translate(1.625 9.5) rotate(45)"> | ||
| <use href="#screw" /> | ||
| </g> | ||
| <!-- Screw Right --> | ||
| <g transform="translate(43.375 9.5) rotate(45)"> | ||
| <use href="#screw" /> | ||
| </g> | ||
| <!-- Tip --> | ||
| <g | ||
| id="tip" | ||
| transform="translate(${tipOffSetX} 0)" | ||
| @mousedown=${this.down} | ||
| @touchstart=${this.down} | ||
| @touchmove=${this.touchMove} | ||
| @touchend=${this.up} | ||
| @keydown=${this.down} | ||
| @keyup=${this.up} | ||
| @click="${this.focusInput}" | ||
| > | ||
| <rect x="19.75" y="8.6" width="5.5" height="1.8" /> | ||
| <rect | ||
| x="16.5" | ||
| y="0" | ||
| width="12" | ||
| height="19" | ||
| fill="url(#tipGradient)" | ||
| stroke-width="2.6518" | ||
| rx=".1" | ||
| ry=".1" | ||
| /> | ||
| <rect x="22.2" y="0" width=".6" height="19" fill="#efefef" /> | ||
| </g> | ||
| </g> | ||
| </svg> | ||
| `; | ||
| } | ||
|
|
||
| connectedCallback() { | ||
| super.connectedCallback(); | ||
| window.addEventListener('mouseup', this.up); | ||
| window.addEventListener('mousemove', this.mouseMove); | ||
| window.addEventListener('mouseleave', this.up); | ||
| } | ||
|
|
||
| disconnectedCallback() { | ||
| super.disconnectedCallback(); | ||
| window.removeEventListener('mouseup', this.up); | ||
| window.removeEventListener('mousemove', this.mouseMove); | ||
| window.removeEventListener('mouseleave', this.up); | ||
| } | ||
|
|
||
| private focusInput() { | ||
| const inputEl: HTMLInputElement | null | undefined = this.shadowRoot?.querySelector( | ||
| '.hide-input' | ||
| ); | ||
| inputEl?.focus(); | ||
| } | ||
|
|
||
| private down(): void { | ||
| if (!this.isPressed) { | ||
| this.updateCaseDisplayProperties(); | ||
| } | ||
| this.isPressed = true; | ||
| } | ||
|
|
||
| private up = () => { | ||
| if (this.isPressed) { | ||
| this.isPressed = false; | ||
| } | ||
| }; | ||
|
|
||
| private updateCaseDisplayProperties(): void { | ||
| const element = this.shadowRoot?.querySelector<SVGRectElement>('#sliderCase'); | ||
| if (element) { | ||
| this.pageToLocalTransformationMatrix = element.getScreenCTM()?.inverse() || null; | ||
| } | ||
|
|
||
| // Handle zooming in the storybook | ||
| const zoom = getComputedStyle(window.document.body)?.zoom; | ||
| if (zoom !== undefined) { | ||
| this.zoom = Number(zoom); | ||
| } else { | ||
| this.zoom = 1; | ||
| } | ||
| } | ||
|
|
||
| private onInputValueChange(event: KeyboardEvent): void { | ||
| const target = event.target as HTMLInputElement; | ||
| if (target.value) { | ||
| this.updateValue(Number(target.value)); | ||
| } | ||
| } | ||
|
|
||
| private mouseMove = (event: MouseEvent) => { | ||
| if (this.isPressed) { | ||
| this.updateValueFromXCoordinate(new DOMPointReadOnly(event.pageX, event.pageY)); | ||
| } | ||
| }; | ||
|
|
||
| private touchMove(event: TouchEvent): void { | ||
| if (this.isPressed) { | ||
| if (event.targetTouches.length > 0) { | ||
| const touchTarget = event.targetTouches[0]; | ||
| this.updateValueFromXCoordinate(new DOMPointReadOnly(touchTarget.pageX, touchTarget.pageY)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private updateValueFromXCoordinate(position: DOMPointReadOnly): void { | ||
| if (this.pageToLocalTransformationMatrix) { | ||
| // Handle zoom first, the transformation matrix does not take that into account | ||
| let localPosition = new DOMPointReadOnly(position.x / this.zoom, position.y / this.zoom); | ||
| // Converts the point from the page coordinate space to the #caseRect coordinate space | ||
| // It also translates the units from pixels to millimeters! | ||
| localPosition = localPosition.matrixTransform(this.pageToLocalTransformationMatrix); | ||
| const caseBorderWidth = 7.5; | ||
| const tipPositionXinMM = localPosition.x - caseBorderWidth; | ||
| const mmPerIncrement = 30 / (this.maxValue - this.minValue); | ||
| this.updateValue(Math.round(tipPositionXinMM / mmPerIncrement)); | ||
| } | ||
| } | ||
|
|
||
| private updateValue(value: number) { | ||
| let clampedValue = Math.min(value, this.maxValue); | ||
| clampedValue = Math.max(clampedValue, this.minValue); | ||
Spinkelben marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.value = clampedValue; | ||
| this.dispatchEvent(new InputEvent('input', { detail: clampedValue })); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused import? or did you want to attach an action to the potentiometer input event?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never became good friends with storybook, what would be the advantage of adding the action on input event? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It means that when you move the potentiometer, you'll see the events firing in the "Actions" tab:
pot-actions.mp4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Still not 100 % what you get from that, but it is quick to wire up, so I have done it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
It's just a quick way to test that the component emits the events correctly. There's an issue where it doesn't show the
detailvalue of the events, otherwise this would also show users the information contained in events.