Enabling Debug Mode
Enable debug mode through the constructor:How Debug Mode Works
When debug mode is enabled, WebHaptics creates an audio feedback system that:- Generates click sounds synchronized with vibrations
- Varies pitch and volume based on intensity
- Works on both mobile (alongside vibration) and desktop (audio only)
On mobile devices with vibration support, debug mode plays audio in addition to vibration. On desktop, only audio plays.
Audio Feedback System
The audio system is initialized inensureAudio() (index.ts:379-406):
Audio System Components
Audio System Components
AudioContext
- Creates and manages the audio processing graph
- Resumes if suspended (common on page load)
- Type: Bandpass filter
- Center frequency: 4000 Hz (varies with intensity)
- Q value: 8 (narrow bandwidth for crisp clicks)
- Controls volume based on intensity
- Range: 0 to 0.5 (scaled by intensity)
- Duration: 4ms (very short click)
- Contains exponentially decaying white noise
- Regenerated for each click with randomization
Click Sound Generation
Each vibration triggers theplayClick() function (index.ts:352-377):
Intensity-Based Audio Mapping
Intensity-Based Audio Mapping
Volume (Gain)
- Intensity 0.0: Silent
- Intensity 0.5: 25% volume
- Intensity 1.0: 50% volume (max)
- Intensity 0.0: ~2000 Hz (low pitch)
- Intensity 0.5: ~3000 Hz (medium pitch)
- Intensity 1.0: ~4000 Hz (high pitch)
- Jitter: ±15% randomization for natural variation
- White noise with exponential decay
- Creates a “click” or “tap” sound
- Regenerated for each click (subtle variation)
Debug Mode Example
- Basic Testing
- Intensity Testing
- Custom Pattern Testing
Test different patterns and hear the differences:
Visual Debug Indicator
Debug mode also injects a visual indicator on unsupported devices. The indicator is created inensureDOM() (index.ts:408-445):
The visual indicator appears only when haptics are not supported (typically desktop). It provides visual feedback synchronized with audio clicks.
Optional Haptic Toggle Switch
You can show a user-facing toggle switch:Cleanup
Disabling debug mode or destroying the instance cleans up audio resources:setDebug() (index.ts:246-255):
Development Workflow
Recommended Workflow
Recommended Workflow
-
Develop on desktop with debug mode:
-
Fine-tune patterns using audio feedback:
- Adjust durations, delays, and intensities
- Listen for the right “feel”
- Iterate quickly without mobile device
-
Test on mobile device:
-
Deploy with debug disabled:
Conditional Debug Mode
Conditional Debug Mode
Enable debug mode only in development:Or based on URL parameter:
Performance Impact
Audio System Overhead
Audio System Overhead
Debug mode adds minimal overhead:
- AudioContext: Created once, reused for all clicks
- Audio nodes: Small fixed memory footprint
- Click generation: ~4ms audio buffer, negligible processing
- Network: Zero (all audio generated in-browser)
Browser Support
Debug audio requires Web Audio API support:Web Audio API is supported in all modern browsers (Chrome, Firefox, Safari, Edge). Fallback gracefully degrades to visual-only feedback on unsupported browsers.
Next Steps
Built-in Patterns
Explore all built-in patterns to test with debug mode
Custom Patterns
Create and test custom patterns using debug audio feedback