Skip to content

Instantly share code, notes, and snippets.

View av's full-sized avatar
💻
🌚

Ivan Charapanau av

💻
🌚
View GitHub Profile
@av
av / main.dart
Created September 29, 2019 11:07
Flutter: uvGrid with certain tiles count
/// 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) {
/// Compute unified vector, values of its components
/// will be between 0 and 1
var uv = Vector2(x / size.width, y / size.height);
var xTiles = 5.0;
var yTiles = 10.0;
/// We can now use this [Vector2] as our unit coordinates
@av
av / main.dart
Last active September 29, 2019 11:32
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
@av
av / main.dart
Created September 29, 2019 11:35
Flutter: checkerboard 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;
var uv = Vector2(x / size.width, y / size.height);
uv.y /= size.aspectRatio;
var gridUv = frac2(uv * tiles);
/// We have a square with top left and bottom right quarters
@av
av / main.dart
Created September 29, 2019 11:47
Flutter: truchet noise
/// Don't forget to import dart:math!
var rnd = Random();
/// 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 = 3.0;
var uv = Vector2(x / size.width, y / size.height);
uv.y /= size.aspectRatio;
@av
av / main.dart
Last active September 29, 2019 12:46
Flutter: pseudo-random truchet tiling
/// Generates pseudo random double
/// based on input [Vector2], try to
/// change numbers within to see how it changes
/// the pattern generated. Try some small integers
/// to see more repetition in how tiles are placed.
double random2(Vector2 vec) {
// return vec.x % 2 + vec.y % 2;
// return frac(vec.dot(vec.xy) * 0.28);
// return frac(vec.x * vec.y * .99);
@av
av / main.dart
Created September 29, 2019 13:41
Flutter: perlin snake
/// Generates a [ui.Image] with certain pixel data
Future<ui.Image> generateImage(Size size) async {
int width = size.width.ceil();
int height = size.height.ceil();
/// PerlinNoise generator, there're lots of parameters
/// to tweak and lots of effects possible to produce
var noise = PerlinNoise(
octaves: 1, frequency: 0.03, fractalType: FractalType.RigidMulti);
@av
av / main.dart
Created September 29, 2019 13:57
Flutter: smoothStep perlin snake
pixels[index] = toColorInt(
Vector3.all(
// [smoothStep] will return value between 0..1
// which is growing smoothly when luminance is between
// 0.92 and 0.99
smoothStep(0.92, .99, luminance),
),
);
@av
av / main.dart
Created October 10, 2019 14:06
Flutter: tough_bikes first iteration
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(BikeApp());
class BikeApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
@av
av / main.dart
Created October 11, 2019 11:41
Flutter: tough_bikes BikeButton with CustomPaint
class BikeButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
height: 50.0,
child: CustomPaint(
painter: BikeButtonPainter(),
child: Center(
child: Text('Buy Tough Bike'.toUpperCase()),
),
@av
av / main.dart
Created October 11, 2019 11:47
Flutter: tough_bikes BikeButtonPainter
class BikeButtonPainter extends CustomPainter {
final fillPaint = Paint()..color = Colors.grey;
@override
void paint(Canvas canvas, Size size) {
// This [Rect] will fill the whole canvas we got
var fillRect = Rect.fromPoints(Offset.zero, size.bottomRight(Offset.zero));
canvas.drawRect(
fillRect,