blob: be8e4a20ba9a55da5cd5c0e712e86508dea1ad6d [file] [log] [blame]
Kevin Lubickd1c6cc12021-01-21 11:41:01 -05001/**
2 * This file houses miscellaneous helper functions and constants.
3 */
4
5var nullptr = 0; // emscripten doesn't like to take null as uintptr_t
6
7
8function radiansToDegrees(rad) {
9 return (rad / Math.PI) * 180;
10}
11
12function degreesToRadians(deg) {
13 return (deg / 180) * Math.PI;
14}
15
16// See https://stackoverflow.com/a/31090240
17// This contraption keeps closure from minifying away the check
18// if btoa is defined *and* prevents runtime 'btoa' or 'window' is not defined.
19// Defined outside any scopes to make it available in all files.
20var isNode = !(new Function('try {return this===window;}catch(e){ return false;}')());
21
22function almostEqual(floata, floatb) {
23 return Math.abs(floata - floatb) < 0.00001;
24}
25
26function saveBytesToFile(bytes, fileName) {
27 if (!isNode) {
28 // https://stackoverflow.com/a/32094834
29 var blob = new Blob([bytes], {type: 'application/octet-stream'});
30 url = window.URL.createObjectURL(blob);
31 var a = document.createElement('a');
32 document.body.appendChild(a);
33 a.href = url;
34 a.download = fileName;
35 a.click();
36 // clean up after because FF might not download it synchronously
37 setTimeout(function() {
38 URL.revokeObjectURL(url);
39 a.remove();
40 }, 50);
41 } else {
42 var fs = require('fs');
43 // https://stackoverflow.com/a/42006750
44 // https://stackoverflow.com/a/47018122
45 fs.writeFile(fileName, new Buffer(bytes), function(err) {
46 if (err) throw err;
47 });
48 }
49}