Skip to content

Instantly share code, notes, and snippets.

@rydmike
Created November 20, 2020 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rydmike/46720c2363cd9d37f2eec8e4eb7ebe0b to your computer and use it in GitHub Desktop.
Save rydmike/46720c2363cd9d37f2eec8e4eb7ebe0b to your computer and use it in GitHub Desktop.
Playground to demonstrate Plasma Issue in Simple animation and Liquid Studio
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:simple_animations/simple_animations.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Issue Discovery',
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: Text('Plasma issue demo')),
body: Plasma(
particles: 10,
foregroundColor: Color(0x6642a5f5),
backgroundColor: Color(0xff0d47a1),
size: 1.00,
speed: 6.00,
offset: 0.00,
blendMode: BlendMode.colorDodge,
child: Container(), // your UI here
),
),
);
}
}
class Plasma extends StatelessWidget {
final int particles;
final Color foregroundColor;
final Color backgroundColor;
final BlendMode blendMode;
final double size;
final double speed;
final int fps;
final double offset;
final Widget child;
Plasma({
Key key,
this.particles = 10,
this.foregroundColor = Colors.white,
this.backgroundColor = Colors.black,
this.size = 1.0,
this.speed = 1.0,
this.offset = 0.0,
this.fps,
this.blendMode = BlendMode.srcOver,
this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(bottom: 0.01),
child: ClipRect(
clipBehavior: Clip.hardEdge,
child: CustomAnimation<double>(
control: speed > 0
? CustomAnimationControl.LOOP
: CustomAnimationControl.STOP,
tween: Tween(begin: 0.0, end: 2 * pi),
child: child,
fps: fps,
duration: speed > 0
? Duration(milliseconds: (120000.0 / speed).round())
: Duration(seconds: 1),
builder: (context, animatedChild, value) {
return Stack(
children: [
Positioned.fill(
child: CustomPaint(
foregroundPainter: _PlasmaPainter(
particles: particles,
value: value,
color: foregroundColor,
circleSize: size,
blendMode: blendMode,
offset: offset,
),
child: Container(
color: backgroundColor,
),
),
),
if (animatedChild != null)
Positioned.fill(
child: animatedChild,
)
],
);
}),
),
);
}
}
class _PlasmaPainter extends CustomPainter {
final int particles;
final double value;
final Color color;
final double circleSize;
final BlendMode blendMode;
final double offset;
_PlasmaPainter(
{this.particles,
this.value,
this.color,
this.circleSize,
this.blendMode,
this.offset});
@override
void paint(Canvas canvas, Size size) {
var compute = InternalPlasmaCompute(
circleSize: circleSize, offset: offset, canvasSize: size, value: value);
var paint = Paint()
..color = color
..maskFilter = MaskFilter.blur(BlurStyle.normal, compute.blurRadius())
..blendMode = blendMode;
for (var n = 0; n < particles; n++) {
canvas.drawCircle(compute.position(n), compute.radius(), paint);
// canvas.drawCircle(Offset(compute.position(n).dx.roundToDouble(), compute.position(n).dy.roundToDouble()), compute.radius(), paint);
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
class InternalPlasmaCompute {
final Size canvasSize;
final double circleSize;
final double offset;
final double value;
double _radius;
InternalPlasmaCompute(
{this.canvasSize, this.circleSize, this.offset, this.value}) {
_radius = (circleSize * (canvasSize.width + canvasSize.height) / 2 / 3)
.roundToDouble();
}
Offset position(int particleNumber) {
var rand = sin(particleNumber).abs();
var randomValue = (value + rand * 2 * pi) % (2 * pi);
var x = sin(-rand + randomValue + offset) * canvasSize.width / 2;
var y = sin(rand + -2 * randomValue + offset) * canvasSize.height / 2;
return Offset(canvasSize.width / 2 + x, canvasSize.height / 2 + y);
}
double radius() => _radius;
double blurRadius() => (_radius * 1.2).roundToDouble();
}
@rydmike
Copy link
Author

rydmike commented Nov 20, 2020

This gist is related to this issue:

felixblaschke/simple_animations#45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment