Advanced UX Control and Customization for Experienced Developers
Last updated
Introduction
This guide walks you through building an advanced surface tracking experience using the WebAR SDK with custom progress handlers, error management, and enhanced user experience. This builds upon the by adding professional-grade features for production applications.
Why Build an Advanced Experience?
Professional UX: Custom loading screens with real-time progress indicators and comprehensive error handling provide a polished, branded user experience that builds trust and engagement.
Real-time Monitoring: Track actual SDK loading progress with precise percentage and object count data instead of generic spinners, giving users meaningful feedback about loading status.
Full Control on Camera Stream: Manage camera permissions, initialization, and error handling manually with external-camera-stream="true", enabling custom permission prompts and better user guidance for camera-related issues.
Headless Mode: With minimal-ui="true" + on-progress + on-error, the screen goes completely white (no default SDK UI), then opens camera and starts the AR pipeline - giving you complete control to implement your own UI and user flow.
Prerequisites
Complete the first, or have familiarity with basic WebAR SDK integration.
Understanding Custom Handlers
on-progress Attribute
The on-progress attribute allows you to receive real-time loading progress data from the WebAR SDK:
// Your custom progress handler receives this data:
function customProgressHandler(progress) {
// progress.percentage - Loading completion (0-100)
// progress.current - Current loaded objects
// progress.total - Total objects to load
}
Pass this function to on-progress attribute while loading WebAR SDK script:
<script src="./webar-sdk.min.js"
webar-mode="surface-tracking"
auto-init="true"
auto-start-tracking="true"
minimal-ui="true"
on-progress="customProgressCallback" <!-- Required for custom progress bar -->>
</script>
Why use on-progress?
Replace generic "Loading..." with actual progress percentages
Show users detailed loading status (e.g., "Loading AR engine: 75%")
Debug loading performance issues
Provide smooth, professional loading experience
on-error Attribute
The on-error attribute provides comprehensive error information when something goes wrong:
// Your custom error handler receives detailed error data:
function customErrorHandler(error) {
// error.code - Specific error type (e.g., 'ERR_CAMERA_PERMISSION_DENIED')
// error.description - Human-readable error message
// error.instruction - Suggested user action
// error.browser - Browser information for debugging
// error.timestamp
}
Error Codes You May Receive:
// Browser Compatibility Errors
'ERR_UNSUPPORTED_BROWSER' // Browser doesn't support WebAR
'ERR_WEBRTC_NOT_SUPPORTED' // Browser lacks WebRTC support
'ERR_FACEBOOK_BROWSER' // Facebook in-app browser detected
'ERR_INSTAGRAM_BROWSER' // Instagram in-app browser detected
// Orientation Errors
'ERR_LANDSCAPE_MODE' // Device in unsupported landscape mode
// Permission Errors
'ERR_CAMERA_PERMISSION_DENIED' // User denied camera access
'ERR_MOTION_PERMISSION_DENIED' // User denied motion sensor access
// System Errors
'ERR_INITIALIZATION_FAILED' // SDK failed to initialize
'ERR_TRACKING_FAILED' // AR tracking failed to start
'ERR_RESOURCE_LOAD_FAILED' // Failed to load SDK resources
// Network Errors
'ERR_NETWORK_ERROR' // Network connectivity issues
'ERR_SERVER_ERROR' // Server-side error
// Generic Errors
'ERR_UNKNOWN' // Unspecified error
Why custom loading screen? Professional apps need branded, informative loading experiences.
With minimal-ui="true", the SDK provides no default UI - you must create your own splash screen and manage its lifecycle. Please note that you may use the on-load attribute to override the default SDK splash screen while keeping other SDK UI elements when minimal-ui="false" .
Critical Timing: Load Splash Screen Before SDK loads
Important: Load your custom-handlers.js script BEFORE the WebAR SDK script. With minimal-ui="true", the SDK provides no default UI, so without your custom handlers loaded first, users will see a blank white screen during SDK initialization. Your custom handlers automatically create and inject the splash screen when the SDK starts loading.
<head>
<!-- A-Frame -->
<script src="https://aframe.io/releases/1.6.0/aframe.min.js"></script>
<!-- Load custom handlers BEFORE SDK -->
<script src="custom-handlers.js"></script>
<!-- WebAR SDK with minimal-ui -->
<script src="./webar-sdk.min.js"
webar-mode="surface-tracking"
auto-init="true"
minimal-ui="true" ← Disable ALL SDK UI
on-progress="customProgressCallback" ← Get progress updates, may use the data to update loading screen UI
>
</script>
</head>
Why this order matters:
minimal-ui="true" disables all SDK UI
SDK immediately calls customProgressCallback when loading starts
If your handlers aren't loaded yet → white screen
With handlers loaded first → instant professional splash screen
Your custom-handlers.js contains all the splash screen creation logic, so no additional HTML or CSS setup is required in your main page.
Step 5: Camera and Sensor Management
Why manual camera management? Better control over permissions and error handling.
loader.js
const iOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
let camstream = null;
let sdkloaded = false;
// Why check iOS separately? iOS requires user interaction before camera access
function handleCameraPermissions() {
if (iOS) {
showPermissionPrompt('camera');
} else {
startCameraStream();
}
}
function startCameraStream() {
navigator.mediaDevices.getUserMedia({
video: {
facingMode: 'environment',
advanced: [{ focusMode: 'manual', focusDistance: 0 }]
}
})
.then(function(stream) {
camstream = stream;
const videoElement = document.getElementById('video');
videoElement.srcObject = stream;
// Why wait for gyro? Tracking needs motion sensors
handleMotionPermissions();
})
.catch(function(error) {
console.error('Camera access failed:', error);
customErrorHandler({
code: 'ERR_CAMERA_PERMISSION_DENIED',
description: 'Camera access was denied',
instruction: 'Please refresh and allow camera access'
});
});
}
// Why motion permissions? AR tracking requires device orientation data
function handleMotionPermissions() {
if (typeof DeviceMotionEvent.requestPermission === 'function') {
DeviceMotionEvent.requestPermission().then(response => {
if (response === 'granted') {
initializeTracking();
} else {
customErrorHandler({
code: 'ERR_MOTION_PERMISSION_DENIED',
description: 'Motion sensor access was denied',
instruction: 'Motion sensors are required for AR tracking'
});
}
});
} else {
// Non-iOS devices
initializeTracking();
}
}
function initializeTracking() {
if (sdkloaded && camstream) {
WEBARSDK.SetCameraStream(camstream);
}
}
// SDK callback when ready
WEBARSDK.SetAppReadyCallback(() => {
sdkloaded = true;
initializeTracking();
});
// SDK callback when video starts
WEBARSDK.SetVideoStartedCallback(() => {
// Hide loading screen when AR is ready
const loadingScreen = document.getElementById('loadingscreen');
if (loadingScreen) {
loadingScreen.style.display = 'none';
}
// Remove original video element (SDK creates its own)
const videoElement = document.getElementById('video');
if (videoElement && videoElement.parentElement) {
videoElement.parentElement.removeChild(videoElement);
}
});
The customProgressHandler receives actual SDK loading data including:
progress.percentage - Loading completion (0-100)
progress.current - Current loaded objects
progress.total - Total objects to load
External Camera Stream
Using external-camera-stream="true" gives you full control over camera initialization, allowing for custom permission handling and better user experience.
Comprehensive Error Handling
The error handler provides detailed error information and user-friendly messages for different error scenarios.
Deployment Considerations
HTTPS Required: WebAR requires HTTPS for camera access
License Key: Replace YOUR_LICENSE_KEY_HERE with your actual license key
Asset Optimization: Optimize 3D models for web delivery
Browser Testing: Test across Safari (iOS) and Chrome (Android)
Full Source Code
Common Issues and Solutions
Progress handler not called?
Ensure minimal-ui="true" is set
Check that custom-handlers.js loads before the SDK
Verify function names match exactly
Error handler not working?
Confirm on-error attribute points to correct function name
Check browser console for JavaScript errors
Ensure error UI elements exist in DOM
Camera permissions failing?
Verify HTTPS is enabled
Test iOS/Android permission prompt timing
Check camera constraints are supported
Key Benefits of This Advanced Implementation
Your advanced surface tracking experience will include:
Professional loading screen with real-time progress
Comprehensive error handling and user guidance
Smooth camera and sensor permission management
Enhanced debugging capabilities for development
Modular code architecture for easy maintenance
This advanced implementation provides a professional, production-ready foundation for WebAR applications with comprehensive UX control and error handling.
The complete implementation is available in the under /aframe/advanced-surface-tracking/.