From 749ee3af1304aabeadabb3e765bcc99549ddd75c Mon Sep 17 00:00:00 2001 From: Ben Plate Date: Fri, 24 Jun 2022 16:43:40 -0700 Subject: [PATCH 1/5] update version number --- Cargo.toml | 2 +- website-src/package-lock.json | 2 +- website-src/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 16e0938..de63414 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "solar-sim" -version = "0.5.1" +version = "0.6.0" authors = ["Ben Plate "] edition = "2018" description = "Physics simulator written in Rust WASM for use in Solar Sim website" diff --git a/website-src/package-lock.json b/website-src/package-lock.json index 0ccabda..52662fb 100644 --- a/website-src/package-lock.json +++ b/website-src/package-lock.json @@ -1,6 +1,6 @@ { "name": "solar-sim-app", - "version": "0.5.1", + "version": "0.6.0", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/website-src/package.json b/website-src/package.json index fb64178..25d15b4 100644 --- a/website-src/package.json +++ b/website-src/package.json @@ -1,6 +1,6 @@ { "name": "solar-sim-app", - "version": "0.5.1", + "version": "0.6.0", "description": "Creates the Solar Sim website.", "main": "index.js", "scripts": { From 80aa6c147d9ed5e29b3f0878f5da840ed4aeed30 Mon Sep 17 00:00:00 2001 From: Ben Plate Date: Fri, 24 Jun 2022 16:44:08 -0700 Subject: [PATCH 2/5] create cookie policy --- website-src/cookiePolicy.html | 69 +++++++++++++++++++++++++++++++++++ website-src/index.html | 16 ++++++++ website-src/webpack.config.js | 1 + 3 files changed, 86 insertions(+) create mode 100644 website-src/cookiePolicy.html diff --git a/website-src/cookiePolicy.html b/website-src/cookiePolicy.html new file mode 100644 index 0000000..bdcf0fd --- /dev/null +++ b/website-src/cookiePolicy.html @@ -0,0 +1,69 @@ + + + + + Lagrange Demonstration - Cookie Policy + + + + +
+

Cookie Policy

+

Last updated: 24 June 2022

+

+ In order to provide a better user experience, this website uses cookies to save data across browsing sessions. + Your use of this website does not imply an acceptance of these cookies and you must manually accept them by checking the box at the top of the landing page. + In doing so, you agree to the complete use of cookies by this website according to this policy as permitted by law. +

+

+ Although this website uses cookies, it does not track personal data that can be used for identification. + The website also does not use any third-party cookies, including tracking technologies such as Google Analytics. + Your cookies will not be sold or provided to any other third parties except where required by law. +

+

+ Should you choose not to accept the use of cookies, you will still be able to use this website with the understanding that some functionality may be limited. +

+

+ A list of all cookies used and their purpose can be found below: +

+ + + + + + + + + + + + + + + + + + + + +
CookieInformationReason
cookiesAcceptedWhether you've accepted cookies on this device.If cookies are enabled, you won't need to enable them again when you revisit the website.
configSavesUser-saved configurations of SolarSimAllows users to save SolarSim configurations across browsing sessions. Without this, users can still save and load configurations, but they will be lost when the webpage is closed.
+
+ Return to SolarSim +
+ + diff --git a/website-src/index.html b/website-src/index.html index 816505a..9c5a6af 100644 --- a/website-src/index.html +++ b/website-src/index.html @@ -7,6 +7,22 @@ +
+ This website optionally uses cookies. A comprehensive policy can be viewed here (updated 24 June 2022). By checking this box, you give permission for using these cookies; you can revoke permissions at any time by unchecking this box. + +

+
Enable Debug (slightly lowers performance) diff --git a/website-src/webpack.config.js b/website-src/webpack.config.js index 586f968..e008f90 100644 --- a/website-src/webpack.config.js +++ b/website-src/webpack.config.js @@ -15,6 +15,7 @@ module.exports = { new CopyPlugin({ patterns: [ "index.html", + "cookiePolicy.html", {from: "static", to: "static"} ] }) From 348d8284fb426512885c301d64231f2ebb58763d Mon Sep 17 00:00:00 2001 From: Ben Plate Date: Fri, 24 Jun 2022 16:44:29 -0700 Subject: [PATCH 3/5] fix bug that deletes too many bodies from sim --- website-src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/website-src/index.js b/website-src/index.js index 92508d6..4590a7f 100644 --- a/website-src/index.js +++ b/website-src/index.js @@ -278,7 +278,7 @@ function step(simulate) { if(debug && count % 10 == 0) tickTime = performance.now(); - const newPositions = SolarSim.get_positions(); + let newPositions = SolarSim.get_positions(); for(let i = 0; i < bodies.length; i++) { let body = bodies[i]; let inBounds = (newPositions[i * 2] >= 0 && newPositions[i * 2] < WIDTH && newPositions[i * 2 + 1] >= 0 && newPositions[i * 2 + 1] < HEIGHT); @@ -294,6 +294,7 @@ function step(simulate) { } bodies.splice(i, 1); trails.splice(i, 1); + newPositions.splice(i * 2, 2); SolarSim.remove_body(i); i--; From 341c0b9ed839cb14b9b92e66946438a59ca7e5b4 Mon Sep 17 00:00:00 2001 From: Ben Plate Date: Tue, 26 Jul 2022 17:28:48 -0700 Subject: [PATCH 4/5] add velocity getter --- rust-src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rust-src/lib.rs b/rust-src/lib.rs index db797d5..5b720c2 100644 --- a/rust-src/lib.rs +++ b/rust-src/lib.rs @@ -130,6 +130,19 @@ pub fn get_positions() -> Array { return out; } +#[wasm_bindgen] +pub fn get_velocities() -> Array { + let uni = UNIVERSE.read().unwrap(); + let out: Array = Array::new(); + + for i in 0..uni.len() { + out.push(&JsValue::from_f64(uni[i].velocity.x)); + out.push(&JsValue::from_f64(uni[i].velocity.y)); + } + + return out; +} + #[wasm_bindgen] pub fn set_simulation_accuracy(time_step: f64, num_sims_per_step: i32) { let mut ts = TIME_STEP.write().unwrap(); From 7e0c163e6d12f371c7622575e52b29eb20a31f03 Mon Sep 17 00:00:00 2001 From: Ben Plate Date: Tue, 26 Jul 2022 17:29:06 -0700 Subject: [PATCH 5/5] add saving/loading configs --- website-src/index.html | 31 ++++++++++-- website-src/index.js | 111 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 129 insertions(+), 13 deletions(-) diff --git a/website-src/index.html b/website-src/index.html index 9c5a6af..500a438 100644 --- a/website-src/index.html +++ b/website-src/index.html @@ -14,10 +14,9 @@ document.getElementById("cookieAcceptance").addEventListener("change", function() { if (this.checked) { - document.cookie = "cookiesAccepted=true;max-age=315360000;secure;SameSite=Strict"; + document.cookie = "cookiesAccepted=true;max-age=315360000;SameSite=Strict"; } else { - document.cookie = "cookiesAccepted=;max-age=0;secure;SameSite=Strict"; - document.cookie = "configSaves=;max-age=0;secure;SameSite=Strict"; + document.cookie = "cookiesAccepted=;max-age=0;SameSite=Strict"; } }); @@ -25,7 +24,7 @@ - Enable Debug (slightly lowers performance) + Enable Debug (May need to enable precise timers in browser)
# of bodies: 6 Simulation tick time: 10μs @@ -61,6 +60,30 @@
+
+ +
+ + + + + +
+
Saves list:
+ +
diff --git a/website-src/index.js b/website-src/index.js index 4590a7f..4993a1d 100644 --- a/website-src/index.js +++ b/website-src/index.js @@ -3,13 +3,15 @@ import * as SolarSim from "solar-sim"; const WIDTH = 1280; const HEIGHT = 720; +let cookesAccepted = () => document.cookie.length > 0; + let bodies = [ { name: "Sun", mass: 5e12, radius: 32, position: [WIDTH / 2, HEIGHT / 2], - initialVelocity: [0, 0], + velocity: [0, 0], color: "#c8c800", }, { @@ -17,7 +19,7 @@ let bodies = [ mass: 1e10, radius: 10, position: [WIDTH / 2 + 160, HEIGHT / 2], - initialVelocity: [0, 1.444169311403618], + velocity: [0, 1.444169311403618], color: "#0064c8", }, { @@ -25,7 +27,7 @@ let bodies = [ mass: 1, radius: 4, position: [WIDTH / 2 + 174.3684756886812, HEIGHT / 2], - initialVelocity: [0, 1.57386005], + velocity: [0, 1.57386005], color: "#969696", }, { @@ -33,7 +35,7 @@ let bodies = [ mass: 1, radius: 4, position: [WIDTH / 2 + 146.4437229236931, HEIGHT / 2], - initialVelocity: [0, 1.321809565], + velocity: [0, 1.321809565], color: "#969696", }, { @@ -41,7 +43,7 @@ let bodies = [ mass: 1, radius: 4, position: [WIDTH / 2 - 159.813705852, HEIGHT / 2], - initialVelocity: [0, -1.444169311403618], + velocity: [0, -1.444169311403618], color: "#4b964b", }, { @@ -49,7 +51,7 @@ let bodies = [ mass: 1, radius: 4, position: [WIDTH / 2 + 80, HEIGHT / 2 + (Math.sqrt(3)/2) * 160], - initialVelocity: [-1.25180591705918, 0.7227304831872841], + velocity: [-1.25180591705918, 0.7227304831872841], color: "#4b964b", }, { @@ -57,7 +59,7 @@ let bodies = [ mass: 1, radius: 4, position: [WIDTH / 2 + 80, HEIGHT / 2 - (Math.sqrt(3)/2) * 160], - initialVelocity: [1.25180591705918, 0.7227304831872841], + velocity: [1.25180591705918, 0.7227304831872841], color: "#4b964b", }, ]; @@ -74,7 +76,7 @@ let trails = [ for(let i = 0; i < bodies.length; i++) { let body = bodies[i]; - SolarSim.add_body(body.mass, body.position[0], body.position[1], body.initialVelocity[0], body.initialVelocity[1]); + SolarSim.add_body(body.mass, body.position[0], body.position[1], body.velocity[0], body.velocity[1]); } function addBody(name, mass, radius, positionX, positionY, velocityX, velocityY) { @@ -94,7 +96,7 @@ function addBody(name, mass, radius, positionX, positionY, velocityX, velocityY) mass: mass, radius: radius, position: [positionX, positionY], - initialVelocity: [velocityX, velocityY], + velocity: [velocityX, velocityY], color: "#" + red + green + blue, }); @@ -279,6 +281,7 @@ function step(simulate) { if(debug && count % 10 == 0) tickTime = performance.now(); let newPositions = SolarSim.get_positions(); + let newVelocities = SolarSim.get_velocities(); for(let i = 0; i < bodies.length; i++) { let body = bodies[i]; let inBounds = (newPositions[i * 2] >= 0 && newPositions[i * 2] < WIDTH && newPositions[i * 2 + 1] >= 0 && newPositions[i * 2 + 1] < HEIGHT); @@ -286,6 +289,8 @@ function step(simulate) { body.position[0] = newPositions[i * 2]; body.position[1] = newPositions[i * 2 + 1]; + body.velocity[0] = newVelocities[i * 2]; + body.velocity[1] = newVelocities[i * 2 + 1]; if(!inSimBounds) { ctx2.fillStyle = "black"; @@ -367,3 +372,91 @@ spawnButton.addEventListener("click", (elem, e) => { }) window.requestAnimationFrame(step); + +let saves = {}; + +let saveNameField = document.getElementById("saveName"); +let saveMessageField = document.getElementById("saveMessage"); +let savesList = document.getElementById("savesList"); +if(cookesAccepted()) { + let saveString = document.cookie.split("; ").find(cookie => cookie.startsWith("configSaves=")); + if(saveString) { + saves = JSON.parse(atob(saveString.split("=")[1])); + } + + savesList.innerHTML = ""; + for(let i = 0; i < Object.keys(saves).length; i++) { + savesList.innerHTML += Object.keys(saves)[i] + "
"; + } +} +document.getElementById("cookieAcceptance").addEventListener("click", (elem, e) => { + if(document.getElementById("cookieAcceptance").checked) { + document.cookie = "configSaves=" + btoa(JSON.stringify(saves)) + ";max-age=315360000;SameSite=Strict"; + } else { + document.cookie = "configSaves=;max-age=0;SameSite=Strict"; + } +}); + +let saveButton = document.getElementById("saveButton"); +saveButton.addEventListener("click", (elem, e) => { + if(saveNameField.value.length == 0) return; + + saves[saveNameField.value] = structuredClone(bodies); + if(cookesAccepted()) { + document.cookie = "configSaves=" + btoa(JSON.stringify(saves)) + ";max-age=315360000;SameSite=Strict"; + } + + savesList.innerHTML = ""; + for(let i = 0; i < Object.keys(saves).length; i++) { + savesList.innerHTML += Object.keys(saves)[i] + "
"; + } + saveMessageField.innerHTML = "Saved as " + saveNameField.value; + setTimeout(() => saveMessageField.innerHTML = "", 3000); +}); + +let loadButton = document.getElementById("loadButton"); +loadButton.addEventListener("click", (elem, e) => { + let saveName = document.getElementById("saveName").value; + console.log(saves); + if(saveName in saves) { + + ctx.clearRect(0, 0, WIDTH, HEIGHT); + ctx2.clearRect(0, 0, WIDTH, HEIGHT); + + bodies = structuredClone(saves[saveName]); + + trails = [] + for(let i = 0; i < bodies.length; i++) { + trails.push([]); + } + + SolarSim.clear_universe(); + for(let i = 0; i < bodies.length; i++) { + SolarSim.add_body(bodies[i].mass, bodies[i].position[0], bodies[i].position[1], bodies[i].velocity[0], bodies[i].velocity[1]); + } + + step(false); + + saveMessageField.innerHTML = "Loaded " + saveName; + } else { + saveMessageField.innerHTML = "No save with that name"; + } + + setTimeout(() => saveMessageField.innerHTML = "", 3000); +}); + +let clearButton = document.getElementById("clearButton"); +clearButton.addEventListener("click", (elem, e) => { + if(clearButton.innerHTML === "Are you sure?") { + saves = {}; + if(cookesAccepted()) { + document.cookie = "configSaves=;max-age=0;SameSite=Strict"; + } + + saveMessageField.innerHTML = "Cleared saves"; + savesList.innerHTML = ""; + } else { + clearButton.innerHTML = "Are you sure?"; + setTimeout(() => clearButton.innerHTML = "Clear", 3000); + } +}); \ No newline at end of file