Skip to content

Instantly share code, notes, and snippets.

@TheAlphamerc
Created January 10, 2025 08:14
Show Gist options
  • Select an option

  • Save TheAlphamerc/e7cc9078d732c9c6922d982116cb1828 to your computer and use it in GitHub Desktop.

Select an option

Save TheAlphamerc/e7cc9078d732c9c6922d982116cb1828 to your computer and use it in GitHub Desktop.
Generative art in flutter
import 'package:flutter/material.dart';
import 'dart:math';
import 'dart:ui';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black,
body: Center(
child: AnimatedAbstractSketch(),
),
),
);
}
}
class AnimatedAbstractSketch extends StatefulWidget {
@override
_AnimatedAbstractSketchState createState() => _AnimatedAbstractSketchState();
}
class _AnimatedAbstractSketchState extends State<AnimatedAbstractSketch>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
double t = 0;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 1),
)..repeat();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _controller,
builder: (context, child) {
t += pi / 30;
return CustomPaint(
size: Size(MediaQuery.of(context).size.width / 2,
MediaQuery.of(context).size.height / 2),
painter: AbstractSketchPainter(t),
);
},
);
}
}
class AbstractSketchPainter extends CustomPainter {
final double t;
AbstractSketchPainter(this.t);
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.white.withOpacity(0.18)
..strokeWidth = 1;
for (int i = 20000; i-- > 0;) {
a(canvas, size, i % 100, i / 250, paint);
}
}
void a(Canvas canvas, Size size, double x, double y, Paint paint) {
final k = x / 4 - 12.5;
final e = y / 9 + 9;
final o = sqrt(k * k + e * e) / 9;
final q = x + 99 + tan(1 / k) + o * k * (cos(e * 9) / 2 + cos(y / 9) / 0.7) * sin(o * 4 - t * 2);
final c = o * e / 30 - t / 8;
final px = (q * 0.7 * sin(c) + 200);
final py = (200 + y * cos(c * 4 - o) - q / 2 * cos(c));
canvas.drawPoints(
PointMode.points,
[Offset(px, py)],
paint,
);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment