1
- import { customElement , html , LitElement , property , queryAll , svg } from 'lit-element' ;
1
+ import { customElement , html , LitElement , property , svg } from 'lit-element' ;
2
2
import { ElementPin } from './pin' ;
3
3
import { RGB } from './types/rgb' ;
4
4
@@ -29,7 +29,7 @@ export class LEDRingElement extends LitElement {
29
29
*/
30
30
@property ( ) animation = false ;
31
31
32
- @ queryAll ( '.pixel' ) pixelElements : SVGCircleElement [ ] = [ ] ;
32
+ private pixelElements : SVGCircleElement [ ] | null = null ;
33
33
34
34
private animationFrame : number | null = null ;
35
35
@@ -58,8 +58,22 @@ export class LEDRingElement extends LitElement {
58
58
] ;
59
59
}
60
60
61
+ private getPixelElements ( ) {
62
+ if ( ! this . shadowRoot ) {
63
+ return null ;
64
+ }
65
+ if ( ! this . pixelElements ) {
66
+ this . pixelElements = Array . from ( this . shadowRoot . querySelectorAll ( 'rect.pixel' ) ) ;
67
+ }
68
+ return this . pixelElements ;
69
+ }
70
+
61
71
setPixel ( pixel : number , { r, g, b } : RGB ) {
62
- const { pixelElements } = this ;
72
+ const pixelElements = this . getPixelElements ( ) ;
73
+ if ( ! pixelElements ) {
74
+ return ;
75
+ }
76
+
63
77
if ( pixel < 0 || pixel >= pixelElements . length ) {
64
78
return ;
65
79
}
@@ -70,7 +84,8 @@ export class LEDRingElement extends LitElement {
70
84
* Resets all the pixels to off state (r=0, g=0, b=0).
71
85
*/
72
86
reset ( ) {
73
- for ( const element of this . pixelElements ) {
87
+ const pixelElements = this . getPixelElements ( ) ;
88
+ for ( const element of pixelElements ?? [ ] ) {
74
89
element . style . fill = '' ;
75
90
}
76
91
}
@@ -118,6 +133,8 @@ export class LEDRingElement extends LitElement {
118
133
transform ="rotate(${ angle } ${ radius } ${ radius } ) "/> `
119
134
) ;
120
135
}
136
+ this . pixelElements = null ; // Invalidate element cache
137
+
121
138
return html `
122
139
< svg
123
140
width ="${ width } mm "
0 commit comments