Skip to content

Commit 553362a

Browse files
committed
feat: add resistor element
1 parent d33d849 commit 553362a

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { LEDElement } from './led-element';
22
export { PushbuttonElement } from './pushbutton-element';
3+
export { ResistorElement } from './resistor-element';

src/resistor-element.stories.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { storiesOf } from '@storybook/polymer';
2+
import { html } from 'lit-html';
3+
import { withKnobs, number } from '@storybook/addon-knobs';
4+
5+
import './resistor-element';
6+
7+
storiesOf('resistor', module)
8+
.addDecorator(withKnobs())
9+
.add(
10+
'1Ω',
11+
() =>
12+
html`
13+
<wokwi-resistor value="${number('R', 1)}"></wokwi-resistor>
14+
`
15+
)
16+
.add(
17+
'470Ω',
18+
() =>
19+
html`
20+
<wokwi-resistor value="${number('R', 470)}"></wokwi-resistor>
21+
`
22+
);

src/resistor-element.ts

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { customElement, html, LitElement, property } from 'lit-element';
2+
3+
const bandColors: { [key: number]: string } = {
4+
[-2]: 'silver',
5+
[-1]: '#c4a000', // gold
6+
0: 'black',
7+
1: 'brown',
8+
2: 'red',
9+
3: 'orange',
10+
4: 'yellow',
11+
5: 'green',
12+
6: 'blue',
13+
7: 'violet',
14+
8: 'gray',
15+
9: 'white'
16+
};
17+
18+
@customElement('wokwi-resistor')
19+
export class ResistorElement extends LitElement {
20+
@property() value = '1000';
21+
22+
private breakValue(value: number) {
23+
const exponent =
24+
value >= 1e10
25+
? 9
26+
: value >= 1e9
27+
? 8
28+
: value >= 1e8
29+
? 7
30+
: value >= 1e7
31+
? 6
32+
: value >= 1e6
33+
? 5
34+
: value >= 1e5
35+
? 4
36+
: value >= 1e4
37+
? 3
38+
: value >= 1e3
39+
? 2
40+
: value >= 1e2
41+
? 1
42+
: value >= 1e1
43+
? 0
44+
: value >= 1
45+
? -1
46+
: -2;
47+
const base = Math.round(value / 10 ** exponent);
48+
if (value === 0) {
49+
return [0, 0];
50+
}
51+
if (exponent < 0 && base % 10 === 0) {
52+
return [base / 10, exponent + 1];
53+
}
54+
return [Math.round(base % 100), exponent];
55+
}
56+
57+
render() {
58+
const { value } = this;
59+
const numValue = parseFloat(value);
60+
const [base, exponent] = this.breakValue(numValue);
61+
const band1Color = bandColors[Math.floor(base / 10)];
62+
const band2Color = bandColors[base % 10];
63+
const band3Color = bandColors[exponent];
64+
return html`
65+
<svg
66+
width="15.645mm"
67+
height="3mm"
68+
version="1.1"
69+
viewBox="0 0 15.645 3"
70+
xmlns="http://www.w3.org/2000/svg"
71+
xmlns:xlink="http://www.w3.org/1999/xlink"
72+
>
73+
<defs>
74+
<linearGradient
75+
id="a"
76+
x2="0"
77+
y1="22.332"
78+
y2="38.348"
79+
gradientTransform="matrix(.14479 0 0 .14479 -23.155 -4.0573)"
80+
gradientUnits="userSpaceOnUse"
81+
spreadMethod="reflect"
82+
>
83+
<stop stop-color="#323232" offset="0" />
84+
<stop stop-color="#fff" stop-opacity=".42268" offset="1" />
85+
</linearGradient>
86+
</defs>
87+
<rect y="1.1759" width="15.645" height=".63826" fill="#eaeaea" />
88+
<g stroke-width=".14479">
89+
<path
90+
d="m4.6918 0c-1.0586 0-1.9185 0.67468-1.9185 1.5022 0 0.82756 0.85995 1.4978 1.9185 1.4978 0.4241 0 0.81356-0.11167 1.1312-0.29411h4.0949c0.31802 0.18313 0.71075 0.29411 1.1357 0.29411 1.0586 0 1.9185-0.67015 1.9185-1.4978 0-0.8276-0.85995-1.5022-1.9185-1.5022-0.42499 0-0.81773 0.11098-1.1357 0.29411h-4.0949c-0.31765-0.18244-0.7071-0.29411-1.1312-0.29411z"
91+
fill="#d5b597"
92+
/>
93+
<path
94+
d="m4.6918 0c-1.0586 0-1.9185 0.67468-1.9185 1.5022 0 0.82756 0.85995 1.4978 1.9185 1.4978 0.4241 0 0.81356-0.11167 1.1312-0.29411h4.0949c0.31802 0.18313 0.71075 0.29411 1.1357 0.29411 1.0586 0 1.9185-0.67015 1.9185-1.4978 0-0.8276-0.85995-1.5022-1.9185-1.5022-0.42499 0-0.81773 0.11098-1.1357 0.29411h-4.0949c-0.31765-0.18244-0.7071-0.29411-1.1312-0.29411z"
95+
fill="url(#a)"
96+
opacity=".44886"
97+
/>
98+
<path
99+
d="m4.6917 0c-0.10922 0-0.21558 0.00884-0.31985 0.022624v2.955c0.10426 0.013705 0.21063 0.02234 0.31985 0.02234 0.15603 0 0.3074-0.015363 0.4522-0.043551v-2.9129c-0.1448-0.028193-0.29617-0.043551-0.4522-0.043552z"
100+
fill="${band1Color}"
101+
/>
102+
<path d="m6.4482 0.29411v2.4117h0.77205v-2.4117z" fill="${band2Color}" />
103+
<path d="m8.5245 0.29411v2.4117h0.77205v-2.4117z" fill="${band3Color}" />
104+
<path
105+
d="m11.054 0c-0.15608 0-0.30749 0.015253-0.45277 0.043268v2.9134c0.14527 0.028012 0.29669 0.043268 0.45277 0.043268 0.10912 0 0.21539-0.00867 0.31957-0.02234v-2.955c-0.10418-0.013767-0.21044-0.022624-0.31957-0.022624z"
106+
fill="#c4a000"
107+
/>
108+
</g>
109+
</svg>
110+
`;
111+
}
112+
}

0 commit comments

Comments
 (0)