Permalink
Browse files

Tweaks

  • Loading branch information...
1 parent 5e29dee commit ead49ccaea2ad08a34ef480d64b3b5718062d7a1 @gfx committed May 10, 2012
Showing with 35 additions and 26 deletions.
  1. +35 −26 Fireworks.dart
View
@@ -5,11 +5,13 @@
#import('dart:html');
-final num quantity = 2000;
-final num size = 2.0;
-final num decay = 0.98;
-final num gravity = 2.0;
-final num speed = 6.0;
+class Config {
+ static final num quantity = 1000;
+ static final num size = 2.0;
+ static final num decay = 0.98;
+ static final num gravity = 2.0;
+ static final num speed = 6.0;
+}
class Random { // drand48
static num x = 0;
@@ -19,7 +21,7 @@ class Random { // drand48
return x * (1.0/(0xFFFFFFFFFFFF+1));
}
}
-num random() => Math.random(); // Random.next();
+num random() => Random.next();
String randomColor() {
final List<int> rgb = new List<int>(3);
@@ -37,56 +39,63 @@ class Spark {
num velX;
num velY;
num size;
+ String color;
+ int state = 0;
- Spark(this.posX, this.posY, this.size) {
+ Spark(this.posX, this.posY, this.size, this.color) {
num angle = random() * rad;
- num velocity = random() * speed;
+ num velocity = random() * Config.speed;
velX = Math.cos(angle) * velocity;
velY = Math.sin(angle) * velocity;
}
- bool draw(FireworkView view, String color) {
+ bool draw(FireworkView view) {
posX += velX;
- posY += velY;
-
- velX *= decay;
- velY *= decay;
- size *= decay;
-
- posY += gravity;
+ posY += velY + Config.gravity;
+
+ velX *= Config.decay;
+ velY *= Config.decay;
+ size *= Config.decay;
+
+ if(size < 0.5 && state == 0) {
+ color = randomColor();
+ size = Config.size;
+ ++state;
+ }
view.cx.beginPath();
view.cx.arc(posX, posY, size, 0, rad, true);
- view.cx.fillStyle = color;
+ view.cx.fillStyle = random() > 0.2 ? color : "white";
view.cx.fill();
- if(size <= 0.1) return false;
- if(posX <= 0 || posY <= 0) return false;
+ if(size <= 0.01) return false;
+ if(posX <= 0) return false;
if(posX >= view.width || posY >= view.height) return false;
return true;
}
}
class Firework {
- final String color;
final List<Spark> sparks;
final FireworkView view;
- Firework(this.view, int x, int y) : color = randomColor(), sparks = new List<Spark>() {
- for(int i = 0; i < quantity; ++i) {
- sparks.add(new Spark(x, y, size));
+ Firework(this.view, int x, int y) : sparks = new List<Spark>() {
+
+ String color = "lime";
+ for(int i = 0; i < Config.quantity; ++i) {
+ sparks.add(new Spark(x, y, Config.size, color));
}
}
bool update() {
for(int i = 0; i < sparks.length; ++i) {
Spark s = sparks[i];
- if(! s.draw(view, color)) {
+ if(! s.draw(view)) {
sparks.removeRange(i, 1);
}
}
@@ -122,8 +131,8 @@ class FireworkView {
explode(e.touches[0].pageX, e.touches[1].pageY);
});
- // initial one
- explode(width / 2 + top, height / 3);
+ // initial one
+ explode((width / 2 + top).toInt(), (height / 3).toInt());
});
}

0 comments on commit ead49cc

Please sign in to comment.