-
Notifications
You must be signed in to change notification settings - Fork 1
/
plasma.dart
423 lines (375 loc) · 12.8 KB
/
plasma.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import 'dart:math';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:supercharged/supercharged.dart';
import '../../stateless_animation/custom_animation.dart';
import 'factory.dart';
/// Plasma creates an animation with moving particles that generates a
/// smooth liquid effect. It can be configured in many styles.
///
/// You can also use a non-animating variant by setting [speed] to `0.0`
/// and find a good looking position with the [offset].
///
/// Example:
/// ```dart
/// import 'dart:math';
/// import 'package:flutter/material.dart';
/// import 'package:simple_animations/simple_animations.dart';
///
/// void main() => runApp(MaterialApp(home: Page()));
///
/// class Page extends StatelessWidget {
/// @override
/// Widget build(BuildContext context) {
/// return Scaffold(
/// body: Plasma(
/// particles: 10,
/// foregroundColor: Color(0xd61e0000),
/// backgroundColor: Color(0xffee360d),
/// size: 0.87,
/// speed: 5.92,
/// offset: 0.00,
/// blendMode: BlendMode.darken,
/// child: Center(child: Text("Hot!")), // your UI here
/// ),
/// );
/// }
/// }
///
/// ```
class PlasmaRenderer extends StatelessWidget {
/// Type of plasma animation
final PlasmaType type;
/// Method used to render each particle.
///
/// By default renders using [ParticleType.circle].
///
/// See [ParticleType] for details.
final ParticleType particleType;
/// Number of particles to simulate. Has impact on computation demand.
final int particles;
/// Color of the animating particles
final Color color;
/// Describes how the color merges.
final BlendMode blendMode;
/// Size scale of the animating particles. Has slight impact on computation
/// demand.
final double size;
/// Speed scale of the animation. You can set it to `0.0` that disable the
/// animation. Disabled animations doesn't require computational demand at all.
final double speed;
/// If set, the animation will reduce the framerate (fps) to the specified
/// value.
final int? fps;
/// Influences the start position of the particle animation.
final double offset;
/// Rotation of the animation in radians
final double rotation;
/// Blur scale of the particle.
final double blur;
/// Number influencing the rendering of certain Plasma types.
final double variation1;
/// Number influencing the rendering of certain Plasma types.
final double variation2;
/// Number influencing the rendering of certain Plasma types.
final double variation3;
/// Child that's placed inside this [Widget].
final Widget? child;
const PlasmaRenderer({
Key? key,
this.type = PlasmaType.infinity,
this.particleType = ParticleType.circle,
this.particles = 10,
this.color = Colors.white,
this.size = 1.0,
this.speed = 1.0,
this.offset = 0.0,
this.fps,
this.blendMode = BlendMode.srcOver,
this.blur = 1.0,
this.variation1 = 0.0,
this.variation2 = 0.0,
this.variation3 = 0.0,
this.rotation = 0.0,
this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned.fill(
child: Padding(
padding: const EdgeInsets.only(bottom: 0.01),
// workaround for https://github.com/felixblaschke/simple_animations/issues/45
child: ClipRect(
clipBehavior: Clip.hardEdge,
child: Transform.rotate(
angle: rotation,
child: CustomAnimation<double>(
key: Key('plasma_$fps'),
control: speed > 0
? CustomAnimationControl.loop
: CustomAnimationControl.stop,
tween: 0.0.tweenTo(2 * pi),
fps: fps,
duration: speed > 0
? (120000.0 / speed).round().milliseconds
: 1.seconds,
builder: (context, _, value) {
return CustomPaint(
painter: _PlasmaPainter(
particles: particles,
value: value,
color: color,
circleSize: size,
blendMode: blendMode,
offset: offset,
blur: blur,
type: type,
particleType: particleType,
rotation: rotation,
variation1: variation1,
variation2: variation2,
variation3: variation3,
),
);
}),
),
),
),
),
if (child != null)
Positioned.fill(
child: child!,
)
],
);
}
}
class _PlasmaPainter extends CustomPainter {
final int particles;
final PlasmaType type;
final ParticleType particleType;
final double value;
final Color color;
final double circleSize;
final BlendMode blendMode;
final double offset;
final double blur;
final double rotation;
final double variation1;
final double variation2;
final double variation3;
_PlasmaPainter({
required this.type,
required this.particleType,
required this.particles,
required this.value,
required this.color,
required this.circleSize,
required this.blendMode,
required this.offset,
required this.blur,
required this.rotation,
required this.variation1,
required this.variation2,
required this.variation3,
});
@override
void paint(Canvas canvas, Size canvasSize) {
var size = Size(
max(canvasSize.width, canvasSize.height),
max(canvasSize.width, canvasSize.height),
);
var correctionX = (size.width - canvasSize.width) / 2;
var correctionY = (size.height - canvasSize.height) / 2;
var compute = LiPlasmaComputeFactory.create(
particles: particles,
type: type,
circleSize: circleSize,
offset: offset,
canvasSize: size,
value: value,
blur: blur,
variation1: variation1,
variation2: variation2,
variation3: variation3,
);
switch (particleType) {
case ParticleType.circle:
_drawCircleParticles(canvas, compute, correctionX, correctionY);
break;
case ParticleType.atlas:
_drawAtlasParticles(canvas, compute, correctionX, correctionY);
break;
}
}
void _drawCircleParticles(Canvas canvas, LiPlasmaCompute compute,
double correctionX, double correctionY) {
final paint = Paint()
..color = color
..blendMode = blendMode;
for (var n = 0; n < particles; n++) {
var position = compute.position(n);
var particleRadius = compute.radius(n);
if (blur > 0) {
var blurRadius = (blur * particleRadius * 0.4).roundToDouble();
if (blurRadius > 0) {
paint.maskFilter = MaskFilter.blur(BlurStyle.normal, blurRadius);
}
}
canvas.drawCircle(
Offset(
position.dx - correctionX,
position.dy - correctionY,
),
particleRadius,
paint);
}
}
// The resolution of the atlas.
//
// The number 100 was picked empirically. It can be adjusted if found to be
// insufficient, or it can be made customizable. If this number is too big
// it may impact memory usage and/or performance. It should be as small as
// possible while still producing reasonably looking results.
static const double _atlasResolution = 100;
// If not null, indicates that we started generating an atlas.
Future<void>? atlasFuture;
// If not null, the atlas used to render particles.
//
// If null, we fallback to drawCircle.
ui.Image? atlas;
// Produces an image containing one blurred circle. The image is then used to
// render all particles using `Canvas.drawAtlas`, which is very fast.
void _createAtlas() {
var atlasSize = _atlasResolution;
var particleRadius = _atlasResolution / 2;
final paint = Paint()..color = const Color(0xFFFFFFFF);
if (blur > 0) {
var blurRadius = (blur * particleRadius * 0.4).roundToDouble();
// Increase the atlas size to fit the effect of the blur. 2.5x blur on
// each side seems enough.
atlasSize = atlasSize + 5 * blurRadius;
paint.maskFilter = MaskFilter.blur(BlurStyle.normal, blurRadius);
}
final recorder = ui.PictureRecorder();
final canvas = Canvas(recorder, Rect.fromLTRB(0, 0, atlasSize, atlasSize));
canvas.drawCircle(
Offset(atlasSize / 2, atlasSize / 2),
particleRadius,
paint,
);
final picture = recorder.endRecording();
atlasFuture =
picture.toImage(atlasSize.toInt(), atlasSize.toInt()).then((image) {
atlas = image;
});
}
void _drawAtlasParticles(Canvas canvas, LiPlasmaCompute compute,
double correctionX, double correctionY) {
// If the atlas is not available yet, fallback on drawCircle
// See: https://github.com/flutter/flutter/issues/77289
if (atlas == null) {
// Create an atlas if we're not already creating one.
if (atlasFuture == null) {
_createAtlas();
}
_drawCircleParticles(canvas, compute, correctionX, correctionY);
return;
}
final paint = Paint()
..filterQuality = FilterQuality.medium
..blendMode = blendMode;
final rstTransforms = Float32List(4 * particles);
final rects = Float32List(4 * particles);
final colors = Int32List(particles);
final colorValue = color.value;
final atlasSize = atlas!.width.toDouble();
for (var n = 0; n < particles; n++) {
colors[n] = colorValue;
final offset = 4 * n;
rects[offset + 2] = atlasSize;
rects[offset + 3] = atlasSize;
final position = compute.position(n);
final particleRadius = compute.radius(n);
final scale = 2 * particleRadius / _atlasResolution;
final center = atlasSize / 2;
final scos = scale;
const ssin = 0.0;
final tx = (position.dx - correctionX) + -scos * center + ssin * center;
final ty = (position.dy - correctionY) + -ssin * center - scos * center;
rstTransforms[offset] = scos;
rstTransforms[offset + 1] = ssin;
rstTransforms[offset + 2] = tx;
rstTransforms[offset + 3] = ty;
}
canvas.drawRawAtlas(
atlas!,
rstTransforms,
rects,
colors,
BlendMode.dstIn,
null,
paint,
);
}
@override
bool shouldRepaint(covariant _PlasmaPainter oldDelegate) {
// If the blur hasn't changed we can reuse the atlas from the old painter.
if (blur == oldDelegate.blur) {
atlas = oldDelegate.atlas;
atlasFuture = oldDelegate.atlasFuture;
}
if (type != oldDelegate.type ||
particleType != oldDelegate.particleType ||
particles != oldDelegate.particles ||
value != oldDelegate.value ||
color != oldDelegate.color ||
circleSize != oldDelegate.circleSize ||
blendMode != oldDelegate.blendMode ||
offset != oldDelegate.offset ||
blur != oldDelegate.blur ||
rotation != oldDelegate.rotation ||
variation1 != oldDelegate.variation1 ||
variation2 != oldDelegate.variation2 ||
variation3 != oldDelegate.variation3) {
return true;
}
return false;
}
}
abstract class LiPlasmaCompute {
Offset position(int n);
double radius(int n);
}
enum PlasmaType { infinity, bubbles, circle }
/// The shape and rendering method used for drawing particles.
enum ParticleType {
/// Renders particles by calling [Canvas.drawCircle] for each particle.
///
/// This rendering method is visually accurate but computationally more
/// expensive than [atlas].
circle,
/// Renders particles from a prerasterized atlas scaled to the particle size.
///
/// This rendering method is faster than [circle] but it's not visually
/// accurate. At low blur levels, large particle sizes, and certain
/// [BlendMode]s it may look different from [circle]. It is therefore not a
/// drop-in replacement for [circle]. The plasma effect should be
/// intentionally tuned for the atlas mode.
///
/// The atlas image is regenerated every time the value of
/// [PlasmaRenderer.blur] changes. If the blur level is being animated, this
/// method will fallback to [circle] on every frame and therefore not have
/// any performance benefit.
///
/// Choose this method if:
///
/// * [PlasmaRenderer.blendMode] produces the desired effect with atlases.
/// * Blur is high enough or particle size is small enough to hide atlas
/// pixelation, or if pixelation is not a concern.
/// * Blur value does not change during the animation.
atlas,
}