@@ -47,6 +47,7 @@ export enum PlayerState {
47
47
// Define player events
48
48
export enum PlayerEvent {
49
49
Load = 'load' ,
50
+ InstanceSaved = 'instanceSaved' ,
50
51
Error = 'error' ,
51
52
Ready = 'ready' ,
52
53
Play = 'play' ,
@@ -314,13 +315,13 @@ export class Player extends React.Component<IPlayerProps, IPlayerState> {
314
315
const fetchResult = await fetch ( animationData as string ) . catch ( ( ) => {
315
316
this . setState ( { playerState : PlayerState . Error } ) ;
316
317
this . triggerEvent ( PlayerEvent . Error ) ;
317
- throw new Error ( 'Error @LottieFiles/lottie-react: Animation data could not be fetched.' ) ;
318
+ throw new Error ( '@LottieFiles/lottie-react: Animation data could not be fetched.' ) ;
318
319
} ) ;
319
320
320
321
animationData = await fetchResult . json ( ) . catch ( ( ) => {
321
322
this . setState ( { playerState : PlayerState . Error } ) ;
322
323
this . triggerEvent ( PlayerEvent . Error ) ;
323
- throw new Error ( 'Error @LottieFiles/lottie-react: Animation data could not be fetched.' ) ;
324
+ throw new Error ( '@LottieFiles/lottie-react: Animation data could not be fetched.' ) ;
324
325
} ) ;
325
326
}
326
327
@@ -344,84 +345,85 @@ export class Player extends React.Component<IPlayerProps, IPlayerState> {
344
345
this . setState ( { animationData } ) ;
345
346
346
347
this . setState ( { instance : newInstance } , ( ) => {
348
+ this . triggerEvent ( PlayerEvent . InstanceSaved ) ;
349
+
347
350
if ( typeof lottieRef === 'function' ) {
348
351
lottieRef ( newInstance ) ;
349
352
}
353
+ if ( autoplay ) {
354
+ this . play ( ) ;
355
+ }
356
+ } ) ;
350
357
351
- // Handle new frame event
352
- newInstance . addEventListener ( 'enterFrame' , ( ) => {
353
- this . triggerEvent ( PlayerEvent . Frame ) ;
358
+ // Handle new frame event
359
+ newInstance . addEventListener ( 'enterFrame' , ( ) => {
360
+ this . triggerEvent ( PlayerEvent . Frame ) ;
354
361
355
- this . setState ( {
356
- seeker : Math . floor ( ( newInstance as any ) . currentFrame ) ,
357
- } ) ;
362
+ this . setState ( {
363
+ seeker : Math . floor ( ( newInstance as any ) . currentFrame ) ,
358
364
} ) ;
365
+ } ) ;
359
366
360
- // Handle lottie-web ready event
361
- newInstance . addEventListener ( 'DOMLoaded' , ( ) => {
362
- this . triggerEvent ( PlayerEvent . Load ) ;
363
-
364
- if ( autoplay ) {
365
- this . play ( ) ;
366
- }
367
- } ) ;
367
+ // Handle lottie-web ready event
368
+ newInstance . addEventListener ( 'DOMLoaded' , ( ) => {
369
+ this . triggerEvent ( PlayerEvent . Load ) ;
370
+ } ) ;
368
371
369
- // Handle animation data load complete
370
- newInstance . addEventListener ( 'data_ready' , ( ) => {
371
- this . triggerEvent ( PlayerEvent . Ready ) ;
372
- } ) ;
372
+ // Handle animation data load complete
373
+ newInstance . addEventListener ( 'data_ready' , ( ) => {
374
+ this . triggerEvent ( PlayerEvent . Ready ) ;
375
+ } ) ;
373
376
374
- // Set error state when animation load fail event triggers
375
- newInstance . addEventListener ( 'data_failed' , ( ) => {
376
- this . setState ( { playerState : PlayerState . Error } ) ;
377
- this . triggerEvent ( PlayerEvent . Error ) ;
378
- } ) ;
377
+ // Set error state when animation load fail event triggers
378
+ newInstance . addEventListener ( 'data_failed' , ( ) => {
379
+ this . setState ( { playerState : PlayerState . Error } ) ;
380
+ this . triggerEvent ( PlayerEvent . Error ) ;
381
+ } ) ;
379
382
380
- // Handle new loop event
381
- newInstance . addEventListener ( 'loopComplete' , ( ) => {
382
- this . triggerEvent ( PlayerEvent . Loop ) ;
383
- } ) ;
383
+ // Handle new loop event
384
+ newInstance . addEventListener ( 'loopComplete' , ( ) => {
385
+ this . triggerEvent ( PlayerEvent . Loop ) ;
386
+ } ) ;
384
387
385
- // Set state to paused if loop is off and anim has completed
386
- newInstance . addEventListener ( 'complete' , ( ) => {
387
- this . triggerEvent ( PlayerEvent . Complete ) ;
388
- this . setState ( { playerState : PlayerState . Paused } ) ;
388
+ // Set state to paused if loop is off and anim has completed
389
+ newInstance . addEventListener ( 'complete' , ( ) => {
390
+ this . triggerEvent ( PlayerEvent . Complete ) ;
391
+ this . setState ( { playerState : PlayerState . Paused } ) ;
389
392
390
- if ( ! this . props . keepLastFrame || this . props . loop ) {
391
- this . setSeeker ( 0 ) ;
392
- }
393
- } ) ;
393
+ if ( ! this . props . keepLastFrame || this . props . loop ) {
394
+ this . setSeeker ( 0 ) ;
395
+ }
396
+ } ) ;
394
397
395
- // Set handlers to auto play animation on hover if enabled
396
- if ( this . container ) {
397
- this . container . addEventListener ( 'mouseenter' , ( ) => {
398
- if ( hover && this . state . playerState !== PlayerState . Playing ) {
399
- if ( this . props . keepLastFrame ) {
400
- this . stop ( ) ;
401
- }
402
- this . play ( ) ;
403
- }
404
- } ) ;
405
- this . container . addEventListener ( 'mouseleave' , ( ) => {
406
- if ( hover && this . state . playerState === PlayerState . Playing ) {
398
+ // Set handlers to auto play animation on hover if enabled
399
+ if ( this . container ) {
400
+ this . container . addEventListener ( 'mouseenter' , ( ) => {
401
+ if ( hover && this . state . playerState !== PlayerState . Playing ) {
402
+ if ( this . props . keepLastFrame ) {
407
403
this . stop ( ) ;
408
404
}
409
- } ) ;
410
- }
405
+ this . play ( ) ;
406
+ }
407
+ } ) ;
408
+ this . container . addEventListener ( 'mouseleave' , ( ) => {
409
+ if ( hover && this . state . playerState === PlayerState . Playing ) {
410
+ this . stop ( ) ;
411
+ }
412
+ } ) ;
413
+ }
411
414
412
- // Set initial playback speed and direction
413
- if ( speed ) {
414
- this . setPlayerSpeed ( speed ) ;
415
- }
415
+ // Set initial playback speed and direction
416
+ if ( speed ) {
417
+ this . setPlayerSpeed ( speed ) ;
418
+ }
416
419
417
- if ( direction ) {
418
- this . setPlayerDirection ( direction ) ;
419
- }
420
+ if ( direction ) {
421
+ this . setPlayerDirection ( direction ) ;
422
+ }
420
423
421
- if ( background ) {
422
- this . setState ( { background } ) ;
423
- }
424
- } ) ;
424
+ if ( background ) {
425
+ this . setState ( { background } ) ;
426
+ }
425
427
} catch ( e ) {
426
428
this . setState ( { playerState : PlayerState . Error } ) ;
427
429
this . triggerEvent ( PlayerEvent . Error ) ;
0 commit comments