Skip to content

Commit 876a1a9

Browse files
authored
feat: add buzzer element (#25)
1 parent 2ef6875 commit 876a1a9

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

src/buzzer-element.stories.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { storiesOf } from '@storybook/web-components';
2+
import { withKnobs, boolean } from '@storybook/addon-knobs';
3+
import { html } from 'lit-html';
4+
import './buzzer-element';
5+
6+
storiesOf('Buzzer', module)
7+
.addParameters({ component: 'wokwi-buzzer' })
8+
.addDecorator(withKnobs)
9+
.add(
10+
'Buzzer',
11+
() =>
12+
html`<div style="height: 20px"></div>
13+
<wokwi-buzzer .hasSignal=${boolean('hasSignal', false)}></wokwi-buzzer>`
14+
);

src/buzzer-element.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { css, customElement, html, LitElement, property } from 'lit-element';
2+
3+
/**
4+
* Renders a piezo electric buzzer.
5+
*/
6+
@customElement('wokwi-buzzer')
7+
export class BuzzerElement extends LitElement {
8+
/**
9+
* Boolean representing if an electric signal is coming in the buzzer
10+
* If true a music note will be displayed on top of the buzzer
11+
*/
12+
@property() hasSignal = false;
13+
14+
static get styles() {
15+
return css`
16+
:host {
17+
display: inline-block;
18+
}
19+
20+
.buzzer-container {
21+
display: flex;
22+
flex-direction: column;
23+
width: 75px;
24+
}
25+
26+
.music-note {
27+
position: relative;
28+
left: 40px;
29+
animation-duration: 1.5s;
30+
animation-name: animate-note;
31+
animation-iteration-count: infinite;
32+
animation-timing-function: linear;
33+
transform: scale(1.5);
34+
fill: blue;
35+
offset-path: path(
36+
'm0 0c-0.9-0.92-1.8-1.8-2.4-2.8-0.56-0.92-0.78-1.8-0.58-2.8 0.2-0.92 0.82-1.8 1.6-2.8 0.81-0.92 1.8-1.8 2.6-2.8 0.81-0.92 1.4-1.8 1.6-2.8 0.2-0.92-0.02-1.8-0.58-2.8-0.56-0.92-1.5-1.8-2.4-2.8'
37+
);
38+
offset-rotate: 0deg;
39+
}
40+
41+
@keyframes animate-note {
42+
0% {
43+
offset-distance: 0%;
44+
opacity: 0;
45+
}
46+
10% {
47+
offset-distance: 10%;
48+
opacity: 1;
49+
}
50+
75% {
51+
offset-distance: 75%;
52+
opacity: 1;
53+
}
54+
100% {
55+
offset-distance: 100%;
56+
opacity: 0;
57+
}
58+
}
59+
`;
60+
}
61+
62+
render() {
63+
const buzzerOn = this.hasSignal;
64+
return html`
65+
<div class="buzzer-container">
66+
<svg
67+
class="music-note"
68+
style="visibility: ${buzzerOn ? '' : 'hidden'}"
69+
xmlns="http://www.w3.org/2000/svg"
70+
width="8"
71+
height="8"
72+
viewBox="0 0 8 8"
73+
>
74+
<path
75+
d="M8 0c-5 0-6 1-6 1v4.09c-.15-.05-.33-.09-.5-.09-.83 0-1.5.67-1.5 1.5s.67 1.5 1.5 1.5 1.5-.67 1.5-1.5v-3.97c.73-.23 1.99-.44 4-.5v2.06c-.15-.05-.33-.09-.5-.09-.83 0-1.5.67-1.5 1.5s.67 1.5 1.5 1.5 1.5-.67 1.5-1.5v-5.5z"
76+
/>
77+
</svg>
78+
79+
<svg
80+
width="17mm"
81+
height="20mm"
82+
version="1.1"
83+
viewBox="0 0 17 20"
84+
xmlns="http://www.w3.org/2000/svg"
85+
>
86+
<path d="m8 16.5v3.5" fill="none" stroke="#000" stroke-width=".5" />
87+
<path d="m9 16.5v3.5" fill="#f00" stroke="#f00" stroke-width=".5" />
88+
<g stroke="#000">
89+
<g>
90+
<ellipse cx="8.5" cy="8.5" rx="8.15" ry="8.15" fill="#1a1a1a" stroke-width=".7" />
91+
<circle
92+
cx="8.5"
93+
cy="8.5"
94+
r="6.3472"
95+
fill="none"
96+
stroke-width=".3"
97+
style="paint-order:normal"
98+
/>
99+
<circle
100+
cx="8.5"
101+
cy="8.5"
102+
r="4.3488"
103+
fill="none"
104+
stroke-width=".3"
105+
style="paint-order:normal"
106+
/>
107+
</g>
108+
<circle cx="8.5" cy="8.5" r="1.3744" fill="#ccc" stroke-width=".25" />
109+
</g>
110+
</svg>
111+
</div>
112+
`;
113+
}
114+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export { MembraneKeypadElement } from './membrane-keypad-element';
1111
export { PotentiometerElement } from './potentiometer-element';
1212
export { NeopixelMatrixElement } from './neopixel-matrix-element';
1313
export { SSD1306Element } from './ssd1306-element';
14+
export { BuzzerElement } from './buzzer-element';

0 commit comments

Comments
 (0)