Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ea1b98a
Initial commit and art
Spinkelben Apr 19, 2021
d9d7217
Improved visual design. Cleaned up svg
Spinkelben Apr 20, 2021
163d187
Add pins to svg
Spinkelben Apr 20, 2021
ae294b9
Add some interaction
Spinkelben Apr 21, 2021
71792bc
Move tip based on value property
Spinkelben Apr 22, 2021
8337474
WIP on moving tip with mouse
Spinkelben Apr 26, 2021
5d55f55
Implement touch and keyboard actions
Spinkelben Apr 30, 2021
8757130
Add pins and highlight
Spinkelben May 4, 2021
2953368
Improved mouse handling
Spinkelben May 4, 2021
1a109d7
Fix pin numbers
Spinkelben May 4, 2021
7a52041
Add mouse handling outside element
Spinkelben May 16, 2021
e501d39
Change pin names
Spinkelben May 16, 2021
e97d70f
Remove commented out code
Spinkelben May 16, 2021
6c32435
Remove events with no purpose
Spinkelben May 16, 2021
00bf779
Fix slider movement when potentiometer is rotated
Spinkelben May 23, 2021
a612951
Merge branch 'master' of https://github.com/wokwi/wokwi-elements into…
Spinkelben May 23, 2021
727da9d
Fix pin coordinates
Spinkelben May 23, 2021
4a1ce69
PR Comments: Remove extra properties, add portrait story
Spinkelben May 23, 2021
37e730a
Attempt at story where you can change the minValue and maxValue
Spinkelben May 23, 2021
4646419
Make the rotation rotated story configurable
Spinkelben May 23, 2021
8e026c2
Changed range and step
Spinkelben May 23, 2021
0079a50
Clamp function, PR comments
Spinkelben May 24, 2021
526d323
Use clamp module in potentiometer element
Spinkelben May 24, 2021
d97990b
Hookup the input events in the actions tab
Spinkelben May 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/slide-potentiometer-element.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ export default {
};

const Template = ({ value }) =>
html` <wokwi-slide-potentiometer
html`<wokwi-slide-potentiometer
value=${value}
@button-press=${action('button-press')}
@button-release=${action('button-release')}
>
</wokwi-slide-potentiometer>`;
/>`;

export const Default = Template.bind({});
Default.args = { value: 5 };
Expand Down
38 changes: 23 additions & 15 deletions src/slide-potentiometer-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ export class SlidePotentiometerElement extends LitElement {
@property() value = 0;
@property() minValue = 0;
@property() maxValue = 50;
@property() mouseX = 0;
@property() mouseY = 0;
@property() rotation = 0;
private isPressed = false;
private caseRect: DOMRect | undefined;
private zoom = 1;
readonly pinInfo: ElementPin[] = [
{ name: 'VCC', x: 17.5, y: 59, number: 1, signals: [{ type: 'power', signal: 'VCC' }] },
{ name: 'SIG', x: 17.5, y: 82.75, number: 2, signals: [analog(0)] },
{ name: 'GND', x: 222.25, y: 59, number: 3, signals: [{ type: 'power', signal: 'GND' }] },
];
private pageToLocalTransformationMatrix: DOMMatrix | null = null;

static get styles() {
return css`
Expand Down Expand Up @@ -176,7 +179,7 @@ export class SlidePotentiometerElement extends LitElement {

private down(): void {
if (!this.isPressed) {
this.updateCaseRect();
this.updateCaseDisplayProperties();
}
this.isPressed = true;
}
Expand All @@ -187,10 +190,10 @@ export class SlidePotentiometerElement extends LitElement {
}
};

private updateCaseRect(): void {
const element = this.shadowRoot?.querySelector('#sliderCase');
private updateCaseDisplayProperties(): void {
const element = this.shadowRoot?.querySelector<SVGRectElement>('#sliderCase');
if (element) {
this.caseRect = element?.getBoundingClientRect();
this.pageToLocalTransformationMatrix = element.getScreenCTM()?.inverse() || null;
}

// Handle zooming in the storybook
Expand All @@ -211,26 +214,31 @@ export class SlidePotentiometerElement extends LitElement {

private mouseMove = (event: MouseEvent) => {
if (this.isPressed) {
this.updateValueFromXCoordinate(event.pageX);
this.updateValueFromXCoordinate(new DOMPointReadOnly(event.pageX, event.pageY));
}
};

private touchMove(event: TouchEvent): void {
if (this.isPressed) {
if (event.targetTouches.length > 0) {
this.updateValueFromXCoordinate(event.targetTouches[0].pageX);
const touchTarget = event.targetTouches[0];
this.updateValueFromXCoordinate(new DOMPointReadOnly(touchTarget.pageX, touchTarget.pageY));
}
}
}

private updateValueFromXCoordinate(xPosition: number): void {
const width = this.caseRect?.width ?? 0;
const pixelPerMM = width / 45;
const travelInPixels = 30 * pixelPerMM;
const pixelPerIncrement = travelInPixels / (this.maxValue - this.minValue);
const caseBorderWidth = pixelPerMM * 7.5;
const tipPositionX = xPosition / this.zoom - (this.caseRect?.left ?? 0) - caseBorderWidth;
this.updateValue(Math.round(tipPositionX / pixelPerIncrement));
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) {
Expand Down