Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Tweaks

  • Loading branch information...
commit ead49ccaea2ad08a34ef480d64b3b5718062d7a1 1 parent 5e29dee
Fuji, Goro authored May 11, 2012

Showing 1 changed file with 35 additions and 26 deletions. Show diff stats Hide diff stats

  1. 61  Fireworks.dart
61  Fireworks.dart
@@ -5,11 +5,13 @@
5 5
 
6 6
 #import('dart:html');
7 7
 
8  
-final num quantity  = 2000;
9  
-final num size      = 2.0;
10  
-final num decay     = 0.98;
11  
-final num gravity   = 2.0;
12  
-final num speed     = 6.0;
  8
+class Config {
  9
+  static final num quantity  = 1000;
  10
+  static final num size      = 2.0;
  11
+  static final num decay     = 0.98;
  12
+  static final num gravity   = 2.0;
  13
+  static final num speed     = 6.0;
  14
+}
13 15
 
14 16
 class Random { // drand48
15 17
   static num x = 0;
@@ -19,7 +21,7 @@ class Random { // drand48
19 21
     return x * (1.0/(0xFFFFFFFFFFFF+1));
20 22
   }
21 23
 }
22  
-num random() => Math.random(); // Random.next();
  24
+num random() => Random.next();
23 25
 
24 26
 String randomColor() {
25 27
   final List<int> rgb = new List<int>(3);
@@ -37,48 +39,55 @@ class Spark {
37 39
   num velX;
38 40
   num velY;
39 41
   num size;
  42
+  String color;
  43
+  int state = 0;
40 44
 
41  
-  Spark(this.posX, this.posY, this.size) {
  45
+  Spark(this.posX, this.posY, this.size, this.color) {
42 46
     num angle    = random() * rad;
43  
-    num velocity = random() * speed;
  47
+    num velocity = random() * Config.speed;
44 48
 
45 49
     velX = Math.cos(angle) * velocity;
46 50
     velY = Math.sin(angle) * velocity;
47 51
   }
48 52
 
49  
-  bool draw(FireworkView view, String color) {
  53
+  bool draw(FireworkView view) {
50 54
     posX += velX;
51  
-    posY += velY;
52  
-
53  
-    velX *= decay;
54  
-    velY *= decay;
55  
-    size *= decay;
56  
-
57  
-    posY += gravity;
  55
+    posY += velY + Config.gravity;
  56
+
  57
+    velX *= Config.decay;
  58
+    velY *= Config.decay;
  59
+    size *= Config.decay;
  60
+    
  61
+    if(size < 0.5 && state == 0) {
  62
+      color = randomColor();
  63
+      size  = Config.size;
  64
+      ++state;
  65
+    }
58 66
 
59 67
     view.cx.beginPath();
60 68
     view.cx.arc(posX, posY, size, 0, rad, true);
61 69
 
62  
-    view.cx.fillStyle = color;
  70
+    view.cx.fillStyle = random() > 0.2 ? color : "white";
63 71
 
64 72
     view.cx.fill();
65 73
 
66  
-    if(size <= 0.1) return false;
67  
-    if(posX <= 0 || posY <= 0) return false;
  74
+    if(size <= 0.01) return false;
  75
+    if(posX <= 0) return false;
68 76
     if(posX >= view.width || posY >= view.height) return false;
69 77
     return true;
70 78
   }
71 79
 }
72 80
 
73 81
 class Firework {
74  
-  final String color;
75 82
   final List<Spark> sparks;
76 83
 
77 84
   final FireworkView view;
78 85
 
79  
-  Firework(this.view, int x, int y) : color = randomColor(), sparks = new List<Spark>() {
80  
-    for(int i = 0; i < quantity; ++i) {
81  
-      sparks.add(new Spark(x, y, size));
  86
+  Firework(this.view, int x, int y) : sparks = new List<Spark>() {
  87
+    
  88
+    String color = "lime";
  89
+    for(int i = 0; i < Config.quantity; ++i) {
  90
+      sparks.add(new Spark(x, y, Config.size, color));
82 91
     }
83 92
   }
84 93
 
@@ -86,7 +95,7 @@ class Firework {
86 95
     for(int i = 0; i < sparks.length; ++i) {
87 96
       Spark s = sparks[i];
88 97
 
89  
-      if(! s.draw(view, color)) {
  98
+      if(! s.draw(view)) {
90 99
         sparks.removeRange(i, 1);
91 100
       }
92 101
     }
@@ -122,8 +131,8 @@ class FireworkView {
122 131
         explode(e.touches[0].pageX, e.touches[1].pageY);
123 132
       });
124 133
 
125  
-    // initial one
126  
-    explode(width / 2 + top, height / 3);
  134
+      // initial one
  135
+      explode((width / 2 + top).toInt(), (height / 3).toInt());
127 136
     });
128 137
   }
129 138
 

0 notes on commit ead49cc

Please sign in to comment.
Something went wrong with that request. Please try again.