From 0e1352203bd52d67168f9b8cc8ad9a6726862237 Mon Sep 17 00:00:00 2001 From: Evgenii Neumerzhitckii Date: Sat, 1 Aug 2020 14:46:23 +1000 Subject: [PATCH] Docs --- js/graphics.js | 2 ++ js/main.js | 1 + js/params.js | 3 --- js/refresh_rate.js | 1 + js/rotate_on_touch.js | 6 ++++++ js/share.js | 17 +++++++++++++++++ js/show_fps.js | 1 + js/simulation.js | 15 ++++++++++++++- js/sliders.js | 8 ++++++++ js/trajectories.js | 1 + js/user_input.js | 3 ++- js/zoom.js | 6 ++++++ 12 files changed, 59 insertions(+), 5 deletions(-) diff --git a/js/graphics.js b/js/graphics.js index 5dafad8..7d89e65 100644 --- a/js/graphics.js +++ b/js/graphics.js @@ -6,6 +6,7 @@ import m4 from './simulation/m4.js'; import { numberOfStarsInAllRingsOneGalaxy, totalNumberOfBodies } from './simulation/initial_conditions.js'; + // Adjust the size of the drawing region (canvas) based on the size of // the web browser window function fitToContainer(drawData){ @@ -190,6 +191,7 @@ export function loadColors(drawData, initialParams) { gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW); } + /** * Load star star sizes into the GPU buffer * diff --git a/js/main.js b/js/main.js index 438d679..117d6eb 100644 --- a/js/main.js +++ b/js/main.js @@ -10,6 +10,7 @@ import {init as initUserInput} from './user_input.js'; import * as showFps from './show_fps.js'; import { updateCameraDistance } from './zoom.js'; + /** * Calculate positions of stars at the next time stamp and redraw the * stars on screen. This function is called at each frame of the animation, diff --git a/js/params.js b/js/params.js index 9765ee8..c2f72ac 100644 --- a/js/params.js +++ b/js/params.js @@ -5,8 +5,6 @@ import m4 from './simulation/m4.js'; import * as simulation from './simulation.js'; - - /** * Return initial parameters of the simulation, they can't be changed without * restart (except for masses) @@ -30,7 +28,6 @@ export function getInitialParameters() { } - /** * Return parameters that can change during the simulation. * diff --git a/js/refresh_rate.js b/js/refresh_rate.js index 5940406..5ed6a9e 100644 --- a/js/refresh_rate.js +++ b/js/refresh_rate.js @@ -43,6 +43,7 @@ function measure(measurements) { return Math.round(1000 / medium); } + /** * Stores time of the animation frame. The method is called multiple time * for each animation frame until the number of `tries` is exceeded. diff --git a/js/rotate_on_touch.js b/js/rotate_on_touch.js index 6bded74..d41510a 100644 --- a/js/rotate_on_touch.js +++ b/js/rotate_on_touch.js @@ -2,12 +2,14 @@ import m4 from './simulation/m4.js'; + function startMoving(state, e) { state.moving = true; state.lastPosition = [e.pageX, e.pageY]; if (state.didStartRotating) state.didStartRotating(); } + function startTouching(state, e) { if (e.targetTouches.length === 2) { // Touching with two fingers. @@ -19,6 +21,7 @@ function startTouching(state, e) { if (e.targetTouches.length === 1) startMoving(state, e.targetTouches[0]); } + function move(state, currentParams, e) { if (!state.moving) return; if (!state.lastPosition) return; @@ -43,6 +46,7 @@ function move(state, currentParams, e) { state.lastPosition = [e.pageX, e.pageY]; } + function touchMove(state, currentParams, e) { if (e.targetTouches.length === 2) { // Touching with two fingers. @@ -56,11 +60,13 @@ function touchMove(state, currentParams, e) { } } + function stopMoving(state) { state.moving = false; if (state.didStopRotating) state.didStopRotating(); } + /** * Start detecting rotation of the scene. * diff --git a/js/share.js b/js/share.js index b525a52..152791e 100644 --- a/js/share.js +++ b/js/share.js @@ -14,6 +14,7 @@ export function readFloat(str) { return parsed; } + /** * Parse comma separated values into an array of floats. * @@ -24,6 +25,7 @@ export function readArrayOfFloats(str) { return readArrayOfNumbers(str, parseFloat); } + /** * Parse comma separated values into an array of integers. * @@ -34,6 +36,7 @@ export function readArrayOfInts(str) { return readArrayOfNumbers(str, parseInt); } + /** * Parse comma separated values into an array of integers. * @@ -52,6 +55,7 @@ export function readArrayOfNumbers(str, parseFn) { return parsed; } + /** * Rounds a float number of given number of decimal places * Source: https://stackoverflow.com/a/56632526/297131 @@ -61,10 +65,12 @@ export function roundN(value, digits) { return (Math.round(value * tenToN)) / tenToN; } + export function roundFloat(decimalPlaces) { return (value) => roundN(value, decimalPlaces); } + export function roundArray(decimalPlaces) { return (arr) => arr.map((a) => roundN(a, decimalPlaces)); } @@ -93,6 +99,7 @@ let sharedInitialParams = { } }; + // The keys are names of current parameters that can be shared. // The values are functions for parsing string value from URL. // The values are: @@ -113,6 +120,7 @@ let sharedCurrentParams = { } }; + /** * Returns the full URL to the simulation for sharing containing * all the selected parameters @@ -123,6 +131,7 @@ export function getShareURL(initialParams, currentParams) { return `${urlStart}?${urlParams}`; } + /** * Returns the last part of the URL containing the parameters */ @@ -146,6 +155,7 @@ export function filterInitialParams(initialParams) { return filterParams(sharedInitialParams, initialParams); } + /** * Keeps only permitted current parameters that will be used for sharing. * @@ -156,6 +166,7 @@ export function filterCurrentParams(currentParams) { return filterParams(sharedCurrentParams, currentParams); } + /** * Keeps only parameters permitted for sharing. * @@ -176,6 +187,7 @@ function filterParams(sharedParams, allParams) { return filtered; } + export function prepareParamsForSharing(params, sharedParams) { let result = {}; @@ -195,10 +207,12 @@ export function prepareParamsForSharing(params, sharedParams) { return result; } + function getCurrentUrlWithoutParameters() { return location.protocol + '//' + location.host + location.pathname; } + /** * Get the initial parameters from the URL string. * @@ -209,6 +223,7 @@ export function getSharedInitialParameters(defaultParams) { return getSharedInitialParametersFromUrl(location.search, defaultParams); } + /** * Get the initial parameters from the URL string. * @@ -221,6 +236,7 @@ export function getSharedInitialParametersFromUrl(urlParams, defaultParams) { return getParametersFromUrl(urlParams, defaultParams, sharedInitialParams); } + /** * Get the current parameters from the URL string. * @@ -231,6 +247,7 @@ export function getSharedCurrentParameters(defaultParams) { return getSharedCurrentParametersFromUrl(location.search, defaultParams); } + /** * Get the current parameters from the URL string. * diff --git a/js/show_fps.js b/js/show_fps.js index 388e5a7..bca9d11 100644 --- a/js/show_fps.js +++ b/js/show_fps.js @@ -1,5 +1,6 @@ // Shows current FPS (frames per second) to check the performance of the system + export function init() { var state = { // The time stamp of the previous frame diff --git a/js/simulation.js b/js/simulation.js index a028073..27af8e5 100644 --- a/js/simulation.js +++ b/js/simulation.js @@ -1,10 +1,14 @@ -// Get positions of stars from the physics simulation +// Calculate positions of stars using the physics simulation import * as trajectories from './trajectories.js'; import * as init from './simulation/initial_conditions.js'; import getAccelerations from './simulation/acceleration.js'; import integrateOneStep from './simulation/integrator.js'; + +/** + * Calculate initial positions of stars + */ export function setInitial(initialParams, currentParams) { var { positions, velocities } = init.allPositionsAndVelocities(initialParams); @@ -19,6 +23,10 @@ export function setInitial(initialParams, currentParams) { currentParams.velocities = velocities; } + +/** + * Evolves the simulation by given number of seconds (fastForwardSeconds). + */ function fastForward(initialParams, currentParams) { var timeSteps = Math.round(Math.abs(currentParams.fastForwardSeconds * currentParams.screenRefreshRateFPS)); @@ -38,6 +46,10 @@ function fastForward(initialParams, currentParams) { } } + +/** + * Evolves the simulation by one time step. + */ export function update(initialParams, currentParams) { if (currentParams.rotating) return; @@ -59,6 +71,7 @@ export function update(initialParams, currentParams) { trajectories.update(currentParams.trajectoriesState, currentParams.positions); } + /** * Calculate the time step given the screen refresh rate. * The point of this is to make the speed of the simulation independent diff --git a/js/sliders.js b/js/sliders.js index c43de66..d219477 100644 --- a/js/sliders.js +++ b/js/sliders.js @@ -2,12 +2,14 @@ import SickSlider from './sick_slider.js'; + function didChangeTimeStep(currentParams) { return function(value, position) { currentParams.timeStep = value; }; } + function didChangeRings(initialParams, currentParams, galaxyIndex, onRestart) { return function(value, position) { initialParams.numberOfRings[galaxyIndex] = value; @@ -22,6 +24,7 @@ function didChangeRings(initialParams, currentParams, galaxyIndex, onRestart) { }; } + function didChangeMass(initialParams, currentParams, galaxyIndex, onRestart) { return function(value, position) { initialParams.masses[galaxyIndex] = value; @@ -36,6 +39,7 @@ function didChangeMass(initialParams, currentParams, galaxyIndex, onRestart) { }; } + function didChangeDistance(initialParams, currentParams, onRestart) { return function(value, position) { initialParams.minimalGalaxySeparation = value; @@ -50,6 +54,7 @@ function didChangeDistance(initialParams, currentParams, onRestart) { }; } + function didChangeEccentricity(initialParams, currentParams, onRestart) { return function(value, position) { initialParams.eccentricity = value; @@ -64,6 +69,7 @@ function didChangeEccentricity(initialParams, currentParams, onRestart) { }; } + function didChangeAngle(initialParams, currentParams, galaxyIndex, onRestart) { return function(value, position) { initialParams.galaxyInclinationAnglesDegree[galaxyIndex] = value; @@ -78,6 +84,7 @@ function didChangeAngle(initialParams, currentParams, galaxyIndex, onRestart) { }; } + function didChangeRingSeparation(initialParams, currentParams, onRestart) { return function(value, position) { initialParams.ringSeparation = value; @@ -92,6 +99,7 @@ function didChangeRingSeparation(initialParams, currentParams, onRestart) { }; } + export function setupSlider(initialParams, currentParams, onRestart) { SickSlider(".TwoGalaxies-sliderTimeStep", { label: 'Time step: ', diff --git a/js/trajectories.js b/js/trajectories.js index 6c3be8c..c5cbdbf 100644 --- a/js/trajectories.js +++ b/js/trajectories.js @@ -1,5 +1,6 @@ // Store the trajectories of the two galaxy cores + /** * Store initial trajectories * diff --git a/js/user_input.js b/js/user_input.js index 98a2e4d..c12f079 100644 --- a/js/user_input.js +++ b/js/user_input.js @@ -1,10 +1,11 @@ -// Taking input from the user: buttons clicks, zoom, rotation. +// Taking input from the user: buttons clicks, zoom, rotation import * as rotate from './rotate_on_touch.js'; import * as zoom from './zoom.js'; import * as sliders from './sliders.js'; import * as buttons from './buttons.js'; + export function init(drawData, initialParams, currentParams, onRestart) { sliders.setupSlider(initialParams, currentParams, onRestart); buttons.init(initialParams, currentParams); diff --git a/js/zoom.js b/js/zoom.js index a659de3..c9c499d 100644 --- a/js/zoom.js +++ b/js/zoom.js @@ -1,12 +1,14 @@ // Zoom the stars in and out by using the mouse scroll wheel // or pinch gesture + function distanceBetweenFingers(e) { return Math.hypot( e.targetTouches[0].pageX - e.targetTouches[1].pageX, e.targetTouches[0].pageY - e.targetTouches[1].pageY); } + function onWheel(state, e, currentParams) { e.preventDefault(); currentParams.cameraDistance += e.deltaY / 100 * currentParams.cameraDistance; @@ -20,6 +22,7 @@ function onWheel(state, e, currentParams) { } } + function startTouch(state, e) { state.touching = true; if (e.targetTouches.length !== 2) return; @@ -27,6 +30,7 @@ function startTouch(state, e) { state.lastDistance = distanceBetweenFingers(e); } + function touchMove(state, e, currentParams) { if (!state.touching) return; if (!state.lastDistance) return; @@ -47,6 +51,7 @@ function touchMove(state, e, currentParams) { } } + function stopTouch(state) { state.touching = false; } @@ -79,6 +84,7 @@ export function updateCameraDistance(currentParams) { currentParams.cameraDistance = zDistance; } + /** * Start detecting zoom. *