1
- import { css , customElement , html , LitElement , property , svg } from 'lit-element' ;
1
+ import { css , customElement , html , LitElement , property } from 'lit-element' ;
2
2
import { analog , ElementPin } from './pin' ;
3
3
import { clamp } from './utils/clamp' ;
4
+ import { mmToPix } from './utils/units' ;
4
5
5
6
@customElement ( 'wokwi-slide-potentiometer' )
6
7
export class SlidePotentiometerElement extends LitElement {
8
+ @property ( ) travelLength = 30 ;
7
9
@property ( ) value = 0 ;
8
10
@property ( ) min = 0 ;
9
11
@property ( ) max = 100 ;
10
12
@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
+
16
28
private isPressed = false ;
17
29
private zoom = 1 ;
18
30
private pageToLocalTransformationMatrix : DOMMatrix | null = null ;
@@ -34,12 +46,12 @@ export class SlidePotentiometerElement extends LitElement {
34
46
}
35
47
36
48
render ( ) {
37
- const { value, min : minValue , max : maxValue } = this ;
38
- const tipTravelInMM = 30 ;
49
+ const { value, min : minValue , max : maxValue , travelLength } = this ;
39
50
// 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
+
43
55
return html `
44
56
< input
45
57
tabindex ="0 "
@@ -55,10 +67,10 @@ export class SlidePotentiometerElement extends LitElement {
55
67
class ="hide-input "
56
68
/>
57
69
< svg
58
- width ="55mm "
70
+ width ="${ travelLength + 25 } mm "
59
71
height ="29mm "
60
72
version ="1.1 "
61
- viewBox ="0 0 55 29 "
73
+ viewBox ="0 0 ${ travelLength + 25 } 29 "
62
74
xmlns ="http://www.w3.org/2000/svg "
63
75
xmlns:xlink ="http://www.w3.org/1999/xlink "
64
76
>
@@ -89,7 +101,7 @@ export class SlidePotentiometerElement extends LitElement {
89
101
gradientUnits ="userSpaceOnUse "
90
102
>
91
103
< stop stop-color ="#d2d2d2 " offset ="0 " />
92
- < stop stop-color ="#2a2a2a " offset ="1 " />
104
+ < stop stop-color ="#7a7a7a " offset ="1 " />
93
105
</ radialGradient >
94
106
< g id ="screw ">
95
107
< circle cx ="0 " cy ="0 " r ="1 " fill ="#858585 " stroke ="#000 " stroke-width =".05 " />
@@ -99,7 +111,7 @@ export class SlidePotentiometerElement extends LitElement {
99
111
<!-- pins -->
100
112
< g fill ="#ccc ">
101
113
< 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 " />
103
115
< rect x ="0 " y ="17.25 " width ="5 " height ="0.75 " />
104
116
</ g >
105
117
< g transform ="translate(5 5) ">
@@ -108,26 +120,34 @@ export class SlidePotentiometerElement extends LitElement {
108
120
id ="sliderCase "
109
121
x ="0 "
110
122
y ="5 "
111
- width ="45 "
123
+ width ="${ travelLength + 15 } "
112
124
height ="9 "
113
125
rx =".2 "
114
126
ry =".2 "
115
127
fill ="url(#bodyGradient) "
116
128
fill-rule ="evenodd "
117
129
/>
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
+ />
119
139
<!-- Screw Left -->
120
140
< g transform ="translate(1.625 9.5) rotate(45) ">
121
141
< use href ="#screw " />
122
142
</ g >
123
143
<!-- Screw Right -->
124
- < g transform ="translate(43 .375 9.5) rotate(45) ">
144
+ < g transform ="translate(${ travelLength + 13 .375} 9.5) rotate(45) ">
125
145
< use href ="#screw " />
126
146
</ g >
127
147
<!-- Tip -->
128
148
< g
129
149
id ="tip "
130
- transform ="translate(${ tipOffSetX } 0) "
150
+ transform ="translate(${ tipOffsetX } 0) "
131
151
@mousedown =${ this . down }
132
152
@touchstart =${ this . down }
133
153
@touchmove=${ this . touchMove }
@@ -234,7 +254,7 @@ export class SlidePotentiometerElement extends LitElement {
234
254
localPosition = localPosition . matrixTransform ( this . pageToLocalTransformationMatrix ) ;
235
255
const caseBorderWidth = 7.5 ;
236
256
const tipPositionXinMM = localPosition . x - caseBorderWidth ;
237
- const mmPerIncrement = 30 / ( this . max - this . min ) ;
257
+ const mmPerIncrement = this . travelLength / ( this . max - this . min ) ;
238
258
this . updateValue ( Math . round ( tipPositionXinMM / mmPerIncrement ) ) ;
239
259
}
240
260
}
0 commit comments