Skip to content

Instantly share code, notes, and snippets.

@av
Last active September 29, 2019 11:32
Show Gist options
  • Save av/73cc529a89d9b57f11f88bdfdb7f73e6 to your computer and use it in GitHub Desktop.
Save av/73cc529a89d9b57f11f88bdfdb7f73e6 to your computer and use it in GitHub Desktop.
Flutter: fixed aspect ratio grid
/// Main area of interest, this function will
/// return color for each particular color on our [ui.Image]
int generatePixel(int x, int y, Size size) {
var tiles = 5.0;
/// Compute uv in same way as in previous examples.
var uv = Vector2(x / size.width, y / size.height);
/// New twist, [Size.aspectRatio] is essentially [Size.width]
/// divided by [Size.height].
/// uv.y /= 2 would make our tiles 2 times taller
/// uv.y *= 2 would make tiles 2 times shorter
/// uv.y /= size.aspectRatio would make them as many
/// times taller or shorter as aspect ratio
/// between [Size.width] and [Size.height]
uv.y /= size.aspectRatio;
return toColorInt((frac2(uv * tiles)).xyx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment