diff options
Diffstat (limited to '2d/softbody/softbody_1.html.content')
-rw-r--r-- | 2d/softbody/softbody_1.html.content | 79 |
1 files changed, 77 insertions, 2 deletions
diff --git a/2d/softbody/softbody_1.html.content b/2d/softbody/softbody_1.html.content index 9e48383..4f66ce3 100644 --- a/2d/softbody/softbody_1.html.content +++ b/2d/softbody/softbody_1.html.content @@ -1,6 +1,7 @@ <script src="./softbody_1/dist/output.js"></script> <script> window.onload = function() { + // -- Play/Stop Logic function addButtonListener(pPlay, pStop, pDisableElementList) { var lPlayElement = document.getElementById(pPlay), lStopElement = document.getElementById(pStop); @@ -29,6 +30,66 @@ addButtonListener('gl_canvas_play_damped', 'gl_canvas_stop_damped', [ ]); + + // -- Slider logic + var sliderList = document.querySelectorAll('input[type=range]'), + attachListener = function(element) { + var output = element.nextElementSibling; + var updateOutput = function() { + var value = Number(element.value); + output.innerHTML = value; + } + + element.addEventListener('input', updateOutput); + element.addEventListener('change', updateOutput); + updateOutput(); + }; + + for(var i = 0; i < sliderList.length; i++) { + attachListener(sliderList[i]); + } + + // -- Add callbacks for sliders + Module.onRuntimeInitialized = function() { + var Undamped_SetLength = Module.cwrap('Undamped_SetLength', 'void', ['number']), + Undamped_SetDisplacement = Module.cwrap('Undamped_SetDisplacement', 'void', ['number']), + Undamped_SetK = Module.cwrap('Undamped_SetK', 'void', ['number']), + Undamped_SetMass = Module.cwrap('Undamped_SetMass', 'void', ['number']), + lengthSlider = document.getElementById('undamped_spring_length'), + displacementSlider = document.getElementById('undamped_start_position'), + kSlider = document.getElementById('undamped_spring_constant'), + massSlider = document.getElementById('undamped_spring_mass'), + setLength = function(value) { + value = Number(value); + Undamped_SetLength(value); + + var currentDisplacementValue = displacementSlider.value; + var bound = value / 2.0; + displacementSlider.setAttribute('max', bound); + displacementSlider.setAttribute('min', -bound); + + if (currentDisplacementValue < -bound) currentDisplacementValue = -bound; + else if (currentDisplacementValue > bound) currentDisplacementValue = bound; + + var event = new Event('change'); + displacementSlider.value = currentDisplacementValue; + displacementSlider.dispatchEvent(event); + }, + setDisplacement = function(value) { + Undamped_SetDisplacement(value); + }, + setK = function(value) { + Undamped_SetK(value); + }, + setMass = function(mass) { + Undamped_SetMass(mass); + }; + + lengthSlider.addEventListener('change', function(event) { setLength(Number(event.target.value)); }); + displacementSlider.addEventListener('change', function(event) { setDisplacement(Number(event.target.value)); }); + kSlider.addEventListener('change', function(event) { setK(Number(event.target.value)); }); + massSlider.addEventListener('change', function(event) { setMass(Number(event.target.value)); }); + }; } </script> @@ -51,12 +112,26 @@ <p> <span class='widget_container'> <label for='undamped_spring_length'>Spring Length (m)</label> - <input type='range' id='undamped_spring_length'/> + <input type='range' id='undamped_spring_length' min="50" max="300" value="150"/> + <span></span> </span> <span class='widget_container'> <label for='undamped_start_position'>Start Displacement (m)</label> - <input type='range' id='undamped_start_position'/> + <input type='range' id='undamped_start_position' min='-75' max='75' value='0'/> + <span></span> + </span> + + <span class='widget_container'> + <label for='undamped_spring_constant'>Spring Constant (N / m)</label> + <input type='range' id='undamped_spring_constant' min='0.1' max='5.0' value='1.0' step='0.1'/> + <span></span> + </span> + + <span class='widget_container'> + <label for='undamped_spring_mass'>Mass (kg)</label> + <input type='range' id='undamped_spring_mass' min='0.1' max='10.0' value='1.0' step='0.1'/> + <span></span> </span> </p> |