Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit 93da134569fd9b5d99bc9a83e3c7e0e8cf2e956a @gfx committed
Showing with 3,909 additions and 0 deletions.
  1. +1 −0 .children
  2. +3 −0 .gitignore
  3. +17 −0 .project
  4. +192 −0 Fireworks.dart
  5. +3,662 −0 Fireworks.dart.js
  6. +21 −0 app.css
  7. +13 −0 index.html
1 .children
@@ -0,0 +1 @@
+Fireworks.dart
3 .gitignore
@@ -0,0 +1,3 @@
+*~
+*.swp
+
17 .project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>Fireworks</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>com.google.dart.tools.core.dartBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>com.google.dart.tools.core.dartNature</nature>
+ </natures>
+</projectDescription>
192 Fireworks.dart
@@ -0,0 +1,192 @@
+// a Dart port of http://jsdo.it/zarkswerk/fireworx
+
+// Author: Fuji, Goro (gfx) <gfuji@cpan.org>
+// LICENSE: The MIT License
+
+#import('dart:html');
+
+num random() {
+ final num N = 3;
+ num gen = 0.0;
+ for(int i = 0; i < N; ++i) {
+ gen += Math.random();
+ }
+ return gen / N;
+}
+
+String randomColor() {
+ final List<int> rgb = new List<int>(3);
+ for(int i = 0; i < rgb.length; ++i) {
+ rgb[i] = (random() * 0xFF).toInt();
+ }
+ return "rgb(${rgb[0]},${rgb[1]},${rgb[2]})";
+}
+
+class Spark {
+ static final num rad = Math.PI * 2;
+
+ num posX;
+ num posY;
+ num velX;
+ num velY;
+ num size;
+ bool sw;
+
+ Spark(this.posX, this.posY, this.size) {
+ num angle = random() * rad;
+ num velocity = random() * 5.0;
+
+ velX = Math.cos(angle) * velocity;
+ velY = Math.sin(angle) * velocity;
+ sw = random() > 0.5;
+ }
+
+ void computeVelocity() {
+ posX += velX;
+ posY += velY;
+ }
+
+ void computeDecay(num d) {
+ velX *= d;
+ velY *= d;
+ size *= d;
+ }
+
+ void computeGravity(num g) {
+ posY += g;
+ }
+
+ void draw(CanvasRenderingContext2D cx, String color) {
+ cx.beginPath();
+ cx.arc(posX, posY, size, 0, rad, true);
+
+ if(sw) {
+ cx.fillStyle = "rgb(255, 255, 255)";
+ sw = false;
+ }
+ else {
+ cx.fillStyle = color;
+ sw = true;
+ }
+
+ cx.fill();
+ }
+}
+
+class Firework {
+ static final num quantity = 360;
+ static final num size = 2.0;
+ static final num decay = 0.98;
+ static final num gravity = 1.5;
+
+
+ final String color;
+ final List<Spark> sparks;
+
+ Firework(int x, int y) : color = randomColor(), sparks = new List<Spark>() {
+ for(int i = 0; i < quantity; ++i) {
+ sparks.add(new Spark(x, y, size));
+ }
+ }
+
+ void update(CanvasRenderingContext2D cx) {
+ for(int i = 0; i < sparks.length; ++i) {
+ Spark s = sparks[i];
+
+ s.computeVelocity();
+ s.computeDecay(decay);
+ s.computeGravity(gravity);
+
+ s.draw(cx, color);
+
+
+ if(s.size <= 0.1 || s.posX <= 0 || s.posX >= 400 || s.posY >= 400) {
+ sparks.removeRange(i, 1);
+ }
+ }
+ }
+
+ bool isDismissed() => sparks.isEmpty();
+}
+
+class FireworkView {
+ CanvasRenderingContext2D cx;
+ int width;
+ int height;
+ int left;
+ int top;
+
+ List<Firework> fireworks;
+
+ FireworkView(CanvasElement canvas) : fireworks = new List<Firework>() {
+ cx = canvas.getContext("2d");
+
+ width = canvas.width;
+ height = canvas.height;
+ canvas.rect.then((ElementRect rect) {
+ left = rect.bounding.left;
+ top = rect.bounding.top;
+
+ canvas.on.mouseDown.add( (MouseEvent e) {
+ explode(e.clientX, e.clientY);
+ });
+ canvas.on.touchStart.add( (TouchEvent e) {
+ explode(e.touches[0].pageX, e.touches[1].pageY);
+ });
+ });
+ }
+
+ void explode(int x, int y) {
+ fireworks.add(new Firework(x - left, y - top));
+ }
+
+ void update() {
+ if(fireworks.isEmpty()) return;
+
+ for(int i = 0; i < fireworks.length; ++i) {
+ Firework fw = fireworks[i];
+
+ fw.update(cx);
+
+ if(fw.isDismissed()) {
+ fireworks.removeRange(i, 1);
+ }
+ }
+
+ cx.fillStyle = "rgba(0, 0, 0, 0.3)";
+ cx.fillRect(0, 0, width, height);
+ }
+}
+
+class FPSWatcher {
+ Stopwatch watch;
+ int fps;
+
+ FPSWatcher() {
+ watch = new Stopwatch.start();
+ fps = 0;
+ }
+
+ void update() {
+ ++fps;
+
+ if(watch.elapsedInMs() >= 1000) {
+ String message = "FPS: $fps";
+ document.query('#fps').innerHTML = message;
+ watch.reset();
+ fps = 0;
+ }
+ }
+}
+
+void main() {
+ CanvasElement canvas = document.query("#night-sky");
+
+ FireworkView fm = new FireworkView(canvas);
+ FPSWatcher watcher = new FPSWatcher();
+
+ window.setInterval(() {
+ fm.update();
+ watcher.update();
+ }, 0);
+}
3,662 Fireworks.dart.js
3,662 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
21 app.css
@@ -0,0 +1,21 @@
+body {
+ background-color: #222;
+ padding: 0;
+ margin: 0;
+ overflow: hidden;
+}
+canvas {
+ background-color: #000;
+ padding: 0;
+ margin: 30px;
+}
+
+#fps {
+ position: absolute;
+ top: 0;
+ left: 0;
+ padding: 5px;
+ margin: 0;
+ font-size: 20px;
+ color: #fff;
+}
13 index.html
@@ -0,0 +1,13 @@
+<html>
+ <head>
+ <title>Fireworks</title>
+ <link rel="stylesheet" type="text/css" href="app.css" />
+
+ <script type="application/dart" src="Fireworks.dart"></script>
+ <script src="http://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js"></script>
+ </head>
+ <body>
+ <canvas id="night-sky" width="400" height="400"></canvas>
+ <p id="fps">not running</p>
+ </body>
+</html>

0 comments on commit 93da134

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