From b9dc2f677e7c3021aeeea6b1ab609a9b40806b48 Mon Sep 17 00:00:00 2001 From: julian laplace Date: Mon, 8 May 2023 23:49:29 +0200 Subject: v1 of relabi generator --- src/lib/util.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/lib/util.js (limited to 'src/lib/util.js') diff --git a/src/lib/util.js b/src/lib/util.js new file mode 100644 index 0000000..750c5b7 --- /dev/null +++ b/src/lib/util.js @@ -0,0 +1,63 @@ +import * as Tone from "tone"; +import StartAudioContext from "./startAudioContext"; + +const isIphone = + navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i); +const isIpad = navigator.userAgent.match(/iPad/i); +const isAndroid = navigator.userAgent.match(/Android/i); +const isMobile = isIphone || isIpad || isAndroid; +const isDesktop = !isMobile; + +document.body.classList.add(isMobile ? "mobile" : "desktop"); + +export const browser = { isIphone, isIpad, isMobile, isDesktop }; +export const choice = (a) => a[Math.floor(Math.random() * a.length)]; +export const mod = (n, m) => n - m * Math.floor(n / m); +export const random = () => Math.random(); +export const rand = (n) => Math.random() * n; +export const randint = (n) => rand(n) | 0; +export const randrange = (a, b) => a + rand(b - a); +export const randsign = () => (random() >= 0.5 ? -1 : 1); +export const randnullsign = () => { + var r = random(); + return r < 0.333 ? -1 : r < 0.666 ? 0 : 1; +}; + +export function requestAudioContext(fn) { + const container = document.createElement("div"); + const button = document.createElement("div"); + button.innerHTML = "Tap to start - please unmute your phone"; + Object.assign(container.style, { + display: "block", + position: "absolute", + width: "100%", + height: "100%", + zIndex: "10000", + top: "0px", + left: "0px", + backgroundColor: "rgba(0, 0, 0, 0.8)", + }); + Object.assign(button.style, { + display: "block", + position: "absolute", + left: "50%", + top: "50%", + padding: "20px", + backgroundColor: "#7F33ED", + color: "white", + fontFamily: "monospace", + borderRadius: "3px", + transform: "translate3D(-50%,-50%,0)", + textAlign: "center", + lineHeight: "1.5", + width: "150px", + }); + container.appendChild(button); + document.body.appendChild(container); + StartAudioContext.setContext(Tone.context); + StartAudioContext.on(button); + StartAudioContext.onStarted((_) => { + container.remove(); + fn(); + }); +} -- cgit v1.2.3-70-g09d2