@@ -77,6 +77,7 @@ export class FastImageSequence {
77
77
private container : HTMLElement ;
78
78
private resizeObserver : ResizeObserver ;
79
79
private mutationObserver : MutationObserver ;
80
+ private inViewportObserver : IntersectionObserver ;
80
81
private clearCanvas : boolean = true ;
81
82
private speed : number = 0 ;
82
83
private prevFrame : number = 0 ;
@@ -87,6 +88,7 @@ export class FastImageSequence {
87
88
private initialized : boolean = false ;
88
89
private posterImage : HTMLImageElement | undefined ;
89
90
private timeFrameVisible : number = 0 ;
91
+ private inViewport : boolean = false ;
90
92
91
93
/**
92
94
* Creates an instance of FastImageSequence.
@@ -138,6 +140,13 @@ export class FastImageSequence {
138
140
} ) ;
139
141
this . mutationObserver . observe ( container , { childList : true } ) ;
140
142
143
+ this . inViewportObserver = new IntersectionObserver ( ( entries ) => {
144
+ entries . forEach ( entry => {
145
+ this . inViewport = entry . isIntersecting ;
146
+ } ) ;
147
+ } ) ;
148
+ this . inViewportObserver . observe ( this . canvas ) ;
149
+
141
150
// init all frames
142
151
this . frames = Array . from ( { length : this . options . frames } , ( _ , index ) => new Frame ( index ) ) ;
143
152
this . log = this . options . showDebugInfo ? console . log : ( ) => {
@@ -311,6 +320,7 @@ export class FastImageSequence {
311
320
312
321
this . resizeObserver . disconnect ( ) ;
313
322
this . mutationObserver . disconnect ( ) ;
323
+ this . inViewportObserver . disconnect ( ) ;
314
324
315
325
this . container . removeChild ( this . canvas ) ;
316
326
if ( this . logElement ) {
@@ -390,13 +400,9 @@ export class FastImageSequence {
390
400
this . frame += this . speed * dt ;
391
401
this . frame = this . wrapFrame ( this . frame ) ;
392
402
393
- const index = this . index ;
394
-
395
- // check if canvas is in viewport
396
- const rect = this . canvas . getBoundingClientRect ( ) ;
397
- const inViewport = rect . top < window . innerHeight && rect . bottom > 0 ;
398
403
399
- if ( inViewport ) {
404
+ if ( this . inViewport ) {
405
+ const index = this . index ;
400
406
// find the best matching loaded frame, based on current index and direction
401
407
// first set some sort of priority
402
408
this . frames . forEach ( ( frame ) => {
0 commit comments