@@ -4,23 +4,27 @@ import { GraphicsContext } from "../graphics/context";
44import { Timeline } from "./timeline" ;
55import { MetacityLabel } from "./label" ;
66import { MetacityLayerProps } from "./layer" ;
7-
7+ import { Utils } from "../metacitygl" ;
88
99interface MetacityGLProps {
1010 background ?: number ;
1111 children ?: React . ReactNode | React . ReactNode [ ] ;
1212 target ?: [ number , number , number ] ;
1313 position ?: [ number , number , number ] ;
14+ invertColors ?: boolean ;
1415}
1516
1617export function MetacityGL ( props : MetacityGLProps ) {
1718 const [ context , setContext ] = React . useState < GraphicsContext > ( ) ;
1819 const canvasRef = React . useRef < HTMLCanvasElement > ( null ) ;
1920 const containerRef = React . useRef < HTMLDivElement > ( null ) ;
21+ const LoaderRef = React . useRef < HTMLDivElement > ( null ) ;
2022 const [ hoverId , setHoverId ] = React . useState < number | null > ( 0 ) ;
2123 const [ metadataHover , setMetadataHover ] = React . useState < any > ( 0 ) ;
2224 const [ hoverLocation , setHoverLocation ] = React . useState < { x : number , y : number } | null > ( null ) ;
2325 const children = React . Children . toArray ( props . children ) ;
26+ let layersLoaded = 0 ;
27+ const color = props . background ? Utils . Color . colorHexToStr ( props . background ) : "#000000" ;
2428
2529 const [ enableTimeline , setEnableTimeline ] = React . useState < boolean > ( false ) ;
2630 const [ enableUI , setEnableUI ] = React . useState < boolean > ( false ) ;
@@ -52,9 +56,16 @@ export function MetacityGL(props: MetacityGLProps) {
5256 //TODO ideally calculate near and far to fit
5357 } , 20 ) ;
5458 } ) ;
59+
60+ //Does not work in strict mode
61+ //return () => {
62+ // window.location.reload();
63+ //};
5564 }
5665 } , [ canvasRef , containerRef ] ) ;
5766
67+
68+
5869 const onMove = ( e : React . PointerEvent < HTMLCanvasElement > ) => {
5970 //TODO implement hover
6071 const x = e . clientX ;
@@ -100,13 +111,51 @@ export function MetacityGL(props: MetacityGLProps) {
100111 context . updateSize ( ) ;
101112 } ;
102113
114+ const onLoaded = ( ) => {
115+ layersLoaded += 1 ;
116+ console . log ( "loaded" , layersLoaded ) ;
117+
118+ if ( LoaderRef . current ) {
119+ const container = LoaderRef . current . children [ 0 ]
120+ container . childNodes . forEach ( ( child , index ) => {
121+ if ( index < layersLoaded ) {
122+ ( child as HTMLElement ) . classList . add ( "loaded" ) ;
123+ }
124+ } ) ;
125+ }
126+
127+
128+ if ( layersLoaded === children . length ) {
129+ setTimeout ( ( ) => {
130+ if ( LoaderRef . current ) {
131+ LoaderRef . current . style . opacity = "0" ;
132+
133+ setTimeout ( ( ) => {
134+ if ( LoaderRef . current ) {
135+ LoaderRef . current . style . display = "none" ;
136+ }
137+ } , 200 ) ;
138+ }
139+ } , 500 ) ;
140+ }
141+ } ;
103142
104143 return (
105144 < div className = "MetacityGLContainer" >
106145 < div className = "RenderingAreaContainer" >
107146 < div className = "CanvasContainer" ref = { containerRef } >
147+ < div id = "loader" className = { ( props . invertColors ? " invert" : "" ) } ref = { LoaderRef } style = { {
148+ backgroundColor : color ,
149+ } } >
150+ < div id = "loadingBalls" >
151+ { children . map ( ( _ , index ) =>
152+ < div className = { "loadingBlob" } key = { index } style = { { animationDelay : `${ Math . random ( ) } s` } } />
153+ ) }
154+ </ div >
155+
156+ < div id = "loadingName" > Loading • Metacity Tools</ div > </ div >
108157 < canvas ref = { canvasRef } onPointerMove = { onMove } > </ canvas >
109- ( { hoverId != null &&
158+ { hoverId != null &&
110159 < div
111160 className = "hoverDialog"
112161 style = { {
@@ -118,7 +167,7 @@ export function MetacityGL(props: MetacityGLProps) {
118167 >
119168 < pre > { JSON . stringify ( metadataHover , null , 2 ) } </ pre >
120169 </ div >
121- } )
170+ }
122171 </ div >
123172 { enableTimeline && < Timeline context = { context } /> }
124173 </ div >
@@ -127,7 +176,7 @@ export function MetacityGL(props: MetacityGLProps) {
127176 } } >
128177 { children . map ( ( child , index ) => {
129178 if ( React . isValidElement < MetacityLayerProps > ( child ) ) {
130- return React . cloneElement ( child , { context : context } ) ;
179+ return React . cloneElement ( child , { context : context , onLoaded } ) ;
131180 }
132181 } ) }
133182 < MetacityLabel context = { context } />
0 commit comments