Skip to content

Commit 7ca7b71

Browse files
committed
feat: add variable refresh with delta time
1 parent 92173d9 commit 7ca7b71

File tree

9 files changed

+74
-74
lines changed

9 files changed

+74
-74
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Upcomming
2+
⛔️ Breaking!
3+
- Added the concept of delta time, to allow for better simulation when the refresh rate is 120 (or more than 60). There should be not noticable change on a 60fps screen, however, there may be variance if Flutter drops frames. No changes needed from a widget level.
4+
- `ParticleSystem.update` and `Pariticle.update` now contain a `deltaTime` argument. No changes needed from a widget level.
5+
16
## [0.7.0]
27
⭐️ Added
38
- Stroke width and color can now optionally be set. `strokeWidth` (default 0) and `strokeColor` (default black). Requires a stroke width bigger than 0

example/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ fvm_config.json
4040
/build/
4141

4242
# Web related
43-
lib/generated_plugin_registrant.dart
4443

4544
# Exceptions to above rules.
4645
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

example/pubspec.lock

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.8.2"
10+
version: "2.9.0"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -21,21 +21,14 @@ packages:
2121
name: characters
2222
url: "https://pub.dartlang.org"
2323
source: hosted
24-
version: "1.2.0"
25-
charcode:
26-
dependency: transitive
27-
description:
28-
name: charcode
29-
url: "https://pub.dartlang.org"
30-
source: hosted
31-
version: "1.3.1"
24+
version: "1.2.1"
3225
clock:
3326
dependency: transitive
3427
description:
3528
name: clock
3629
url: "https://pub.dartlang.org"
3730
source: hosted
38-
version: "1.1.0"
31+
version: "1.1.1"
3932
collection:
4033
dependency: transitive
4134
description:
@@ -63,7 +56,7 @@ packages:
6356
name: fake_async
6457
url: "https://pub.dartlang.org"
6558
source: hosted
66-
version: "1.3.0"
59+
version: "1.3.1"
6760
flutter:
6861
dependency: "direct main"
6962
description: flutter
@@ -80,28 +73,28 @@ packages:
8073
name: matcher
8174
url: "https://pub.dartlang.org"
8275
source: hosted
83-
version: "0.12.11"
76+
version: "0.12.12"
8477
material_color_utilities:
8578
dependency: transitive
8679
description:
8780
name: material_color_utilities
8881
url: "https://pub.dartlang.org"
8982
source: hosted
90-
version: "0.1.4"
83+
version: "0.1.5"
9184
meta:
9285
dependency: transitive
9386
description:
9487
name: meta
9588
url: "https://pub.dartlang.org"
9689
source: hosted
97-
version: "1.7.0"
90+
version: "1.8.0"
9891
path:
9992
dependency: transitive
10093
description:
10194
name: path
10295
url: "https://pub.dartlang.org"
10396
source: hosted
104-
version: "1.8.1"
97+
version: "1.8.2"
10598
sky_engine:
10699
dependency: transitive
107100
description: flutter
@@ -113,7 +106,7 @@ packages:
113106
name: source_span
114107
url: "https://pub.dartlang.org"
115108
source: hosted
116-
version: "1.8.2"
109+
version: "1.9.0"
117110
stack_trace:
118111
dependency: transitive
119112
description:
@@ -134,21 +127,21 @@ packages:
134127
name: string_scanner
135128
url: "https://pub.dartlang.org"
136129
source: hosted
137-
version: "1.1.0"
130+
version: "1.1.1"
138131
term_glyph:
139132
dependency: transitive
140133
description:
141134
name: term_glyph
142135
url: "https://pub.dartlang.org"
143136
source: hosted
144-
version: "1.2.0"
137+
version: "1.2.1"
145138
test_api:
146139
dependency: transitive
147140
description:
148141
name: test_api
149142
url: "https://pub.dartlang.org"
150143
source: hosted
151-
version: "0.4.9"
144+
version: "0.4.12"
152145
vector_math:
153146
dependency: transitive
154147
description:

example/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ version: 1.0.0+1
66
publish_to: none
77

88
environment:
9-
sdk: '>=2.12.0 <3.0.0'
9+
sdk: ">=2.17.0 <3.0.0"
1010

1111
dependencies:
1212
confetti:
@@ -20,4 +20,4 @@ dev_dependencies:
2020
sdk: flutter
2121

2222
flutter:
23-
uses-material-design: true
23+
uses-material-design: true

lib/src/confetti.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:math';
22

3+
import 'package:confetti/src/constants.dart';
34
import 'package:confetti/src/particle.dart';
45
import 'package:flutter/material.dart';
56

@@ -202,12 +203,23 @@ class _ConfettiWidgetState extends State<ConfettiWidget>
202203
}
203204
}
204205

206+
late var lastTime = DateTime.now().millisecondsSinceEpoch;
207+
205208
void _animationListener() {
206209
if (_particleSystem.particleSystemStatus == ParticleSystemStatus.finished) {
207210
_animController.stop();
208211
return;
209212
}
210-
_particleSystem.update();
213+
final currentTime = DateTime.now().millisecondsSinceEpoch;
214+
final deltaTime = (currentTime - lastTime) / 1000;
215+
216+
lastTime = currentTime;
217+
218+
if (deltaTime > kLowLimit) {
219+
_particleSystem.update(kLowLimit);
220+
} else {
221+
_particleSystem.update(deltaTime);
222+
}
211223
}
212224

213225
void _animationStatusListener(AnimationStatus status) {

lib/src/constants.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const double kLowLimit = 1 / 60;
2+
const desiredSpeed = 1 / kLowLimit;

lib/src/helper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:flutter/material.dart';
55

66
final _rand = Random();
77

8-
class Helper {
8+
abstract class Helper {
99
static double randomize(double min, double max) {
1010
return lerpDouble(min, max, _rand.nextDouble())!;
1111
}

lib/src/particle.dart

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:math';
22
import 'dart:ui';
33

4+
import 'package:confetti/src/constants.dart';
45
import 'package:flutter/material.dart';
56
import 'package:vector_math/vector_math.dart' as vmath;
67

@@ -109,10 +110,10 @@ class ParticleSystem extends ChangeNotifier {
109110
List<Particle> get particles => _particles;
110111
ParticleSystemStatus? get particleSystemStatus => _particleSystemStatus;
111112

112-
void update() {
113+
void update(double deltaTime) {
113114
_clean();
114115
if (_particleSystemStatus != ParticleSystemStatus.finished) {
115-
_updateParticles();
116+
_updateParticles(deltaTime);
116117
}
117118

118119
if (_particleSystemStatus == ParticleSystemStatus.started) {
@@ -143,9 +144,9 @@ class ParticleSystem extends ChangeNotifier {
143144
_leftBorder = _screenSize!.width - _rightBorder;
144145
}
145146

146-
void _updateParticles() {
147+
void _updateParticles(double deltaTime) {
147148
for (final particle in _particles) {
148-
particle.update();
149+
particle.update(deltaTime);
149150
}
150151
}
151152

@@ -214,14 +215,16 @@ class Particle {
214215
_acceleration = vmath.Vector2.zero(),
215216
_velocity =
216217
vmath.Vector2(Helper.randomize(-3, 3), Helper.randomize(-3, 3)),
217-
// _size = size,
218218
_pathShape = createParticlePath != null
219219
? createParticlePath(size)
220220
: createPath(size),
221221
_aVelocityX = Helper.randomize(-0.1, 0.1),
222222
_aVelocityY = Helper.randomize(-0.1, 0.1),
223223
_aVelocityZ = Helper.randomize(-0.1, 0.1),
224-
_gravity = lerpDouble(0.1, 5, gravity);
224+
gravityVector = vmath.Vector2(
225+
0,
226+
lerpDouble(0.1, 5, gravity)!,
227+
);
225228

226229
final vmath.Vector2 _startUpForce;
227230

@@ -236,14 +239,15 @@ class Particle {
236239
double _aVelocityY;
237240
double _aZ = 0;
238241
double _aVelocityZ;
239-
final double? _gravity;
242+
final vmath.Vector2 gravityVector;
240243
final _aAcceleration = 0.0001;
241244

242245
final Color _color;
243246
final double _mass;
244247
final Path _pathShape;
245248

246249
double _timeAlive = 0;
250+
vmath.Vector2 windforceUp = vmath.Vector2(0, -1);
247251

248252
static Path createPath(Size size) {
249253
final pathShape = Path()
@@ -255,53 +259,45 @@ class Particle {
255259
return pathShape;
256260
}
257261

258-
void applyForce(vmath.Vector2 force) {
262+
void applyForce(vmath.Vector2 force, double deltaTimeSpeed) {
259263
final f = force.clone()..divide(vmath.Vector2.all(_mass));
260-
_acceleration.add(f);
264+
_acceleration.add(f * deltaTimeSpeed);
261265
}
262266

263-
void drag() {
267+
void drag(double deltaTimeSpeed) {
264268
final speed = sqrt(pow(_velocity.x, 2) + pow(_velocity.y, 2));
265269
final dragMagnitude = _particleDrag * speed * speed;
266270
final drag = _velocity.clone()
267271
..multiply(vmath.Vector2.all(-1))
268272
..normalize()
269273
..multiply(vmath.Vector2.all(dragMagnitude));
270-
applyForce(drag);
274+
applyForce(drag, deltaTimeSpeed);
271275
}
272276

273-
void _applyStartUpForce() {
274-
applyForce(_startUpForce);
275-
}
276-
277-
void _applyWindForceUp() {
278-
applyForce(vmath.Vector2(0, -1));
279-
}
280-
281-
void update() {
282-
drag();
277+
void update(double deltaTime) {
278+
final deltaTimeSpeed = deltaTime * desiredSpeed;
279+
drag(deltaTimeSpeed);
283280

284281
if (_timeAlive < 5) {
285-
_applyStartUpForce();
282+
applyForce(_startUpForce, deltaTimeSpeed);
286283
}
287284
if (_timeAlive < 25) {
288-
_applyWindForceUp();
285+
applyForce(windforceUp, deltaTimeSpeed);
286+
_timeAlive += 1;
289287
}
290288

291-
_timeAlive += 1;
292-
293-
applyForce(vmath.Vector2(0, _gravity!));
289+
applyForce(gravityVector, deltaTimeSpeed);
294290

295-
_velocity.add(_acceleration);
296-
_location.add(_velocity);
291+
_velocity.add(_acceleration * deltaTimeSpeed);
292+
_location.add(_velocity * deltaTimeSpeed);
297293
_acceleration.setZero();
298294

299295
_aVelocityX += _aAcceleration / _mass;
300296
_aVelocityY += _aAcceleration / _mass;
301297
_aVelocityZ += _aAcceleration / _mass;
302-
_aX += _aVelocityX;
303-
_aY += _aVelocityY;
304-
_aZ += _aVelocityZ;
298+
_aX += _aVelocityX * deltaTimeSpeed;
299+
_aY += _aVelocityY * deltaTimeSpeed;
300+
_aZ += _aVelocityZ * deltaTimeSpeed;
305301
}
306302

307303
Offset get location {

0 commit comments

Comments
 (0)