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';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
///
/// FIX shapes
///
void main() {
runApp(MaterialApp(
home: Scaffold(body: Board()),
debugShowCheckedModeBanner: false,
));
}
final size = window.physicalSize / window.devicePixelRatio;
const maxPoints = 99;
const force = Offset(0, 1);
const acceleration = 1.1;
const fillColor = Colors.lime;
const fillRed = Color(0xffff0000);
final strokeColor = Colors.lime[700];
const segmentMaxWidth = 60.0;
const segmentMinWidth = 2.0;
const segmentMaxLength = 500.0;
extension on Color {
HSLColor get hsl => HSLColor.fromColor(this);
double get lightness => hsl.lightness;
Color withLightness(double value) =>
hsl.withLightness(hsl.lightness * .98).toColor();
}
class Point {
final Offset offset;
final Offset force;
final bool active;
static const zero = Point(Offset.zero, Offset.zero, false);
const Point(this.offset, this.force, [this.active = true]);
Point update() => active
? Point(offset + force, force * acceleration, offset.dy < size.height)
: zero;
Offset up(double value) => offset + Offset(0, -value);
Offset down(double value) => offset + Offset(0, value);
Point freeze() => Point(offset, Offset.zero, false);
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Point &&
runtimeType == other.runtimeType &&
offset == other.offset &&
force == other.force &&
active == other.active;
@override
int get hashCode => offset.hashCode ^ force.hashCode ^ active.hashCode;
}
class Segment {
final Point point1;
final Point point2;
final Color strokeColor;
final Color fillColor;
Offset get offset1 => point1.offset;
Offset get offset2 => point2.offset;
final Segment previous;
const Segment(
this.point1,
this.point2, {
@required this.previous,
this.strokeColor,
this.fillColor,
});
bool get active => point1.active && point2.active;
List<Offset> get corners {
final previousWidth =
previous != null ? previous.segmentWidth : segmentWidth;
final width = segmentWidth;
return [
point1.up(previousWidth),
point2.up(width),
point2.down(width),
point1.down(previousWidth),
];
}
double get segmentWidth => max(
segmentMinWidth,
segmentMaxWidth -
(Rect.fromPoints(point1.offset, point2.offset).longestSide /
segmentMaxLength) *
(segmentMaxWidth - segmentMinWidth),
);
Segment update() {
final newFillColor = fillColor.lightness > 0
? fillColor.withLightness(min(1, fillColor.lightness * .98))
: fillColor;
return Segment(
point1.update(),
point2.update(),
previous: previous,
strokeColor: strokeColor,
fillColor: newFillColor,
);
}
Segment freeze() => Segment(
point1.freeze(),
point2.freeze(),
previous: previous,
strokeColor: strokeColor,
fillColor: fillColor,
);
}
class Board extends StatefulWidget {
@override
_BoardState createState() => _BoardState();
}
class _BoardState extends State<Board> with SingleTickerProviderStateMixin {
List<Segment> _segments = [];
StreamController<List<Segment>> _streamer;
Stream<List<Segment>> get _segment$ => _streamer.stream;
final List<List<Segment>> _freezedLines = [];
StreamController<List<List<Segment>>> _freezedStreamer;
Stream<List<List<Segment>>> get freezedShape$ => _freezedStreamer.stream;
@override
void initState() {
_streamer = StreamController<List<Segment>>()..add(<Segment>[]);
_freezedStreamer = StreamController<List<List<Segment>>> /*.broadcast*/ ()
..add([]);
Timer.periodic(Duration(seconds: 2), (timer) {
final freezables = _segments
.where((segment) => segment.active)
.map((segment) => segment.freeze())
.toList();
if (freezables.isEmpty) return;
_freezedLines.add([...freezables]);
_freezedStreamer.add(_freezedLines);
});
AnimationController.unbounded(vsync: this, duration: Duration(seconds: 1))
..repeat()
..addListener(_updateSegments);
super.initState();
}
@override
Widget build(BuildContext context) => MouseRegion(
onHover: (details) => _addSegment(details.position),
child: Stack(
children: [
StreamBuilder<List<List<Segment>>>(
stream: freezedShape$,
builder: (context, snapshot) => CustomPaint(
size: size,
painter: BackgroundPainter(snapshot.data ?? []),
),
),
RepaintBoundary(
child: StreamBuilder<List<Segment>>(
initialData: <Segment>[],
stream: _segment$,
builder: (_, stream) => CustomPaint(
size: size, painter: ForegroundPainter(stream.data)),
),
),
],
),
);
void _addSegment(Offset offset) {
_segments
..add(
Segment(
_segments.isEmpty ? Point(offset, force) : _segments.last.point2,
Point(offset, force),
previous: _segments.isNotEmpty ? _segments.last : null,
strokeColor: strokeColor,
fillColor: fillColor,
),
);
}
void _updateSegments() {
_segments = _segments
.where((element) => element.active)
.map((element) => element.update())
.toList();
_streamer.add(_segments);
}
}
class BackgroundPainter extends CustomPainter {
final List<List<Segment>> lines;
BackgroundPainter(this.lines);
@override
void paint(Canvas canvas, Size size) {
for (final segments in lines) {
for (final segment in segments) drawSegment(canvas, segment);
}
}
@override
bool shouldRepaint(BackgroundPainter oldDelegate) => true;
}
class ForegroundPainter extends CustomPainter {
static final fill = Paint()..color = fillColor;
static final stroke = Paint()
..color = Colors.grey
..style = PaintingStyle.stroke;
final List<Segment> segments;
const ForegroundPainter(this.segments);
@override
void paint(Canvas canvas, Size size) {
if (segments.isEmpty) return;
for (final segment in segments.where((segment) => segment.active))
drawSegment(canvas, segment);
}
@override
bool shouldRepaint(ForegroundPainter oldDelegate) =>
segments.isNotEmpty && !listEquals(segments, oldDelegate.segments);
}
void drawSegment(Canvas canvas, Segment segment) {
final path = Path()
..moveTo(segment.corners[0].dx, segment.corners[0].dy)
..lineTo(segment.corners[1].dx, segment.corners[1].dy)
..lineTo(segment.corners[2].dx, segment.corners[2].dy)
..lineTo(segment.corners[3].dx, segment.corners[3].dy)
..close();
canvas.drawPath(path, Paint()..color = segment.fillColor);
canvas.drawPath(
path,
Paint()
..color = segment.strokeColor
..style = PaintingStyle.stroke,
);
}
Also see: Tab Triggers