privateuserh commited on
Commit
74e7a9d
·
verified ·
1 Parent(s): 1353374

Create index.html

Browse files
Files changed (1) hide show
  1. index.html +251 -0
index.html ADDED
@@ -0,0 +1,251 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>AR City Explorer | Smart Glasses Companion</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
9
+
10
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/aframe.min.js"></script>
11
+ <script src="https://raw.githack.com/AR-js-org/AR.js/master/aframe/build/aframe-ar-nft.js"></script>
12
+
13
+ <style>
14
+ /* Your original CSS styles are fine, no changes needed here. */
15
+ @keyframes pulse { 0% { opacity: 0.6; } 50% { opacity: 1; } 100% { opacity: 0.6; } }
16
+ .pulse { animation: pulse 2s infinite; }
17
+ .ar-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.8); z-index: 9999; display: flex; flex-direction: column; justify-content: center; align-items: center; color: white; }
18
+ .ai-message { max-width: 80%; margin-bottom: 10px; padding: 12px; border-radius: 18px; position: relative; }
19
+ .user-message { background-color: #3b82f6; color: white; align-self: flex-end; border-bottom-right-radius: 4px; }
20
+ .assistant-message { background-color: #f3f4f6; color: #111827; align-self: flex-start; border-bottom-left-radius: 4px; }
21
+ .glasses-pairing { background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%); }
22
+ .loading-spinner { width: 50px; height: 50px; border: 5px solid rgba(255,255,255,0.3); border-radius: 50%; border-top-color: #fff; animation: spin 1s ease-in-out infinite; }
23
+ @keyframes spin { to { transform: rotate(360deg); } }
24
+ </style>
25
+ </head>
26
+
27
+ <body class="bg-gray-100 font-sans">
28
+ <div class="max-w-md mx-auto bg-white shadow-lg rounded-lg overflow-hidden min-h-screen flex flex-col">
29
+ <header class="bg-indigo-900 text-white p-4 flex justify-between items-center">
30
+ <div class="flex items-center space-x-2">
31
+ <i class="fas fa-glasses text-2xl"></i>
32
+ <h1 class="text-xl font-bold">AR City Explorer</h1>
33
+ </div>
34
+ <div class="flex space-x-3">
35
+ <button id="arToggle" class="bg-indigo-700 hover:bg-indigo-600 px-3 py-1 rounded-full text-sm flex items-center">
36
+ <i class="fas fa-vr-cardboard mr-1"></i> AR Mode
37
+ </button>
38
+ <button id="settingsBtn" class="bg-indigo-700 hover:bg-indigo-600 px-3 py-1 rounded-full text-sm">
39
+ <i class="fas fa-cog"></i>
40
+ </button>
41
+ </div>
42
+ </header>
43
+
44
+ <main class="flex-1 overflow-hidden">
45
+ <div id="arViewport" class="hidden relative">
46
+ <a-scene id="arScene" vr-mode-ui="enabled: false" arjs='sourceType: webcam; videoTexture: true; debugUIEnabled: false;'>
47
+ <a-camera gps-new-camera='gpsMinDistance: 5'></a-camera>
48
+ </a-scene>
49
+ </div>
50
+
51
+ <div id="normalView" class="h-full flex flex-col">
52
+ <div id="aiAssistant" class="flex-1 overflow-y-auto p-4 bg-gray-50">
53
+ <div class="flex flex-col space-y-2">
54
+ <div class="ai-message assistant-message">
55
+ <div class="font-bold text-indigo-800 mb-1">AR Guide</div>
56
+ <p>Hello! Ask me about nearby landmarks, or switch to AR mode to explore.</p>
57
+ </div>
58
+ </div>
59
+ </div>
60
+ <div class="p-4 border-t border-gray-200 bg-white">
61
+ <div class="flex space-x-2">
62
+ <input type="text" id="userInput" placeholder="Ask about places..." class="flex-1 border border-gray-300 rounded-full px-4 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-500">
63
+ <button id="sendBtn" class="bg-indigo-600 text-white rounded-full w-10 h-10 flex items-center justify-center">
64
+ <i class="fas fa-paper-plane"></i>
65
+ </button>
66
+ <button id="voiceBtn" class="bg-gray-200 text-gray-700 rounded-full w-10 h-10 flex items-center justify-center">
67
+ <i class="fas fa-microphone"></i>
68
+ </button>
69
+ </div>
70
+ </div>
71
+ </div>
72
+ </main>
73
+
74
+ <nav class="bg-white border-t border-gray-200 p-2 flex justify-around">
75
+ </nav>
76
+ </div>
77
+
78
+ <div id="objectModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden z-50">
79
+ <div class="bg-white rounded-xl p-6 max-w-sm w-full">
80
+ <div class="flex justify-between items-center mb-4">
81
+ <h3 id="objectTitle" class="text-xl font-bold text-gray-800"></h3>
82
+ <button id="closeObjectModal" class="text-gray-500"><i class="fas fa-times"></i></button>
83
+ </div>
84
+ <img id="objectImage" src="" alt="Landmark" class="w-full h-48 object-cover rounded-lg mb-4">
85
+ <div id="objectDescription" class="text-gray-700 mb-4"></div>
86
+ </div>
87
+ </div>
88
+ <script>
89
+ document.addEventListener('DOMContentLoaded', () => {
90
+ // --- CONFIGURATION ---
91
+ const config = {
92
+ // IMPORTANT: Paste your full and correct Cloudflare Worker URL here
93
+ backendUrl: 'https://ar-city-explorer-backend.<your-subdomain>.workers.dev'
94
+ };
95
+
96
+ // --- GLOBAL STATE ---
97
+ let poisData = []; // This will hold all our landmark data from the backend
98
+ let arActive = false;
99
+ let glassesPaired = false; // You can manage this state as needed
100
+
101
+ // --- DOM ELEMENT SELECTORS ---
102
+ const arToggle = document.getElementById('arToggle');
103
+ const arViewport = document.getElementById('arViewport');
104
+ const normalView = document.getElementById('normalView');
105
+ const aiAssistant = document.getElementById('aiAssistant');
106
+ const userInput = document.getElementById('userInput');
107
+ const sendBtn = document.getElementById('sendBtn');
108
+ const objectModal = document.getElementById('objectModal');
109
+ const objectTitle = document.getElementById('objectTitle');
110
+ const objectDescription = document.getElementById('objectDescription');
111
+ const objectImage = document.getElementById('objectImage');
112
+ const closeObjectModal = document.getElementById('closeObjectModal');
113
+
114
+ // --- CORE FUNCTIONS ---
115
+
116
+ function toggleARView(showAR) {
117
+ arActive = showAR;
118
+ if (arActive) {
119
+ normalView.classList.add('hidden');
120
+ arViewport.classList.remove('hidden');
121
+ arToggle.innerHTML = '<i class="fas fa-times mr-1"></i> Exit AR';
122
+ // Fetch data only if it hasn't been fetched yet
123
+ if (poisData.length === 0) {
124
+ fetchPoisAndCreateAREntities();
125
+ }
126
+ } else {
127
+ arViewport.classList.add('hidden');
128
+ normalView.classList.remove('hidden');
129
+ arToggle.innerHTML = '<i class="fas fa-vr-cardboard mr-1"></i> AR Mode';
130
+ }
131
+ }
132
+
133
+ function fetchPoisAndCreateAREntities() {
134
+ console.log('Fetching POIs from backend...');
135
+ fetch(`${config.backendUrl}/api/pois`)
136
+ .then(response => {
137
+ if (!response.ok) throw new Error(`Network error: ${response.statusText}`);
138
+ return response.json();
139
+ })
140
+ .then(pois => {
141
+ console.log('POIs received:', pois);
142
+ poisData = pois; // Store the data globally
143
+ const scene = document.querySelector('a-scene');
144
+ if (!scene) return console.error('A-Frame scene not found!');
145
+
146
+ pois.forEach(poi => {
147
+ const entity = document.createElement('a-entity');
148
+ entity.setAttribute('gps-new-entity-place', {
149
+ latitude: poi.latitude,
150
+ longitude: poi.longitude
151
+ });
152
+
153
+ const box = document.createElement('a-box');
154
+ box.setAttribute('material', 'color: red; opacity: 0.7;');
155
+ box.setAttribute('scale', '10 10 10');
156
+ box.setAttribute('position', '0 5 0');
157
+ box.setAttribute('data-poi-id', poi.id); // Set ID for click detection
158
+ entity.appendChild(box);
159
+
160
+ const text = document.createElement('a-text');
161
+ text.setAttribute('value', poi.name);
162
+ text.setAttribute('look-at', '[gps-new-camera]');
163
+ text.setAttribute('scale', '50 50 50');
164
+ text.setAttribute('position', '0 15 0');
165
+ entity.appendChild(text);
166
+
167
+ scene.appendChild(entity);
168
+ });
169
+ })
170
+ .catch(error => {
171
+ console.error('Failed to load POIs:', error);
172
+ alert('Could not load city data. Check your connection and the backend URL.');
173
+ toggleARView(false);
174
+ });
175
+ }
176
+
177
+ // --- MODAL AND UI FUNCTIONS ---
178
+
179
+ function showObjectInfo(poiId) {
180
+ const poi = poisData.find(p => p.id === poiId);
181
+ if (!poi) return;
182
+ objectTitle.textContent = poi.name;
183
+ objectDescription.textContent = poi.description || "No description available.";
184
+ objectImage.src = `https://via.placeholder.com/300x200?text=${encodeURIComponent(poi.name)}`;
185
+ objectModal.classList.remove('hidden');
186
+ }
187
+
188
+ function addUserMessage(message) {
189
+ const chatContainer = aiAssistant.querySelector('.flex-col');
190
+ const msg = `<div class="ai-message user-message"><p>${message}</p></div>`;
191
+ chatContainer.insertAdjacentHTML('beforeend', msg);
192
+ aiAssistant.scrollTop = aiAssistant.scrollHeight;
193
+ }
194
+
195
+ function addAIMessage(message) {
196
+ const chatContainer = aiAssistant.querySelector('.flex-col');
197
+ const msg = `<div class="ai-message assistant-message"><div class="font-bold text-indigo-800 mb-1">AR Guide</div><p>${message}</p></div>`;
198
+ chatContainer.insertAdjacentHTML('beforeend', msg);
199
+ aiAssistant.scrollTop = aiAssistant.scrollHeight;
200
+ }
201
+
202
+ function handleSearch() {
203
+ const searchTerm = userInput.value.trim();
204
+ if (!searchTerm) return;
205
+ addUserMessage(searchTerm);
206
+ userInput.value = '';
207
+
208
+ if (poisData.length === 0) {
209
+ addAIMessage("I'm still loading location data. Please try again in a moment.");
210
+ return;
211
+ }
212
+
213
+ const results = poisData.filter(poi =>
214
+ poi.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
215
+ (poi.description && poi.description.toLowerCase().includes(searchTerm.toLowerCase()))
216
+ );
217
+
218
+ if (results.length > 0) {
219
+ let response = `I found ${results.length} result(s):<ul>`;
220
+ results.forEach(poi => { response += `<li class="mt-2 list-disc list-inside">${poi.name}</li>`; });
221
+ response += "</ul>";
222
+ addAIMessage(response);
223
+ } else {
224
+ addAIMessage(`Sorry, I couldn't find anything matching "${searchTerm}".`);
225
+ }
226
+ }
227
+
228
+ // --- EVENT LISTENERS ---
229
+
230
+ arToggle.addEventListener('click', () => toggleARView(!arActive));
231
+
232
+ document.querySelector('a-scene').addEventListener('click', (event) => {
233
+ if (event.target.hasAttribute('data-poi-id')) {
234
+ const poiId = parseInt(event.target.getAttribute('data-poi-id'), 10);
235
+ showObjectInfo(poiId);
236
+ }
237
+ });
238
+
239
+ closeObjectModal.addEventListener('click', () => objectModal.classList.add('hidden'));
240
+
241
+ sendBtn.addEventListener('click', handleSearch);
242
+ userInput.addEventListener('keypress', (e) => {
243
+ if (e.key === 'Enter') handleSearch();
244
+ });
245
+
246
+ // Initial fetch of data for the search bar to work immediately
247
+ fetchPoisAndCreateAREntities();
248
+ });
249
+ </script>
250
+ </body>
251
+ </html>