1- import { css , customElement , html , LitElement , property , svg } from 'lit-element' ;
1+ import { css , customElement , html , LitElement , property } from 'lit-element' ;
22import { analog , ElementPin } from './pin' ;
33import { clamp } from './utils/clamp' ;
4+ import { mmToPix } from './utils/units' ;
45
56@customElement ( 'wokwi-slide-potentiometer' )
67export 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