Skip to content

Commit 67f9043

Browse files
committed
feat: add 7segment element
close #2
1 parent 99ad537 commit 67f9043

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/7segment-element.stories.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { withKnobs } from '@storybook/addon-knobs';
2+
import { storiesOf } from '@storybook/polymer';
3+
import { html } from 'lit-html';
4+
import './7segment-element';
5+
import { LitElement, customElement, property } from 'lit-element';
6+
7+
storiesOf('7 Segment', module)
8+
.addDecorator(withKnobs)
9+
.add(
10+
'Red 4.',
11+
() => html`
12+
<wokwi-7segment color="red" values="[0,1,1,0,0,1,1,1]"></wokwi-7segment>
13+
`
14+
)
15+
.add(
16+
'Green 5',
17+
() => html`
18+
<wokwi-7segment color="green" values="[1,0,1,1,0,1,1,0]"></wokwi-7segment>
19+
`
20+
)
21+
.add(
22+
'Blue spinner',
23+
() => html`
24+
<wokwi-blue-spinner></wokwi-blue-spinner>
25+
`
26+
);
27+
28+
@customElement('wokwi-blue-spinner')
29+
export class BlueSpinnerElement extends LitElement {
30+
@property({ type: Array }) values: number[] = [];
31+
32+
private interval: number | null = null;
33+
34+
connectedCallback() {
35+
super.connectedCallback();
36+
let i = 0;
37+
this.interval = setInterval(() => {
38+
this.values = [0, 0, 0, 0, 0, 0, 0, 0];
39+
this.values[i++ % 6] = 1;
40+
}, 100);
41+
}
42+
43+
disconnectedCallback() {
44+
if (this.interval) {
45+
clearInterval(this.interval);
46+
this.interval = null;
47+
}
48+
}
49+
50+
render() {
51+
return html`
52+
<wokwi-7segment color="blue" .values="${this.values}"></wokwi-7segment>
53+
`;
54+
}
55+
}

src/7segment-element.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { css, customElement, html, LitElement, property } from 'lit-element';
2+
3+
@customElement('wokwi-7segment')
4+
export class SevenSegmentElement extends LitElement {
5+
@property() color = 'red';
6+
@property({ type: Array }) values: number[] = [0, 0, 0, 0, 0, 0, 0, 0];
7+
8+
static get styles() {
9+
return css`
10+
polygon {
11+
transform: scale(0.9);
12+
transform-origin: 50% 50%;
13+
transform-box: fill-box;
14+
}
15+
`;
16+
}
17+
18+
render() {
19+
const { color, values } = this;
20+
const fill = (index: number) => (values[index] ? color : '#ddd');
21+
return html`
22+
<svg
23+
width="12mm"
24+
height="18.5mm"
25+
version="1.1"
26+
viewBox="0 0 12 18.5"
27+
xmlns="http://www.w3.org/2000/svg"
28+
>
29+
<g transform="skewX(-8) translate(2, 0)">
30+
<polygon points="2 0 8 0 9 1 8 2 2 2 1 1" fill="${fill(0)}" />
31+
<polygon points="10 2 10 8 9 9 8 8 8 2 9 1" fill="${fill(1)}" />
32+
<polygon points="10 10 10 16 9 17 8 16 8 10 9 9" fill="${fill(2)}" />
33+
<polygon points="8 18 2 18 1 17 2 16 8 16 9 17" fill="${fill(3)}" />
34+
<polygon points="0 16 0 10 1 9 2 10 2 16 1 17" fill="${fill(4)}" />
35+
<polygon points="0 8 0 2 1 1 2 2 2 8 1 9" fill=${fill(5)} />
36+
<polygon points="2 8 8 8 9 9 8 10 2 10 1 9" fill=${fill(6)} />
37+
</g>
38+
<circle cx="11" cy="17" r="1.1" fill="${fill(7)}" />
39+
</svg>
40+
`;
41+
}
42+
}

src/index.ts

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

0 commit comments

Comments
 (0)