Created
May 18, 2023 01:36
-
-
Save jonahwilliams/37763f78a9c6c07fa79d26eecca9b992 to your computer and use it in GitHub Desktop.
crashy path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'dart:math' as math; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(Example()); | |
} | |
class Example extends StatefulWidget { | |
const Example({super.key}); | |
@override | |
State<Example> createState() => _ExampleState(); | |
} | |
class _ExampleState extends State<Example> | |
with SingleTickerProviderStateMixin { | |
late AnimationController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController(vsync: this, duration: Duration(hours: 1)); | |
_controller.addListener(() { | |
// setState(() { | |
// }); | |
}); | |
_controller.forward(from: 0); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: CustomPaint( | |
painter: HeartPainter(), | |
size: Size(400, 400), | |
)); | |
} | |
} | |
class HeartPainter extends CustomPainter { | |
@override | |
void paint(Canvas canvas, Size size) { | |
var random = math.Random(123); | |
// Calculate the center of the canvas. | |
Offset center = size.center(Offset.zero); | |
// Calculate the radius of the heart. | |
double radius = math.min(size.width, size.height) / 2; | |
// Create a path for the heart. | |
Path path = Path(); | |
for (var i = 0; i < 12; i++) { | |
var x1 = center.dx + (500 * (random.nextDouble() - 0.5)); | |
var y1 = center.dy + (500 * (random.nextDouble() - 0.5)); | |
var x2 = center.dx + (500 * (random.nextDouble() - 0.5)); | |
var y2 = center.dy + (500 * (random.nextDouble() - 0.5)); | |
path.cubicTo( | |
x1, | |
y1, | |
x2, | |
y2, | |
center.dx, | |
center.dy, | |
); | |
} | |
path.close(); | |
// Paint the heart. | |
Paint paint = Paint() | |
..style = PaintingStyle.stroke | |
..color = Colors.red | |
..strokeWidth = 5.0 | |
..strokeCap = StrokeCap.round; | |
canvas.drawPath(path, 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