initializeParticles method

void initializeParticles(
  1. Path path,
  2. SpoilerConfig config
)

Sets up the spoiler region, the bounding path, and initializes all particles. If config.isEnabled is true, the spoiler is turned on, and we start the particle animation.

path is the shape describing where particles should exist. config includes fade, density, maxParticleSize, etc.

Implementation

void initializeParticles(Path path, SpoilerConfig config) {
  // Ensure maxParticleSize is valid
  assert(config.maxParticleSize >= 1, 'maxParticleSize must be >= 1');
  _config = config;
  particles.clear();

  if (_spoilerPath != path) {
    _spoilerPath.reset();
    if (config.maskConfig != null) {
      final newPath = Path.combine(
        config.maskConfig!.maskOperation,
        path,
        config.maskConfig!.maskPath.shift(
          config.maskConfig!.offset,
        ),
      );
      _spoilerPath.addPath(newPath, Offset.zero);
    } else {
      _spoilerPath.addPath(path, Offset.zero);
    }
    _spoilerBounds = _spoilerPath.getBounds();
  }

  _initFadeIfNeeded();

  if (_circleImage.color != _config.particleColor ||
      _circleImage.dimension != _config.maxParticleSize) {
    _circleImage = CircleImageFactory.create(
      diameter: _config.maxParticleSize,
      color: _config.particleColor,
    );
  }

  final subPaths = _spoilerPath.subPaths;

  for (final path in subPaths) {
    final rect = path.getBounds();
    final particleCount =
        (rect.width * rect.height) * _config.particleDensity;
    for (int i = 0; i < particleCount; i++) {
      particles.add(_createRandomParticlePath(path));
    }
  }

  _isEnabled = config.isEnabled;

  _reallocAtlasBuffers();

  _startParticleAnimationIfNeeded();
}