Skip to content

Commit

Permalink
Minor corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
pgiacomo69 committed Dec 13, 2021
1 parent 941cd52 commit 5b2bc56
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 87 deletions.
15 changes: 8 additions & 7 deletions lib/snowfall/snowfall_rendering.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ class SnowfallRendering extends StatefulWidget {
final int startTimeSimulationTicks;

const SnowfallRendering(
{
required this.builder,
required this.onTick,
this.startTime = Duration.zero,
this.startTimeSimulationTicks = 20, Key? key}) : super(key: key);
{required this.builder,
required this.onTick,
this.startTime = Duration.zero,
this.startTimeSimulationTicks = 20,
Key? key})
: super(key: key);

@override
_SnowfallRenderingState createState() => _SnowfallRenderingState();
Expand Down Expand Up @@ -62,8 +63,8 @@ class _SnowfallRenderingState extends State<SnowfallRendering>
for (var i in Iterable<int>.generate(widget.startTimeSimulationTicks + 1)) {
final simulatedTime = Duration(
milliseconds: (widget.startTime.inMilliseconds *
i /
widget.startTimeSimulationTicks)
i /
widget.startTimeSimulationTicks)
.round());
widget.onTick(simulatedTime);
}
Expand Down
35 changes: 17 additions & 18 deletions lib/snowfall/snowfall_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@ import 'package:flutter/material.dart';
import 'package:snowfall/snowfall/snowflakes.dart';

class SnowfallWidget extends StatelessWidget {

final Widget child;
final Color color;
final int numberOfSnowflakes;
final int alpha;
const SnowfallWidget(
{
Key? key,
required this.child,
this.numberOfSnowflakes=30,
this.color=Colors.white,
this.alpha=180
}) : super(key: key);


{Key? key,
required this.child,
this.numberOfSnowflakes = 30,
this.color = Colors.white,
this.alpha = 180})
: super(key: key);

@override
Widget build(BuildContext context) => Stack(
children: <Widget>[
Positioned.fill(child: Snowflakes(numberOfSnowflakes: numberOfSnowflakes, color:color,alpha:alpha)),
Positioned.fill(child: child),

],
);
}
Widget build(BuildContext context) => Stack(
children: <Widget>[
Positioned.fill(
child: Snowflakes(
numberOfSnowflakes: numberOfSnowflakes,
color: color,
alpha: alpha)),
Positioned.fill(child: child),
],
);
}
84 changes: 42 additions & 42 deletions lib/snowfall/snowflake_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import 'package:flutter/material.dart';
import 'package:simple_animations/simple_animations.dart';
import 'package:snowfall/snowfall/utils/point.dart';

enum AniProps { X,Y }

enum AniProps { X, Y }

class AnimationProgress {
final Duration duration;
Expand All @@ -19,17 +18,17 @@ class AnimationProgress {
/// Queries the current progress value based on the specified [startTime] and
/// [duration] as a value between `0.0` and `1.0`. It will automatically
/// clamp values this interval to fit in.
double progress(Duration time) => math.max(0.0,
math.min((time - startTime).inMilliseconds / duration.inMilliseconds, 1.0));
double progress(Duration time) => math.max(
0.0,
math.min(
(time - startTime).inMilliseconds / duration.inMilliseconds, 1.0));
}


class SnowflakeModel {

static Map<int, Path> cachedFlakes = {};
static Map<int, Path> cachedFlakes = {};

Animatable? tween;
double size=0.0;
double size = 0.0;
AnimationProgress? animationProgress;
math.Random random;
Path? _path;
Expand All @@ -38,16 +37,16 @@ class SnowflakeModel {
restart();
}



void restart({Duration time = Duration.zero}) {
_path = null;
final startPosition = Offset(-0.2 + 1.4 * random.nextDouble(), -0.2);
final endPosition = Offset(-0.2 + 1.4 * random.nextDouble(), 1.2);
final duration = Duration(seconds: 5, milliseconds: random.nextInt(10000));
tween = MultiTween<AniProps>()
..add(AniProps.X, Tween(begin: startPosition.dx, end: endPosition.dx),duration,Curves.easeInOutSine )
..add(AniProps.Y, Tween(begin: startPosition.dy, end: endPosition.dy),duration,Curves.easeIn );
..add(AniProps.X, Tween(begin: startPosition.dx, end: endPosition.dx),
duration, Curves.easeInOutSine)
..add(AniProps.Y, Tween(begin: startPosition.dy, end: endPosition.dy),
duration, Curves.easeIn);

/* tween = MultiTrackTween([
Track("x").add(
Expand All @@ -63,45 +62,45 @@ class SnowflakeModel {
}

void drawPath() {
if(_path != null) {
if (_path != null) {
return;
}
}
double sideLength = 100;

int iterationsTotal = 1;
// we calculate the total number of iterations
// based on the snowflake's size
if(size > 40) {
// we calculate the total number of iterations
// based on the snowflake's size
if (size > 40) {
iterationsTotal += (size) ~/ 25;
}
_path = Path();
if(cachedFlakes[iterationsTotal] == null) {
double down = (sideLength/2) * math.tan(math.pi/6);
double up = (sideLength/2) * math.tan(math.pi/3) - down;
Point p1 = Point(-sideLength/2,down);
Point p2 = Point(sideLength/2,down);
Point p3 = Point(0,-up);
Point p4 = Point(0,0);
Point p5 = Point(0,0);
if (cachedFlakes[iterationsTotal] == null) {
double down = (sideLength / 2) * math.tan(math.pi / 6);
double up = (sideLength / 2) * math.tan(math.pi / 3) - down;
Point p1 = Point(-sideLength / 2, down);
Point p2 = Point(sideLength / 2, down);
Point p3 = Point(0, -up);
Point p4 = Point(0, 0);
Point p5 = Point(0, 0);
double rot = random.nextDouble() * 6.28319;
List<Point> lines = <Point>[p1, p2, p3];
List<Point> tmpLines = <Point>[];

for(int iterations=0; iterations<iterationsTotal; iterations++){
for (int iterations = 0; iterations < iterationsTotal; iterations++) {
sideLength /= 3;
for(int loop = 0; loop < lines.length; loop++){
for (int loop = 0; loop < lines.length; loop++) {
p1 = lines[loop];
if(loop == lines.length-1) {
if (loop == lines.length - 1) {
p2 = lines[0];
} else {
p2 = lines[loop+1];
p2 = lines[loop + 1];
}
rot = math.atan2(p2.y - p1.y, p2.x - p1.x);
p3 = p1 + Point.polar(sideLength,rot);
rot += math.pi/3;
p4 = p3 + Point.polar(sideLength,rot);
rot -= 2 * math.pi/3;
p5 = p4 + Point.polar(sideLength,rot);
p3 = p1 + Point.polar(sideLength, rot);
rot += math.pi / 3;
p4 = p3 + Point.polar(sideLength, rot);
rot -= 2 * math.pi / 3;
p5 = p4 + Point.polar(sideLength, rot);
tmpLines.add(p1);
tmpLines.add(p3);
tmpLines.add(p4);
Expand All @@ -111,27 +110,28 @@ class SnowflakeModel {
tmpLines = <Point>[];
}
lines.add(p2);
_path!.moveTo(lines[0].x,lines[0].y);
for(int a=0; a<lines.length; a++){
_path!.lineTo(lines[a].x,lines[a].y);
_path!.moveTo(lines[0].x, lines[0].y);
for (int a = 0; a < lines.length; a++) {
_path!.lineTo(lines[a].x, lines[a].y);
}
_path!.lineTo(lines[0].x,lines[0].y);
_path!.lineTo(lines[0].x, lines[0].y);
cachedFlakes[iterationsTotal] = _path!;
} else {
_path = cachedFlakes[iterationsTotal];
}
Matrix4 m = Matrix4.identity();
// the rotation must be in radians
// and to get a random angle we use the 360 equivalent
// the rotation must be in radians
// and to get a random angle we use the 360 equivalent
// in radians that is 6.28319
m.setRotationZ(random.nextDouble() * 6.28319);
num scaleTo = size / sideLength;
m.scale(scaleTo);
List<double> list = m.storage.toList();
_path = _path!.transform(Float64List.fromList(list));
}

Path? get path {
if(_path != null) {
if (_path != null) {
return _path;
}
drawPath();
Expand All @@ -143,4 +143,4 @@ class SnowflakeModel {
restart(time: time);
}
}
}
}
15 changes: 12 additions & 3 deletions lib/snowfall/snowflakes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ class Snowflakes extends StatefulWidget {
final int numberOfSnowflakes;
final Color color;
final int alpha;
const Snowflakes({required this.numberOfSnowflakes,required this.color, required this.alpha,Key? key}) : super(key: key);
const Snowflakes(
{required this.numberOfSnowflakes,
required this.color,
required this.alpha,
Key? key})
: super(key: key);

@override
_SnowflakesState createState() => _SnowflakesState();
Expand All @@ -35,7 +40,11 @@ class _SnowflakesState extends State<Snowflakes> {
onTick: _simulateFlakes,
builder: (context, time) {
return CustomPaint(
painter: SnowflakesPainter(snowflakes: flakes,time: time, color: widget.color,alpha:widget.alpha),
painter: SnowflakesPainter(
snowflakes: flakes,
time: time,
color: widget.color,
alpha: widget.alpha),
);
},
);
Expand All @@ -46,4 +55,4 @@ class _SnowflakesState extends State<Snowflakes> {
flake.maintainRestart(time);
}
}
}
}
13 changes: 8 additions & 5 deletions lib/snowfall/snowflakes_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ class SnowflakesPainter extends CustomPainter {
Duration time;
Color color;
int alpha;
SnowflakesPainter({required this.snowflakes, required this.time, required this.color, required this.alpha});
SnowflakesPainter(
{required this.snowflakes,
required this.time,
required this.color,
required this.alpha});

@override
void paint(Canvas canvas, Size size) {
Expand All @@ -16,15 +20,14 @@ class SnowflakesPainter extends CustomPainter {
for (var snowflake in snowflakes) {
var progress = snowflake.animationProgress!.progress(time);
final animation = snowflake.tween!.transform(progress);
final position =
Offset(animation.get(AniProps.X) * size.width, animation.get(AniProps.Y) * size.height);
final position = Offset(animation.get(AniProps.X) * size.width,
animation.get(AniProps.Y) * size.height);
canvas.drawPath(snowflake.path!.shift(position), p);

}
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
}
18 changes: 9 additions & 9 deletions lib/snowfall/utils/point.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'dart:math' as math;

class Point {

class Point {
double x = 0;
double y = 0;

Point([this.x=0, this.y=0]);
Point([this.x = 0, this.y = 0]);

Point add(Point p) {
return Point(x + p.x, y + p.y);
Expand Down Expand Up @@ -34,7 +33,7 @@ class Point {

/// find point between points
Point _interpolate(Point p, double f) {
return Point( p.x + (x - p.x) * f, p.y + (y - p.y) * f );
return Point(p.x + (x - p.x) * f, p.y + (y - p.y) * f);
}

double length() {
Expand Down Expand Up @@ -68,21 +67,22 @@ class Point {
}

static Point pointsInterpolation(Point p1, Point p2, double f) {
return p1._interpolate(p2, f);
return p1._interpolate(p2, f);
}

static Point polar(double l, double r) {
return Point(l * math.cos(r), l * math.sin(r));
}

static double distance(Point p1, Point p2) {
return p1._distance(p2);
}

Point operator +(Point p) => Point(x+p.x, y+p.y);
Point operator +(Point p) => Point(x + p.x, y + p.y);

@override
bool operator ==(Object other) => hashCode == other.hashCode;

@override
int get hashCode => x.hashCode + y.hashCode;

}
}
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: snowfall
description: A beautiful Snowfall effect for your widgets
version: 0.0.1
description: A beautiful Snowfall effect for your widgets, simply to implement in your App.
version: 0.0.2
homepage: https://github.com/pgiacomo69/snowfall
# publish_to: 'none' # Remove this line if you wish to publish to pub.dev

environment:
sdk: ">=2.15.0 <3.0.0"
sdk: ">=2.13.0 <3.0.0"
flutter: ">=1.17.0"

dependencies:
Expand Down

0 comments on commit 5b2bc56

Please sign in to comment.