Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Add a network manager and give notification when changing offline/online status. Show toast message and add a CSS file that can change things

Scott Petrovicc63487fedca2
diff --git a/src/Mesh2MotionEngine.ts b/src/Mesh2MotionEngine.ts
index d7b11ae..f28c385 100644
--- a/src/Mesh2MotionEngine.ts
+++ b/src/Mesh2MotionEngine.ts
@@ -38,6 +38,7 @@ import { SceneEnvironmentManager } from './lib/SceneEnvironmentManager.ts'
import { CameraShake } from './lib/CameraShake.ts'
import { DOMUtilities } from './lib/DOMUtilities.ts'
import { PlatformManager } from './lib/PlatformManager.ts'
+import { NetworkStatusManager } from './lib/NetworkStatusManager.ts'
export class Mesh2MotionEngine {
public readonly camera = Generators.create_camera()
@@ -85,6 +86,7 @@ export class Mesh2MotionEngine {
// this will add a platform CSS file if we are running our desktop app
new PlatformManager().init();
+ new NetworkStatusManager();
this.eventListeners = new EventListeners(this)
// helps resolve requestAnimationFrame calling animate() with wrong context
diff --git a/src/create.html b/src/create.html
index e9f95ec..1c4cb40 100644
--- a/src/create.html
+++ b/src/create.html
@@ -22,6 +22,7 @@
/>
<link rel="stylesheet" href="./dialog.css" crossorigin="anonymous" />
<link rel="stylesheet" href="./settings-dropdown.css" crossorigin="anonymous" />
+ <link rel="stylesheet" href="./offline.css" crossorigin="anonymous" />
<nav>
diff --git a/src/index.html b/src/index.html
index eae9928..22a8f24 100644
--- a/src/index.html
+++ b/src/index.html
@@ -18,6 +18,7 @@
<link rel="stylesheet" href="./dialog.css" crossorigin="anonymous" />
<link rel="stylesheet" href="./settings-dropdown.css" crossorigin="anonymous" />
<link rel="stylesheet" href="./marketing/marketing.css" crossorigin="anonymous" />
+ <link rel="stylesheet" href="./offline.css" crossorigin="anonymous" />
<nav>
<div style="display: inline-flex; align-items: center;">
diff --git a/src/lib/NetworkStatusManager.ts b/src/lib/NetworkStatusManager.ts
new file mode 100644
index 0000000..5cfa4af
--- /dev/null
+++ b/src/lib/NetworkStatusManager.ts
@@ -0,0 +1,62 @@
+// test going offline and online through the console like this
+// window.dispatchEvent(new Event('offline')) // shows "You are offline"
+// window.dispatchEvent(new Event('online')) // shows "Back online"
+
+// The developer tools also have an offline mode (by the "throttling option" in network tab)
+// for testing
+
+
+/**
+ * Manages network status and displays a toast notification when the status changes.
+ */
+export class NetworkStatusManager {
+
+ private toast_timeout: ReturnType<typeof setTimeout> | null = null
+
+ constructor() {
+ this.update(navigator.onLine, false)
+ window.addEventListener('online', () => this.update(true, true))
+ window.addEventListener('offline', () => this.update(false, true))
+ }
+
+ public is_online(): boolean {
+ return navigator.onLine
+ }
+
+ private update(online: boolean, show_toast: boolean): void {
+ if (online) {
+ delete document.documentElement.dataset.networkStatus
+ } else {
+ document.documentElement.dataset.networkStatus = 'offline'
+ }
+
+ if (show_toast) {
+ this.show_toast(online ? 'You are back online' : 'You are offline')
+ }
+ }
+
+ private show_toast(message: string): void {
+ if (this.toast_timeout !== null) {
+ clearTimeout(this.toast_timeout)
+ this.toast_timeout = null
+ }
+
+ let toast = document.getElementById('network-status-toast')
+
+ if (toast === null) {
+ toast = document.createElement('div')
+ toast.id = 'network-status-toast'
+ document.body.appendChild(toast)
+ }
+
+ toast.textContent = message
+ toast.classList.remove('network-toast-hide')
+ toast.classList.add('network-toast-show')
+
+ this.toast_timeout = setTimeout(() => {
+ toast!.classList.remove('network-toast-show')
+ toast!.classList.add('network-toast-hide')
+ this.toast_timeout = null
+ }, 5000)
+ }
+}
diff --git a/src/offline.css b/src/offline.css
new file mode 100644
index 0000000..63f4668
--- /dev/null
+++ b/src/offline.css
@@ -0,0 +1,33 @@
+/* Styles applied when the browser has no network connection.
+ Target elements using: [data-network-status="offline"] .your-selector { ... } */
+
+[data-network-status="offline"] #logo-branding {
+ display: none;
+}
+
+/* --- Network status toast --- */
+
+#network-status-toast {
+ position: fixed;
+ top: 5rem;
+ left: 50%;
+ transform: translateX(-50%);
+ padding: 0.5rem 1.25rem;
+ border-radius: 0.25rem;
+ font-size: 1.2rem;
+ font-weight: 500;
+ pointer-events: none;
+ z-index: 9999;
+ opacity: 0;
+ transition: opacity 0.3s ease;
+ background: #8ce077;
+ color: #0a4d4d;
+}
+
+#network-status-toast.network-toast-show {
+ opacity: 1;
+}
+
+#network-status-toast.network-toast-hide {
+ opacity: 0;
+}
diff --git a/src/retarget/index.html b/src/retarget/index.html
index 923cc24..0ab8dd1 100644
--- a/src/retarget/index.html
+++ b/src/retarget/index.html
@@ -13,6 +13,7 @@
<link rel="stylesheet" href="./retarget.css" crossorigin="anonymous" />
<link rel="stylesheet" href="../dialog.css" crossorigin="anonymous" />
+ <link rel="stylesheet" href="../offline.css" crossorigin="anonymous" />
</head>