@@ -87,6 +87,7 @@ export class ImageOverlayPlugin {
8787 this . processQueue = null ;
8888 this . _onUpdateAfter = null ;
8989 this . _onTileDownloadStart = null ;
90+ this . _onTileVisibilityChange = null ;
9091 this . _virtualChildResetId = 0 ;
9192 this . _bytesUsed = new WeakMap ( ) ;
9293
@@ -222,8 +223,20 @@ export class ImageOverlayPlugin {
222223
223224 } ;
224225
226+ this . _onTileVisibilityChange = ( { tile, visible } ) => {
227+
228+ this . overlayInfo . forEach ( ( { tileInfo } , overlay ) => {
229+
230+ const info = tileInfo . get ( tile ) ;
231+ overlay . setRegionVisible ( info . range , visible ) ;
232+
233+ } ) ;
234+
235+ } ;
236+
225237 tiles . addEventListener ( 'update-after' , this . _onUpdateAfter ) ;
226238 tiles . addEventListener ( 'tile-download-start' , this . _onTileDownloadStart ) ;
239+ tiles . addEventListener ( 'tile-visibility-change' , this . _onTileVisibilityChange ) ;
227240
228241 this . overlays . forEach ( overlay => {
229242
@@ -403,6 +416,8 @@ export class ImageOverlayPlugin {
403416 } ) ;
404417
405418 tiles . removeEventListener ( 'update-after' , this . _onUpdateAfter ) ;
419+ tiles . removeEventListener ( 'tile-download-start' , this . _onTileDownloadStart ) ;
420+ tiles . removeEventListener ( 'tile-visibility-change' , this . _onTileVisibilityChange ) ;
406421
407422 this . resetVirtualChildren ( true ) ;
408423
@@ -1110,6 +1125,12 @@ export class ImageOverlayPlugin {
11101125
11111126 }
11121127
1128+ if ( tile . traversal . visible ) {
1129+
1130+ overlay . setRegionVisible ( info . range , true ) ;
1131+
1132+ }
1133+
11131134 // if the image projection is outside the 0, 1 uvw range or there are no textures to draw in
11141135 // the tiled image set the don't allocate a texture for it.
11151136 let target = null ;
@@ -1306,6 +1327,7 @@ export class ImageOverlay {
13061327 this . _whenReady = null ;
13071328 this . isReady = false ;
13081329 this . isInitialized = false ;
1330+ this . _visibleRegionCounts = new Map ( ) ;
13091331
13101332 }
13111333
@@ -1383,6 +1405,32 @@ export class ImageOverlay {
13831405
13841406 }
13851407
1408+ setRegionVisible ( range , visible ) {
1409+
1410+ const { _visibleRegionCounts } = this ;
1411+ const key = range . join ( '_' ) ;
1412+ let entry = _visibleRegionCounts . get ( key ) ;
1413+ if ( ! entry ) {
1414+
1415+ entry = { range : [ ...range ] , count : 0 } ;
1416+ _visibleRegionCounts . set ( key , entry ) ;
1417+
1418+ }
1419+
1420+ entry . count += visible ? 1 : - 1 ;
1421+
1422+ if ( entry . count < 0 ) {
1423+
1424+ throw new Error ( ) ;
1425+
1426+ } else if ( entry . count === 0 ) {
1427+
1428+ _visibleRegionCounts . delete ( key ) ;
1429+
1430+ }
1431+
1432+ }
1433+
13861434}
13871435
13881436/**
@@ -1682,6 +1730,10 @@ export class GeoJSONOverlay extends ImageOverlay {
16821730 super ( options ) ;
16831731 this . imageSource = new GeoJSONImageSource ( options ) ;
16841732
1733+ this . _redrawQueue = new PriorityQueue ( ) ;
1734+ this . _redrawQueue . maxJobs = 4 ;
1735+ this . _redrawQueue . priorityCallback = ( ) => 0 ;
1736+
16851737 }
16861738
16871739 _init ( ) {
@@ -1727,9 +1779,52 @@ export class GeoJSONOverlay extends ImageOverlay {
17271779
17281780 }
17291781
1782+ setRegionVisible ( range , visible ) {
1783+
1784+ super . setRegionVisible ( range , visible ) ;
1785+
1786+ if ( visible ) {
1787+
1788+ const { _redrawQueue } = this ;
1789+ const key = range . join ( '_' ) ;
1790+ if ( _redrawQueue . has ( key ) ) {
1791+
1792+ _redrawQueue . flush ( key ) ;
1793+
1794+ }
1795+
1796+ }
1797+
1798+ }
1799+
17301800 redraw ( ) {
17311801
1732- this . imageSource . redraw ( ) ;
1802+ const {
1803+ imageSource,
1804+ _redrawQueue,
1805+ _visibleRegionCounts,
1806+ } = this ;
1807+
1808+ for ( const { range } of _visibleRegionCounts . values ( ) ) {
1809+
1810+ imageSource . redraw ( ...range ) ;
1811+
1812+ }
1813+
1814+ imageSource . forEachItem ( ( _ , args ) => {
1815+
1816+ const key = args . join ( '_' ) ;
1817+ if ( ! _visibleRegionCounts . has ( key ) && ! _redrawQueue . has ( key ) ) {
1818+
1819+ _redrawQueue . add ( key , ( ) => {
1820+
1821+ imageSource . redraw ( ...args ) ;
1822+
1823+ } ) ;
1824+
1825+ }
1826+
1827+ } ) ;
17331828
17341829 }
17351830
0 commit comments