initialize method

void initialize(
  1. Map<String, dynamic> config
)

Initialize the DotLottie player with config from Dart

Implementation

void initialize(Map<String, dynamic> config) {
  if (isDisposed) {
    return;
  }

  if (isInitialized) {
    return;
  }

  try {
    // Get the DotLottie constructor from window
    final windowObj = web.window as JSObject;
    final dotLottieConstructor = windowObj['DotLottie'.toJS];

    if (dotLottieConstructor == null) {
      return;
    }

    // Create config object
    final playerConfig = JSObject();

    // Set canvas
    playerConfig['canvas'.toJS] = canvasElement as JSAny;

    // Set source based on sourceType
    final sourceType = config['sourceType'] as String?;
    final source = config['source'] as String?;

    if (sourceType != null && source != null) {
      switch (sourceType) {
        case 'url':
          playerConfig['src'.toJS] = source.toJS;
          break;
        case 'json':
          playerConfig['data'.toJS] = source.toJS;
          break;
        case 'asset':
          playerConfig['src'.toJS] = 'assets/$source'.toJS;
          break;
      }
    }

    final autoplay = config['autoplay'] as bool? ?? true;
    playerConfig['autoplay'.toJS] = autoplay.toJS;

    final loop = config['loop'] as bool? ?? true;
    playerConfig['loop'.toJS] = loop.toJS;

    final loopCount = config['loopCount'] as num? ?? 0;
    playerConfig['loopCount'.toJS] = loopCount.toJS;

    final mode = config['mode'] as String? ?? 'forward';
    playerConfig['mode'.toJS] = mode.toJS;

    final speed = (config['speed'] as num? ?? 1.0).toDouble();
    playerConfig['speed'.toJS] = speed.toJS;

    final useFrameInterpolation =
        config['useFrameInterpolation'] as bool? ?? false;
    playerConfig['useFrameInterpolation'.toJS] = useFrameInterpolation.toJS;

    playerConfig['segment'.toJS] =
        ((config['segment'] as List?)?.map((e) => (e as num).toJS).toList() ??
                [])
            .toJS;

    final backgroundColor = config['backgroundColor'] as String?;
    if (backgroundColor != null) {
      element.style.backgroundColor = backgroundColor;
    }

    final themeId = config['themeId'] as String? ?? '';
    playerConfig['themeId'.toJS] = themeId.toJS;

    final stateMachineId = config['stateMachineId'] as String? ?? '';
    playerConfig['stateMachineId'.toJS] = stateMachineId.toJS;

    final animationId = config['animationId'] as String? ?? '';
    playerConfig['animationId'.toJS] = animationId.toJS;

    final width = config['width'] as int?;
    final height = config['height'] as int?;
    if (width != null) {
      canvasElement.width = width;
    }
    if (height != null) {
      canvasElement.height = height;
    }

    // Create the DotLottie player instance
    dotLottiePlayer = _callConstructor(
      dotLottieConstructor as JSFunction,
      playerConfig,
    );

    isInitialized = true;

    _setupEventListeners();
    _setupStateMachineListeners();
  } catch (e) {
    print('Error initilizing dotLottie: $e');
  }
}