summaryrefslogtreecommitdiff
path: root/frontend/_rigidbody/program_common.js
diff options
context:
space:
mode:
authorMatthew Kosarek <mattkae@protonmail.com>2021-02-06 18:39:48 -0500
committerMatthew Kosarek <mattkae@protonmail.com>2021-02-06 18:39:48 -0500
commit376e1a7f9207fffb1ec3027ac1e7f32db5de4922 (patch)
tree8bc6c381a10d8b62ff33cdcff6daa1d17cee0f9a /frontend/_rigidbody/program_common.js
Initial commit
Diffstat (limited to 'frontend/_rigidbody/program_common.js')
-rw-r--r--frontend/_rigidbody/program_common.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/frontend/_rigidbody/program_common.js b/frontend/_rigidbody/program_common.js
new file mode 100644
index 0000000..9d097e2
--- /dev/null
+++ b/frontend/_rigidbody/program_common.js
@@ -0,0 +1,72 @@
+/// <reference path="shader.js" />
+/// <reference path="mat4.js" />
+
+function getContext(pId, pOnRun) {
+ const lCanvas = $(pId).find('canvas'),
+ lPlayButton = $(pId).find('.play_button'),
+ lStopButton = $(pId).find('.stop_button'),
+ lGl = lCanvas[0].getContext('webgl'),
+ lWidth = lCanvas.width(),
+ lHeight = lCanvas.height();
+
+ return {
+ canvas: lCanvas,
+ playButton: lPlayButton,
+ stopButton: lStopButton,
+ gl: lGl,
+ width: lWidth,
+ height: lHeight,
+ perspective: orthographic(0, lWidth, 0, lHeight),
+ load: function() {
+ lPlayButton.empty().append($('<div>').addClass('spin-loader'));
+ return loadOrthographicShader(lGl).then(function(pProgramInfo) {
+ lPlayButton.css('display', 'none');
+ lStopButton.css('display', 'block');
+ return pProgramInfo;
+ });
+ },
+ reset: function() {
+ lPlayButton.css('display', 'block');
+ lPlayButton.empty().text('Play');
+ lStopButton.css('display', 'none');
+ lStopButton.on('click', undefined);
+ }
+ };
+}
+
+function requestUpdateLoop(pFunction, pOnExit) {
+ let lDeltaTimeSeconds = undefined,
+ lStartTimeSeconds = undefined,
+ lIsRunning = true;
+
+ function update(pTimeStamp) {
+ if (!lIsRunning) {
+ if (pOnExit) {
+ pOnExit();
+ }
+ return;
+ }
+
+ pTimeStamp = pTimeStamp / 1000.0; // Convert to seconds
+
+ // Time calculation
+ if (lStartTimeSeconds === undefined) {
+ lStartTimeSeconds = pTimeStamp;
+ lDeltaTimeSeconds = 0;
+ } else {
+ lDeltaTimeSeconds = lStartTimeSeconds - pTimeStamp;
+ lStartTimeSeconds = pTimeStamp;
+ }
+
+ pFunction(lDeltaTimeSeconds);
+ requestAnimationFrame(update);
+ }
+
+ requestAnimationFrame(update);
+
+ function lExit() {
+ lIsRunning = false;
+ }
+
+ return lExit;
+} \ No newline at end of file