Changed around line 1
+ const video = document.getElementById('camera');
+ const canvas = document.getElementById('ar-canvas');
+ const startButton = document.getElementById('start-ar');
+ const stopButton = document.getElementById('stop-ar');
+ const ctx = canvas.getContext('2d');
+
+ let isARActive = false;
+
+ async function startAR() {
+ try {
+ const stream = await navigator.mediaDevices.getUserMedia({ video: true });
+ video.srcObject = stream;
+ isARActive = true;
+ startButton.disabled = true;
+ stopButton.disabled = false;
+ requestAnimationFrame(renderAR);
+ } catch (error) {
+ alert('Error accessing camera. Please ensure you have granted permission.');
+ console.error('Camera access error:', error);
+ }
+ }
+
+ function stopAR() {
+ const stream = video.srcObject;
+ if (stream) {
+ stream.getTracks().forEach(track => track.stop());
+ }
+ video.srcObject = null;
+ isARActive = false;
+ startButton.disabled = false;
+ stopButton.disabled = true;
+ }
+
+ function renderAR() {
+ if (!isARActive) return;
+
+ canvas.width = video.videoWidth;
+ canvas.height = video.videoHeight;
+
+ // Draw camera feed
+ ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
+
+ // Add AR elements
+ ctx.fillStyle = 'rgba(255, 0, 0, 0.5)';
+ ctx.fillRect(
+ canvas.width / 2 - 50,
+ canvas.height / 2 - 50,
+ 100,
+ 100
+ );
+
+ requestAnimationFrame(renderAR);
+ }
+
+ startButton.addEventListener('click', startAR);
+ stopButton.addEventListener('click', stopAR);
+
+ // Handle window resize
+ window.addEventListener('resize', () => {
+ if (isARActive) {
+ canvas.width = video.videoWidth;
+ canvas.height = video.videoHeight;
+ }
+ });