Skip to content

Commit

Permalink
Added file input error handling, setting system constants from file, …
Browse files Browse the repository at this point in the history
…and current time shown on index.html
  • Loading branch information
Elucidation committed Jan 1, 2012
1 parent 796293b commit 6ba7ea6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
27 changes: 26 additions & 1 deletion fileIO.js
Expand Up @@ -23,7 +23,16 @@ function handleFileSelect(evt) {

reader.onload = function(e) {
var text = e.target.result;
jsData = parseInputText(text);
try {
jsData = parseInputText(text);
}
catch(e) {
ERROR = e; // For console access
alert('Couldn\'t Parse JSON from file : ',e.name,' : ',e.message);
console.log('ERROR parsing: ',e.name,' : ',e.message);
return -1;
}

loadSysFromJSON(jsData);
if (DEBUG) {
console.log("JSON FILE LOADED: ",jsData.name)
Expand Down Expand Up @@ -66,7 +75,23 @@ function parseInputText(text) {
var jsObj = JSON.parse(text);
return jsObj;
}

function loadSysFromJSON(jsonData) {
// Constants
MINMASS = typeof(jsData.Constants.MINMASS) != 'undefined' ? jsData.Constants.MINMASS : MINMASS;
MAXMASS = typeof(jsData.Constants.MAXMASS) != 'undefined' ? jsData.Constants.MAXMASS : MAXMASS;
G = typeof(jsData.Constants.G) != 'undefined' ? jsData.Constants.G : G;
GFACTOR = typeof(jsData.Constants.GFACTOR) != 'undefined' ? jsData.Constants.GFACTOR : GFACTOR;
ETA = typeof(jsData.Constants.ETA) != 'undefined' ? jsData.Constants.ETA : ETA;
if (DEBUG) {
console.log('MINMASS: ',MINMASS);
console.log('MAXMASS: ',MAXMASS);
console.log('G: ',G);
console.log('GFACTOR: ',GFACTOR);
console.log('ETA: ',ETA);
}

// Bodies
for (i=0;i<jsData.Bodies.N;i++) {
var b = jsData.Bodies.BodyData[i];
//addBody(x,y,vx,vy,m) {
Expand Down
3 changes: 3 additions & 0 deletions graphics.js
Expand Up @@ -13,6 +13,7 @@ function initGraphics(canvasId,dataId){
canvasElement = document.getElementById(canvasId);
c = canvasElement.getContext("2d");
data = document.getElementById(dataId);
timeDisp = document.getElementById('timeDisp');

//gfxTimer = setInterval(refreshGraphics,1/60.0*1000);

Expand All @@ -26,6 +27,8 @@ function initGraphics(canvasId,dataId){

// Updates text on html page
function updateData() {
timeDisp.value = T.toFixed(2); // Update time output form

data.innerHTML = "";
if (sysRunning) {
data.innerHTML += ("System <b>Running</b>");
Expand Down
20 changes: 8 additions & 12 deletions index.html
Expand Up @@ -17,10 +17,11 @@ <h2>Barnes-Hut Implementation in HTML/Javascript</h2>
Press e/d keys while dragging to change mass of body<br/>
No collision checks
</p>
<div>
<input type="file" id="fileInputs" name="files[]" />
<output id="fileList"></output
</div>
<p><div>
Load Input JSON File:<br/>
<input type="file" id="fileInputs" name="files[]" accept="json"/>
<output id="fileList"></output>
</div></p>

<table>
<tr>
Expand All @@ -37,6 +38,9 @@ <h2>Barnes-Hut Implementation in HTML/Javascript</h2>
</div>
<div>
<table border=0>
<tr><td colspan=3 align="center">
<b> TIME: <output id="timeDisp">0.0</output></b>
</td></tr>
<tr><td colspan=3 align="center">
<b>DT: <span id="dtSliderVal">0.01</span></b>
</td></tr>
Expand All @@ -46,14 +50,6 @@ <h2>Barnes-Hut Implementation in HTML/Javascript</h2>
<td>10</td>
</tr>
</table>
<script type="text/javascript">
setDT(-2);
function setDT(v)
{
dt = Math.pow(10,v);
document.getElementById("dtSliderVal").innerHTML=dt.toFixed(4);
}
</script>
</div>
<div id="data">Nothing here yet.</div>
</td>
Expand Down
6 changes: 3 additions & 3 deletions inputSimple.json
Expand Up @@ -2,15 +2,15 @@
"name":"inputSimple",
"Constants": {
"MINMASS": 1e2,
"MAXMASS": 1e4,
"MAXMASS": 1e5,
"G": 1,
"ETA": 10,
"GFACTOR": 1.3
},
"Bodies": {
"N": 2,
"BodyData": [[1000, 100, 200, 0, 1, 0, 0],
[5000, 300, 200, 0, 0, 0, 0]]
"BodyData": [[1000, 200, 200, 0, 10, 0, 0],
[100000, 300, 200, 0, -1, 0, 0]]

}
}
9 changes: 9 additions & 0 deletions userInput.js
Expand Up @@ -10,6 +10,7 @@ function initUI(canvasId){
if (DEBUG) {
console.log("Initialize UI complete.");
}
setDT(-2);
}

function mouseClick(e) {
Expand Down Expand Up @@ -117,3 +118,11 @@ function doKeyDown(evt){
// break;
}
}
function setDT(v)
{
dt = Math.pow(10,v);
if (DEBUG) {
console.log("DT SET: ",dt);
}
document.getElementById("dtSliderVal").innerHTML=dt.toFixed(4);
}

0 comments on commit 6ba7ea6

Please sign in to comment.