resize method

void resize(
  1. Vector2 size
)

Implementation

void resize(Vector2 size) {
  double scale(LayerFill fill) {
    return switch (fill) {
      LayerFill.height => parallaxRenderer.image.height / size.y,
      LayerFill.width => parallaxRenderer.image.width / size.x,
      _ => _scale,
    };
  }

  _scale = scale(parallaxRenderer.fill);

  // The image size so that it fulfills the LayerFill parameter
  _imageSize = parallaxRenderer.image.size / _scale;

  // Number of images that can fit on the canvas plus one
  // to have something to scroll to without leaving canvas empty
  final count = Vector2.all(1) + (size.clone()..divide(_imageSize));

  // Percentage of the image size that will overflow
  final overflow = ((_imageSize.clone()..multiply(count)) - size)
    ..divide(_imageSize);

  // Align image to correct side of the screen
  final alignment = parallaxRenderer.alignment;

  final marginX = alignment.x * overflow.x / 2 + overflow.x / 2;
  final marginY = alignment.y * overflow.y / 2 + overflow.y / 2;

  _scroll.setValues(marginX, marginY);

  // Size of the area to paint the images on
  final paintSize = count..multiply(_imageSize);
  _paintArea = paintSize.toRect();
}