Skip to content

Commit 92b9654

Browse files
committed
feat(slide-potentiometer): make travel length configurable #94
close #94
1 parent 54e6b45 commit 92b9654

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

src/slide-potentiometer-element.stories.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,31 @@ import './slide-potentiometer-element';
55
export default {
66
title: 'Slide Potentiometer',
77
component: 'wokwi-slide-potentiometer',
8+
argTypes: {
9+
travelLength: { control: { type: 'range', min: 15, max: 100 } },
10+
},
11+
args: {
12+
travelLength: 30,
13+
},
814
};
915

10-
const Template = ({ degrees = 0 }) => html` <div
11-
style="transform: rotate(${degrees}deg) translate(50%, 50%); width:400px; height: 400px;"
12-
>
13-
<wokwi-slide-potentiometer @input=${action('input')} />
14-
</div>`;
16+
const Template = ({ travelLength, degrees = 0 }) => html`
17+
<div style="transform: rotate(${degrees}deg) translate(50%, 50%); width: 500px; height: 400px;">
18+
<wokwi-slide-potentiometer .travelLength=${travelLength} @input=${action('input')} />
19+
</div>
20+
`;
1521

1622
export const Default = Template.bind({});
1723
Default.args = {};
1824

1925
export const Rotated = Template.bind({});
2026
Rotated.args = { ...Default.args, degrees: 90 };
27+
28+
export const Short = Template.bind({});
29+
Short.args = { travelLength: 15 };
30+
31+
export const Long = Template.bind({});
32+
Long.args = { travelLength: 60 };
33+
34+
export const ExtraLong = Template.bind({});
35+
ExtraLong.args = { travelLength: 100 };

src/slide-potentiometer-element.ts

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
import { css, customElement, html, LitElement, property, svg } from 'lit-element';
1+
import { css, customElement, html, LitElement, property } from 'lit-element';
22
import { analog, ElementPin } from './pin';
33
import { clamp } from './utils/clamp';
4+
import { mmToPix } from './utils/units';
45

56
@customElement('wokwi-slide-potentiometer')
67
export class SlidePotentiometerElement extends LitElement {
8+
@property() travelLength = 30;
79
@property() value = 0;
810
@property() min = 0;
911
@property() max = 100;
1012
@property() step = 2;
11-
readonly pinInfo: ElementPin[] = [
12-
{ name: 'VCC', x: 1, y: 43, number: 1, signals: [{ type: 'power', signal: 'VCC' }] },
13-
{ name: 'SIG', x: 1, y: 66.5, number: 2, signals: [analog(0)] },
14-
{ name: 'GND', x: 207, y: 43, number: 3, signals: [{ type: 'power', signal: 'GND' }] },
15-
];
13+
14+
get pinInfo(): ElementPin[] {
15+
return [
16+
{ name: 'VCC', x: 1, y: 43, number: 1, signals: [{ type: 'power', signal: 'VCC' }] },
17+
{ name: 'SIG', x: 1, y: 66.5, number: 2, signals: [analog(0)] },
18+
{
19+
name: 'GND',
20+
x: 93.6 + this.travelLength * mmToPix,
21+
y: 43,
22+
number: 3,
23+
signals: [{ type: 'power', signal: 'GND' }],
24+
},
25+
];
26+
}
27+
1628
private isPressed = false;
1729
private zoom = 1;
1830
private pageToLocalTransformationMatrix: DOMMatrix | null = null;
@@ -34,12 +46,12 @@ export class SlidePotentiometerElement extends LitElement {
3446
}
3547

3648
render() {
37-
const { value, min: minValue, max: maxValue } = this;
38-
const tipTravelInMM = 30;
49+
const { value, min: minValue, max: maxValue, travelLength } = this;
3950
// Tip is centered by default
40-
const tipBaseOffsetX = -(tipTravelInMM / 2);
41-
const tipMovementX = (value / (maxValue - minValue)) * tipTravelInMM;
42-
const tipOffSetX = tipMovementX + tipBaseOffsetX;
51+
const tipBaseOffsetX = -15;
52+
const tipMovementX = (value / (maxValue - minValue)) * travelLength;
53+
const tipOffsetX = tipMovementX + tipBaseOffsetX;
54+
4355
return html`
4456
<input
4557
tabindex="0"
@@ -55,10 +67,10 @@ export class SlidePotentiometerElement extends LitElement {
5567
class="hide-input"
5668
/>
5769
<svg
58-
width="55mm"
70+
width="${travelLength + 25}mm"
5971
height="29mm"
6072
version="1.1"
61-
viewBox="0 0 55 29"
73+
viewBox="0 0 ${travelLength + 25} 29"
6274
xmlns="http://www.w3.org/2000/svg"
6375
xmlns:xlink="http://www.w3.org/1999/xlink"
6476
>
@@ -89,7 +101,7 @@ export class SlidePotentiometerElement extends LitElement {
89101
gradientUnits="userSpaceOnUse"
90102
>
91103
<stop stop-color="#d2d2d2" offset="0" />
92-
<stop stop-color="#2a2a2a" offset="1" />
104+
<stop stop-color="#7a7a7a" offset="1" />
93105
</radialGradient>
94106
<g id="screw">
95107
<circle cx="0" cy="0" r="1" fill="#858585" stroke="#000" stroke-width=".05" />
@@ -99,7 +111,7 @@ export class SlidePotentiometerElement extends LitElement {
99111
<!-- pins -->
100112
<g fill="#ccc">
101113
<rect x="0" y="11" width="5" height="0.75" />
102-
<rect x="50" y="11" width="5" height="0.75" />
114+
<rect x="${travelLength + 20}" y="11" width="5" height="0.75" />
103115
<rect x="0" y="17.25" width="5" height="0.75" />
104116
</g>
105117
<g transform="translate(5 5)">
@@ -108,26 +120,34 @@ export class SlidePotentiometerElement extends LitElement {
108120
id="sliderCase"
109121
x="0"
110122
y="5"
111-
width="45"
123+
width="${travelLength + 15}"
112124
height="9"
113125
rx=".2"
114126
ry=".2"
115127
fill="url(#bodyGradient)"
116128
fill-rule="evenodd"
117129
/>
118-
<rect x="3.25" y="8" width="38.5" height="3" rx=".1" ry=".1" fill="#3f1e1e" />
130+
<rect
131+
x="3.25"
132+
y="8"
133+
width="${travelLength + 8.5}"
134+
height="3"
135+
rx=".1"
136+
ry=".1"
137+
fill="#3f1e1e"
138+
/>
119139
<!-- Screw Left -->
120140
<g transform="translate(1.625 9.5) rotate(45)">
121141
<use href="#screw" />
122142
</g>
123143
<!-- Screw Right -->
124-
<g transform="translate(43.375 9.5) rotate(45)">
144+
<g transform="translate(${travelLength + 13.375} 9.5) rotate(45)">
125145
<use href="#screw" />
126146
</g>
127147
<!-- Tip -->
128148
<g
129149
id="tip"
130-
transform="translate(${tipOffSetX} 0)"
150+
transform="translate(${tipOffsetX} 0)"
131151
@mousedown=${this.down}
132152
@touchstart=${this.down}
133153
@touchmove=${this.touchMove}
@@ -234,7 +254,7 @@ export class SlidePotentiometerElement extends LitElement {
234254
localPosition = localPosition.matrixTransform(this.pageToLocalTransformationMatrix);
235255
const caseBorderWidth = 7.5;
236256
const tipPositionXinMM = localPosition.x - caseBorderWidth;
237-
const mmPerIncrement = 30 / (this.max - this.min);
257+
const mmPerIncrement = this.travelLength / (this.max - this.min);
238258
this.updateValue(Math.round(tipPositionXinMM / mmPerIncrement));
239259
}
240260
}

0 commit comments

Comments
 (0)