Skip to content
Browse files

No more node stuff - only Rack

  • Loading branch information...
1 parent dec9b47 commit b41df14e024f4c876d5377fa74c41c49164ec3a2 @rbgrouleff committed Jun 20, 2012
View
6 README.md
@@ -10,9 +10,9 @@ Internet Explorer >=10.0.
It is heavily inspired from http://www.arachnoid.com/ruby/gravity/index.html -
which is also where I have the planetary data from.
-If you want to run it, just clone this repo, run `npm install` in the
-dir, run the server with `coffee server` and point your browser to
-http://localhost:3000/index.html
+If you want to run it, just clone this repo, run `bundle install` in the
+dir, run the server with `rackup` and point your browser to
+http://localhost:9292/
#License
View
55 client/lib/body.js
@@ -1,55 +0,0 @@
-(function() {
- var Body;
-
- module.exports = Body = (function() {
-
- Body.G = 6.6742e-11;
-
- function Body(name, radius, mass, position, velocity) {
- this.name = name;
- this.radius = radius;
- this.mass = mass;
- this.position = position;
- this.velocity = velocity;
- }
-
- Body.prototype.exertPullOn = function(otherBody, dt) {
- var radius;
- if (otherBody === this) return;
- radius = otherBody.position.sub(this.position);
- return otherBody.updateVelocity(radius, this.mass, dt);
- };
-
- Body.prototype.updateVelocity = function(r, mass, dt) {
- var acceleration, hypot, sumOfSquares;
- sumOfSquares = r.sumOfSquares();
- hypot = Math.sqrt(sumOfSquares);
- acceleration = -Body.G * mass / sumOfSquares;
- return this.velocity = this.velocity.add(r.div(hypot).mult(acceleration * dt));
- };
-
- Body.prototype.calculateGravitationalPull = function(otherBody, dt) {
- var acceleration, hyp, radius, radiusSquared;
- if (otherBody !== this) {
- radius = this.position.sub(otherBody.position);
- radiusSquared = radius.sumOfSquares();
- hyp = Math.sqrt(radiusSquared);
- acceleration = -Body.G * otherBody.mass / radiusSquared;
- return this.velocity = this.velocity.add(radius.div(hyp).mult(acceleration * dt));
- }
- };
-
- Body.prototype.move = function(dt) {
- this.oldPosition = this.position;
- return this.position = this.position.add(this.velocity.mult(dt));
- };
-
- Body.prototype.distanceToCenter = function() {
- return Math.sqrt(this.position.sumOfSquares());
- };
-
- return Body;
-
- })();
-
-}).call(this);
View
70 client/lib/main.js
@@ -1,70 +0,0 @@
-(function() {
- var Runner, Screen, Timer, Vector, solarSystem,
- __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
-
- Timer = require('./timer');
-
- Screen = require('./screen');
-
- Vector = require('./vector');
-
- solarSystem = require('./solar_system');
-
- module.exports = Runner = (function() {
-
- function Runner() {
- this.tick = __bind(this.tick, this);
- this.startStop = __bind(this.startStop, this);
- var canvas;
- canvas = document.getElementById('universe');
- this.timer = new Timer(60, canvas);
- this.startStopButton = document.getElementById('start_stop');
- this.planets = solarSystem.activatePlanets(['Sun', 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter']);
- this.screen = new Screen(canvas, this.planets, solarSystem.sun, solarSystem.farthestPlanet().distanceToCenter());
- this.dt = 24 * 60 * 60;
- }
-
- Runner.prototype.startStop = function() {
- if (this.timer.running) {
- return this.timer.stop();
- } else {
- return this.timer.start();
- }
- };
-
- Runner.prototype.tick = function() {
- var otherPlanet, planet, _i, _j, _k, _len, _len2, _len3, _ref, _ref2, _ref3;
- _ref = this.planets;
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- planet = _ref[_i];
- _ref2 = this.planets;
- for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
- otherPlanet = _ref2[_j];
- planet.calculateGravitationalPull(otherPlanet, this.dt);
- }
- }
- _ref3 = this.planets;
- for (_k = 0, _len3 = _ref3.length; _k < _len3; _k++) {
- planet = _ref3[_k];
- planet.move(this.dt);
- }
- return this.screen.draw(this.planets);
- };
-
- Runner.prototype.render = function() {
- this.screen.draw(this.planets);
- this.timer.registerCallback(this.tick);
- return this.startStopButton.onclick = this.startStop;
- };
-
- Runner.init = function() {
- var runner;
- runner = new Runner;
- return runner.render();
- };
-
- return Runner;
-
- })();
-
-}).call(this);
View
72 client/lib/screen.js
@@ -1,72 +0,0 @@
-(function() {
- var Screen, Vector;
-
- Vector = require('./vector');
-
- module.exports = Screen = (function() {
-
- function Screen(canvas, bodies, center, maxDistance) {
- var body, _i, _len;
- this.center = center;
- this.maxDistance = maxDistance;
- this.height = canvas.height = canvas.clientHeight;
- this.width = canvas.width = canvas.clientWidth;
- this.canvasCenter = new Vector(Math.floor(this.width / 2), Math.floor(this.height / 2));
- this.ctx = canvas.getContext('2d');
- this.prerenderedBodies = {};
- for (_i = 0, _len = bodies.length; _i < _len; _i++) {
- body = bodies[_i];
- this.prerenderBody(body);
- }
- }
-
- Screen.prototype.draw = function(bodies) {
- var body, _i, _len;
- this.ctx.clearRect(0, 0, this.width, this.height);
- this.ctx.save();
- this.ctx.translate(this.canvasCenter.x, this.canvasCenter.y);
- for (_i = 0, _len = bodies.length; _i < _len; _i++) {
- body = bodies[_i];
- this.renderBody(body);
- }
- return this.ctx.restore();
- };
-
- Screen.prototype.renderBody = function(body) {
- var bodyRadius, translatedPosition;
- bodyRadius = this.radiusInPixels(body.radius);
- translatedPosition = this.positionInPixels(body.position.sub(this.center.position));
- translatedPosition = new Vector(Math.floor(translatedPosition.x), Math.floor(translatedPosition.y));
- return this.ctx.drawImage(this.prerenderedBodies[body.name], Math.floor(translatedPosition.x) - bodyRadius, Math.floor(translatedPosition.y) - bodyRadius);
- };
-
- Screen.prototype.prerenderBody = function(body) {
- var bodyCanvas, bodyCtx, bodyRadius;
- bodyRadius = this.radiusInPixels(body.radius);
- bodyCanvas = document.createElement('canvas');
- bodyCanvas.width = bodyCanvas.height = bodyRadius * 2;
- bodyCtx = bodyCanvas.getContext('2d');
- bodyCtx.fillStyle = "rgb(255, 255, 255)";
- bodyCtx.beginPath();
- bodyCtx.arc(bodyRadius, bodyRadius, bodyRadius, 0, 2 * Math.PI);
- bodyCtx.fill();
- return this.prerenderedBodies[body.name] = bodyCanvas;
- };
-
- Screen.prototype.scale = function() {
- return Math.ceil(this.maxDistance / (this.height / 2 - 10));
- };
-
- Screen.prototype.positionInPixels = function(position) {
- if (position) return position.div(this.scale());
- };
-
- Screen.prototype.radiusInPixels = function(radius) {
- return Math.ceil(Math.log(radius)) - 13;
- };
-
- return Screen;
-
- })();
-
-}).call(this);
View
48 client/lib/solar_system.js
@@ -1,48 +0,0 @@
-(function() {
- var Body, Vector;
-
- Body = require('./body');
-
- Vector = require('./vector');
-
- module.exports = {
- sun: new Body("Sun", 695000000, 1.989E+030, new Vector(0, 0), new Vector(0, 0)),
- mercury: new Body("Mercury", 2440000, 3.33E+023, new Vector(0, 57900000000), new Vector(47900, 0)),
- venus: new Body("Venus", 6050000, 4.869E+024, new Vector(0, 108000000000), new Vector(35000, 0)),
- earth: new Body("Earth", 6378140, 5.976E+024, new Vector(0, 150000000000), new Vector(29800, 0)),
- mars: new Body("Mars", 3397200, 6.421E+023, new Vector(0, 227940000000), new Vector(24100, 0)),
- jupiter: new Body("Jupiter", 71492000, 1.9E+027, new Vector(0, 778330000000), new Vector(13100, 0)),
- saturn: new Body("Saturn", 60268000, 5.688E+026, new Vector(0, 1429400000000), new Vector(9640, 0)),
- uranus: new Body("Uranus", 25559000, 8.686E+025, new Vector(0, 2870990000000), new Vector(6810, 0)),
- neptune: new Body("Neptune", 24746000, 1.024E+026, new Vector(0, 4504300000000), new Vector(5430, 0)),
- pluto: new Body("Pluto", 1137000, 1.27E+022, new Vector(0, 5913520000000), new Vector(4740, 0)),
- activatePlanets: function(list) {
- this.activatedPlanets = list;
- return this.activePlanets();
- },
- activePlanets: function() {
- var name, planet, _results;
- _results = [];
- for (name in this) {
- planet = this[name];
- if (this.activatedPlanets.indexOf(planet.name) > -1) _results.push(planet);
- }
- return _results;
- },
- farthestPlanet: function() {
- return this.max(this.activePlanets(), function(planet) {
- return Math.sqrt(planet.position.sumOfSquares());
- });
- },
- max: function(list, max) {
- var maxTemp, planet, _i, _len;
- maxTemp = list[0];
- for (_i = 0, _len = list.length; _i < _len; _i++) {
- planet = list[_i];
- if (max(maxTemp) < max(planet)) maxTemp = planet;
- }
- return maxTemp;
- }
- };
-
-}).call(this);
View
65 client/lib/timer.js
@@ -1,65 +0,0 @@
-(function() {
- var Timer, requestAnimationFrame,
- __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
-
- requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
-
- module.exports = Timer = (function() {
-
- function Timer(fps, canvas, debug) {
- this.fps = fps;
- this.canvas = canvas;
- this.debug = debug != null ? debug : false;
- this.fpsCounter = __bind(this.fpsCounter, this);
- this.tick = __bind(this.tick, this);
- this.callbacks = [];
- this.msPrFrame = Math.ceil(1000 / this.fps);
- this.ms = 0;
- this.ticksDuringLastSecond = 0;
- if (debug) this.registerCallback(this.fpsCounter);
- }
-
- Timer.prototype.tick = function(timestamp) {
- var callback, delta, _i, _len, _ref;
- delta = timestamp - this.lastTickTime;
- if (delta > this.msPrFrame) {
- this.lastTickTime = timestamp;
- _ref = this.callbacks;
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- callback = _ref[_i];
- callback(delta);
- }
- }
- if (this.running) return requestAnimationFrame(this.tick, this.canvas);
- };
-
- Timer.prototype.start = function() {
- if (this.running) return;
- this.running = true;
- this.lastTickTime = Date.now();
- return requestAnimationFrame(this.tick, this.canvas);
- };
-
- Timer.prototype.stop = function() {
- return this.running = false;
- };
-
- Timer.prototype.registerCallback = function(callback) {
- return this.callbacks.push(callback);
- };
-
- Timer.prototype.fpsCounter = function(delta) {
- if (this.ms + delta > 1000) {
- console.log("FPS: " + this.ticksDuringLastSecond);
- return this.ms = this.ticksDuringLastSecond = 0;
- } else {
- this.ms += delta;
- return this.ticksDuringLastSecond += 1;
- }
- };
-
- return Timer;
-
- })();
-
-}).call(this);
View
55 client/lib/vector.js
@@ -1,55 +0,0 @@
-(function() {
- var Vector;
-
- module.exports = Vector = (function() {
-
- function Vector(x, y) {
- this.x = x;
- this.y = y;
- }
-
- Vector.prototype.mult = function(v) {
- if (this.constructor === v.constructor) {
- return new Vector(this.x * v.x, this.y * v.y);
- } else {
- return new Vector(this.x * v, this.y * v);
- }
- };
-
- Vector.prototype.div = function(v) {
- if (this.constructor === v.constructor) {
- return new Vector(this.x / v.x, this.y / v.y);
- } else {
- return new Vector(this.x / v, this.y / v);
- }
- };
-
- Vector.prototype.add = function(v) {
- return new Vector(this.x + v.x, this.y + v.y);
- };
-
- Vector.prototype.sub = function(v) {
- return new Vector(this.x - v.x, this.y - v.y);
- };
-
- Vector.prototype.sumOfSquares = function() {
- return this.x * this.x + this.y * this.y;
- };
-
- Vector.prototype.eq = function(v) {
- if (v) {
- return this.x === v.x && this.y === v.y;
- } else {
- return false;
- }
- };
-
- Vector.prototype.toString = function() {
- return "(" + this.x + ", " + this.y + ")";
- };
-
- return Vector;
-
- })();
-
-}).call(this);
View
5 client/package.json
@@ -1,5 +0,0 @@
-{
- "name": "gravity",
- "version": "0.0.1",
- "main": "./lib/main.js"
-}
View
33 client/src/body.coffee
@@ -1,33 +0,0 @@
-module.exports = class Body
- @G: 6.6742e-11
-
- # radius, mass - float
- # position, velocity - Vector
- constructor: (@name, @radius, @mass, @position, @velocity) ->
-
- exertPullOn: (otherBody, dt) ->
- # Gravitational pull on itself does not make sense (here at least...)
- return if otherBody is @
- radius = otherBody.position.sub(@position)
- otherBody.updateVelocity radius, @mass, dt
-
- updateVelocity: (r, mass, dt) ->
- sumOfSquares = r.sumOfSquares()
- hypot = Math.sqrt sumOfSquares
- acceleration = -Body.G * mass/sumOfSquares
- @velocity = @velocity.add r.div(hypot).mult(acceleration * dt)
-
- calculateGravitationalPull: (otherBody, dt) ->
- unless otherBody is @
- radius = @position.sub otherBody.position
- radiusSquared = radius.sumOfSquares()
- hyp = Math.sqrt radiusSquared
- acceleration = -Body.G * otherBody.mass/radiusSquared
- @velocity = @velocity.add radius.div(hyp).mult(acceleration * dt)
-
- move: (dt) ->
- @oldPosition = @position
- @position = @position.add @velocity.mult(dt)
-
- distanceToCenter: ->
- Math.sqrt @position.sumOfSquares()
View
35 client/src/main.coffee
@@ -1,35 +0,0 @@
-Timer = require './timer'
-Screen = require './screen'
-Vector = require './vector'
-
-solarSystem = require './solar_system'
-
-module.exports = class Runner
- constructor: ->
- canvas = document.getElementById 'universe'
- @timer = new Timer 60, canvas
- @startStopButton = document.getElementById 'start_stop'
- @planets = solarSystem.activatePlanets ['Sun', 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter'] #[solarSystem.sun, solarSystem.mercury, solarSystem.venus, solarSystem.earth, solarSystem.mars, solarSystem.jupiter, solarSystem.saturn, solarSystem.uranus, solarSystem.neptune, solarSystem.pluto]
- @screen = new Screen canvas, @planets, solarSystem.sun, solarSystem.farthestPlanet().distanceToCenter()
- @dt = 24 * 60 * 60
-
- startStop: =>
- if @timer.running
- @timer.stop()
- else
- @timer.start()
-
- tick: =>
- for planet in @planets
- planet.calculateGravitationalPull otherPlanet, @dt for otherPlanet in @planets
- planet.move @dt for planet in @planets
- @screen.draw @planets
-
- render: ->
- @screen.draw @planets
- @timer.registerCallback @tick
- @startStopButton.onclick = @startStop
-
- @init: ->
- runner = new Runner
- runner.render()
View
45 client/src/screen.coffee
@@ -1,45 +0,0 @@
-Vector = require './vector'
-
-module.exports = class Screen
- constructor: (canvas, bodies, @center, @maxDistance) ->
- @height = canvas.height = canvas.clientHeight
- @width = canvas.width = canvas.clientWidth
- @canvasCenter = new Vector Math.floor(@width/2), Math.floor(@height/2)
- @ctx = canvas.getContext '2d'
- @prerenderedBodies = {}
-
- @prerenderBody body for body in bodies
-
- draw: (bodies) ->
- @ctx.clearRect 0, 0, @width, @height
- @ctx.save()
- @ctx.translate @canvasCenter.x, @canvasCenter.y
- for body in bodies
- @renderBody body
- @ctx.restore()
-
- renderBody: (body) ->
- bodyRadius = @radiusInPixels body.radius
- translatedPosition = @positionInPixels body.position.sub(@center.position)
- translatedPosition = new Vector(Math.floor(translatedPosition.x), Math.floor(translatedPosition.y))
- @ctx.drawImage @prerenderedBodies[body.name], Math.floor(translatedPosition.x) - bodyRadius, Math.floor(translatedPosition.y) - bodyRadius
-
- prerenderBody: (body) ->
- bodyRadius = @radiusInPixels body.radius
- bodyCanvas = document.createElement 'canvas'
- bodyCanvas.width = bodyCanvas.height = bodyRadius*2
- bodyCtx = bodyCanvas.getContext '2d'
- bodyCtx.fillStyle = "rgb(255, 255, 255)"
- bodyCtx.beginPath()
- bodyCtx.arc bodyRadius, bodyRadius, bodyRadius, 0, 2*Math.PI
- bodyCtx.fill()
- @prerenderedBodies[body.name] = bodyCanvas
-
- scale: ->
- Math.ceil @maxDistance/(@height/2 - 10)
-
- positionInPixels: (position) ->
- position.div @scale() if position
-
- radiusInPixels: (radius) ->
- Math.ceil(Math.log(radius)) - 13
View
31 client/src/solar_system.coffee
@@ -1,31 +0,0 @@
-Body = require './body'
-Vector = require './vector'
-
-module.exports =
- sun: new Body("Sun", 695000000, 1.989E+030, new Vector(0,0), new Vector(0,0))
- mercury: new Body("Mercury", 2440000, 3.33E+023, new Vector(0, 57900000000), new Vector(47900, 0))
- venus: new Body("Venus", 6050000, 4.869E+024, new Vector(0, 108000000000), new Vector(35000, 0))
- earth: new Body("Earth", 6378140, 5.976E+024, new Vector(0, 150000000000), new Vector(29800, 0))
- mars: new Body("Mars", 3397200, 6.421E+023, new Vector(0, 227940000000), new Vector(24100, 0))
- jupiter: new Body("Jupiter", 71492000, 1.9E+027, new Vector(0, 778330000000), new Vector(13100, 0))
- saturn: new Body("Saturn", 60268000, 5.688E+026, new Vector(0, 1429400000000), new Vector(9640, 0))
- uranus: new Body("Uranus", 25559000, 8.686E+025, new Vector(0, 2870990000000), new Vector(6810, 0))
- neptune: new Body("Neptune", 24746000, 1.024E+026, new Vector(0, 4504300000000), new Vector(5430, 0))
- pluto: new Body("Pluto", 1137000, 1.27E+022, new Vector(0, 5913520000000), new Vector(4740, 0))
-
- activatePlanets: (list) ->
- @activatedPlanets = list
- @activePlanets()
-
- activePlanets: ->
- (planet for name, planet of @ when @activatedPlanets.indexOf(planet.name) > -1)
-
- farthestPlanet: ->
- @max @activePlanets(), (planet) ->
- Math.sqrt planet.position.sumOfSquares()
-
- max: (list, max) ->
- maxTemp = list[0]
- for planet in list
- maxTemp = planet if max(maxTemp) < max(planet)
- maxTemp
View
38 client/src/timer.coffee
@@ -1,38 +0,0 @@
-requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
- window.webkitRequestAnimationFrame || window.msRequestAnimationFrame
-
-module.exports = class Timer
- constructor: (@fps, @canvas, @debug = false) ->
- @callbacks = []
- @msPrFrame = Math.ceil(1000/@fps)
- @ms = 0
- @ticksDuringLastSecond = 0
- if debug
- @registerCallback @fpsCounter
-
- tick: (timestamp) =>
- delta = timestamp - @lastTickTime
- if delta > @msPrFrame
- @lastTickTime = timestamp
- callback(delta) for callback in @callbacks
- requestAnimationFrame @tick, @canvas if @running
-
- start: ->
- return if @running
- @running = true
- @lastTickTime = Date.now()
- requestAnimationFrame @tick, @canvas
-
- stop: ->
- @running = false
-
- registerCallback: (callback) ->
- @callbacks.push callback
-
- fpsCounter: (delta) =>
- if @ms + delta > 1000
- console.log "FPS: #{@ticksDuringLastSecond}"
- @ms = @ticksDuringLastSecond = 0
- else
- @ms += delta
- @ticksDuringLastSecond += 1
View
32 client/src/vector.coffee
@@ -1,32 +0,0 @@
-module.exports = class Vector
- constructor: (@x, @y) ->
-
- mult: (v) ->
- if @constructor == v.constructor
- new Vector(@x * v.x, @y * v.y)
- else
- new Vector(@x * v, @y * v)
-
- div: (v) ->
- if @constructor == v.constructor
- new Vector @x / v.x, @y / v.y
- else
- new Vector @x / v, @y / v
-
- add: (v) ->
- new Vector @x + v.x, @y + v.y
-
- sub: (v) ->
- new Vector @x - v.x, @y - v.y
-
- sumOfSquares: ->
- @x*@x + @y*@y
-
- eq: (v) ->
- if v
- @x == v.x && @y == v.y
- else
- false
-
- toString: ->
- "(#{@x}, #{@y})"
View
25 client/static/index.html
@@ -1,25 +0,0 @@
-<!DOCTYPE html>
-<html>
- <head>
- <meta charset="utf-8">
- <title>Gravity</title>
- <script src="/application.js" type="text/javascript" charset="utf-8"></script>
- <style type="text/css" media="screen">
- #universe {
- width: 650px;
- height: 650px;
- background: #000;
- -webkit-transform: translateZ(0);
- }
- #universe, .controls {
- float: left;
- }
- </style>
- </head>
- <body onload="require('./lib/main').init();">
- <canvas id="universe"></canvas>
- <div class="controls">
- <button id="start_stop">Start</button>
- </div>
- </body>
-</html>
View
8 package.json
@@ -1,8 +0,0 @@
-{
- "name": "gravity_server",
- "version": "0.0.1-dev",
- "dependencies": {
- "express": "2.5.8",
- "hercules": "0.0.6"
- }
-}
View
23 server.coffee
@@ -1,23 +0,0 @@
-fs = require 'fs'
-path = require 'path'
-{print} = require 'util'
-{spawn} = require 'child_process'
-
-express = require 'express'
-Hercules = require 'hercules'
-
-clientDir = path.join(__dirname, 'client')
-
-coffee = spawn 'coffee', ['-c', '-o', path.join(clientDir, 'lib'), path.join(clientDir, 'src')]
-
-coffee.stderr.on 'data', (data) -> process.stderr.write data.toString()
-coffee.stdout.on 'data', (data) -> print data.toString()
-
-coffee.on 'exit', ->
- bundle = Hercules.bundle clientDir
- app = express.createServer()
- app.get '/application.js', (request, response) ->
- response.send bundle.toString()
- app.use express.static path.join clientDir, 'static'
- app.listen 3000
- console.log "Gravity listening on port 3000"

0 comments on commit b41df14

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