JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
import 'dart:async';
import 'dart:math';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
void main() => runApp(
MaterialApp(
home: Container(color: Colors.grey[900], child: App()),
debugShowMaterialGrid: false,
),
);
final size = ui.window.physicalSize / ui.window.devicePixelRatio;
const frequency = Duration(milliseconds: 50);
class Circle {
final Offset offset;
final double radius;
final Color color;
const Circle({this.offset, this.color = Colors.white, this.radius = 10});
}
class App extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
Timer timer;
final points = <Offset>[Offset.zero];
final circles = <Circle>[];
Offset force = Offset(1, 1);
HSLColor hslColor = HSLColor.fromColor(Colors.red);
final StreamController<List<Circle>> _circleStreamer =
StreamController<List<Circle>>.broadcast();
Stream<List<Circle>> get _circle$ => _circleStreamer.stream;
Offset get randomPoint => size.topLeft(Offset.zero) * Random().nextDouble();
Color get color => hslColor.toColor();
@override
void initState() {
timer = Timer.periodic(
frequency,
(t) {
if (circles.isEmpty)
_circleStreamer.add(
circles..add(Circle(offset: randomPoint, radius: 10, color: color)),
);
int count = 0;
while (count < 19) {
final dx = circles.last.offset.dx;
final dy = circles.last.offset.dy;
if ((dx + force.dx > size.width) || (dx + force.dx < 0))
force = Offset(-force.dx, force.dy);
if ((dy + force.dy > size.height) || (dy + force.dy < 0))
force = Offset(force.dx, -force.dy);
final newPoint = circles.last.offset + force;
final newCircle = Circle(
offset: newPoint,
radius: 80 + (30 * sin(circles.length / 49)),
color: hslColor.toColor(),
);
hslColor = hslColor.withHue((hslColor.hue + 0.1) % 360);
_circleStreamer.add(circles..add(newCircle));
count++;
}
},
);
super.initState();
}
@override
void dispose() {
timer.cancel();
_circleStreamer.close();
super.dispose();
}
@override
Widget build(BuildContext context) => Stack(
children: [
StreamBuilder<List<Circle>>(
initialData: [],
stream: _circle$.map((event) => event.length > 100
? event.getRange(event.length - 100, event.length).toList()
: event),
builder: (context, snapshot) {
final circles = snapshot.data;
return CustomPaint(
size: size,
painter: CirclePainter(circles: circles),
);
},
),
],
);
}
class CirclePainter extends CustomPainter {
List<Circle> circles;
CirclePainter({this.circles});
@override
void paint(Canvas canvas, Size size) {
for (int i = 0; i < circles.length - 1; i++) {
final c = circles[i];
final paint = Paint()
..color = c.color
..shader = ui.Gradient.radial(
c.offset - Offset(0, c.radius),
c.radius,
/*c.offset + Offset(0, c.radius / 8),*/
[
Color(0x053ffffff),
Colors.transparent,
],
);
final light = Paint()
..shader = ui.Gradient.linear(
c.offset,
c.offset + Offset(0, c.radius),
[
c.color.withOpacity(0.85),
Colors.black,
],
);
canvas.drawCircle(c.offset, c.radius, light);
canvas.drawCircle(c.offset, c.radius, paint);
}
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
Also see: Tab Triggers