|
| 1 | +/** |
| 2 | + * Lazy Video Loader |
| 3 | + * Loads videos after page load to prevent freezing |
| 4 | + */ |
| 5 | + |
| 6 | +class LazyVideoLoader { |
| 7 | + constructor() { |
| 8 | + this.loadedVideos = new Set(); |
| 9 | + this.isMobile = this.detectMobile(); |
| 10 | + this.init(); |
| 11 | + } |
| 12 | + |
| 13 | + detectMobile() { |
| 14 | + // Check for mobile devices using multiple methods |
| 15 | + const userAgent = navigator.userAgent.toLowerCase(); |
| 16 | + const isMobileUA = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent); |
| 17 | + const isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0; |
| 18 | + const hasHover = window.matchMedia('(hover: hover)').matches; |
| 19 | + const isSmallScreen = window.innerWidth <= 768; |
| 20 | + |
| 21 | + const isMobile = isMobileUA || (isTouchDevice && !hasHover && isSmallScreen); |
| 22 | + |
| 23 | + // Log detection results for debugging |
| 24 | + console.log('Mobile Detection:', { |
| 25 | + userAgent: isMobileUA, |
| 26 | + touchDevice: isTouchDevice, |
| 27 | + hasHover: hasHover, |
| 28 | + smallScreen: isSmallScreen, |
| 29 | + screenWidth: window.innerWidth, |
| 30 | + finalResult: isMobile |
| 31 | + }); |
| 32 | + |
| 33 | + return isMobile; |
| 34 | + } |
| 35 | + |
| 36 | + init() { |
| 37 | + // Wait for page to fully load before starting video loading |
| 38 | + if (document.readyState === 'loading') { |
| 39 | + document.addEventListener('DOMContentLoaded', () => { |
| 40 | + setTimeout(() => this.loadHeroVideo(), 500); |
| 41 | + }); |
| 42 | + } else { |
| 43 | + setTimeout(() => this.loadHeroVideo(), 500); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + async loadHeroVideo() { |
| 48 | + const heroVideo = document.querySelector('.video-background'); |
| 49 | + if (!heroVideo || this.loadedVideos.has('hero')) return; |
| 50 | + |
| 51 | + try { |
| 52 | + // Show loading state |
| 53 | + this.showVideoLoading(heroVideo); |
| 54 | + |
| 55 | + // Load the video source |
| 56 | + await this.loadVideoSource(heroVideo); |
| 57 | + |
| 58 | + // Set playback rate and play |
| 59 | + heroVideo.playbackRate = 0.25; |
| 60 | + await heroVideo.play(); |
| 61 | + |
| 62 | + this.loadedVideos.add('hero'); |
| 63 | + this.hideVideoLoading(heroVideo); |
| 64 | + |
| 65 | + // Only preload crumple videos on desktop devices |
| 66 | + if (!this.isMobile) { |
| 67 | + setTimeout(() => this.preloadCrumpleVideos(), 1000); |
| 68 | + } else { |
| 69 | + console.log('Mobile device detected - skipping crumple video preloading to save bandwidth'); |
| 70 | + } |
| 71 | + |
| 72 | + } catch (error) { |
| 73 | + console.warn('Hero video failed to load:', error); |
| 74 | + this.hideVideoLoading(heroVideo); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + async preloadCrumpleVideos() { |
| 79 | + const crumpleVideos = document.querySelectorAll('.crumple-video'); |
| 80 | + |
| 81 | + // Load videos one by one to avoid overwhelming the browser |
| 82 | + for (let i = 0; i < crumpleVideos.length; i++) { |
| 83 | + const video = crumpleVideos[i]; |
| 84 | + if (!this.loadedVideos.has(video.className)) { |
| 85 | + try { |
| 86 | + await this.loadVideoSource(video); |
| 87 | + this.loadedVideos.add(video.className); |
| 88 | + |
| 89 | + // Small delay between loading each video |
| 90 | + await new Promise(resolve => setTimeout(resolve, 200)); |
| 91 | + } catch (error) { |
| 92 | + console.warn(`Crumple video ${i} failed to preload:`, error); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + loadVideoSource(video) { |
| 99 | + return new Promise((resolve, reject) => { |
| 100 | + // If video already has a source and is loaded, resolve immediately |
| 101 | + if (video.readyState >= 3) { |
| 102 | + resolve(); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // Check if we need to move data-src to src |
| 107 | + const source = video.querySelector('source[data-src]'); |
| 108 | + if (source && source.dataset.src) { |
| 109 | + source.src = source.dataset.src; |
| 110 | + source.removeAttribute('data-src'); |
| 111 | + } |
| 112 | + |
| 113 | + // Set up event listeners |
| 114 | + const onLoad = () => { |
| 115 | + cleanup(); |
| 116 | + resolve(); |
| 117 | + }; |
| 118 | + |
| 119 | + const onError = (error) => { |
| 120 | + cleanup(); |
| 121 | + reject(error); |
| 122 | + }; |
| 123 | + |
| 124 | + const cleanup = () => { |
| 125 | + video.removeEventListener('canplaythrough', onLoad); |
| 126 | + video.removeEventListener('error', onError); |
| 127 | + }; |
| 128 | + |
| 129 | + video.addEventListener('canplaythrough', onLoad); |
| 130 | + video.addEventListener('error', onError); |
| 131 | + |
| 132 | + // Trigger video loading |
| 133 | + video.load(); |
| 134 | + |
| 135 | + // Timeout after 10 seconds |
| 136 | + setTimeout(() => { |
| 137 | + cleanup(); |
| 138 | + reject(new Error('Video load timeout')); |
| 139 | + }, 10000); |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + showVideoLoading(video) { |
| 144 | + // Add a subtle loading indicator |
| 145 | + video.style.opacity = '0.3'; |
| 146 | + |
| 147 | + // Create loading overlay if it doesn't exist |
| 148 | + let overlay = video.parentNode.querySelector('.video-loading-overlay'); |
| 149 | + if (!overlay) { |
| 150 | + overlay = document.createElement('div'); |
| 151 | + overlay.className = 'video-loading-overlay'; |
| 152 | + overlay.innerHTML = '<div class="loading-spinner"></div>'; |
| 153 | + video.parentNode.appendChild(overlay); |
| 154 | + } |
| 155 | + overlay.style.display = 'flex'; |
| 156 | + } |
| 157 | + |
| 158 | + hideVideoLoading(video) { |
| 159 | + video.style.opacity = '1'; |
| 160 | + const overlay = video.parentNode.querySelector('.video-loading-overlay'); |
| 161 | + if (overlay) { |
| 162 | + overlay.style.display = 'none'; |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// Initialize the lazy video loader |
| 168 | +new LazyVideoLoader(); |
0 commit comments