1- import React , {
2- useState ,
3- useRef ,
4- useEffect ,
5- forwardRef ,
6- useImperativeHandle ,
7- } from 'react' ;
1+ import React , { useState } from 'react' ;
82import './Wallpaper.css' ;
93import SequoiaSunriseImage from '../../media/wallpaper/SequoiaSunrise.jpg' ;
10- import { FocusWrapper } from '../../interactions/FocusControl/FocusControl.jsx' ;
4+ import { FocusWrapper } from '../../interactions/FocusControl/FocusControl.jsx' ; // Import FocusWrapper
115
12- const Wallpaper = forwardRef ( ( props , ref ) => {
6+ function Wallpaper ( ) {
137 const [ isVideoLoaded , setIsVideoLoaded ] = useState ( false ) ;
14- const videoRef = useRef ( null ) ;
15- const segmentTimer = useRef ( null ) ;
16- // Prevent overlapping segment triggers
17- const segmentInProgress = useRef ( false ) ;
18-
19- // Configurable parameters:
20- const speedUpDuration = 1 ; // seconds: video speeds up from 0 to 1
21- const normalDuration = 4 ; // seconds: video plays at normal speed (1×)
22- const slowDownDuration = 1 ; // seconds: video slows down from 1 to 0
23- const segmentDuration = speedUpDuration + normalDuration + slowDownDuration ; // Total segment duration (default: 10 seconds)
24-
25- // Function to trigger a segment: speed-up, normal playback, and then slow-down.
26- const playSegment = ( ) => {
27- if ( ! videoRef . current || segmentInProgress . current ) return ;
28- segmentInProgress . current = true ;
29-
30- // Start playing from current position.
31- videoRef . current . play ( ) . catch ( ( err ) => {
32- console . error ( 'Error playing video:' , err ) ;
33- } ) ;
34-
35- const startTime = Date . now ( ) ;
36-
37- // Clear any existing timer.
38- if ( segmentTimer . current ) {
39- clearInterval ( segmentTimer . current ) ;
40- }
41-
42- segmentTimer . current = setInterval ( ( ) => {
43- const elapsed = ( Date . now ( ) - startTime ) / 1000 ;
44-
45- if ( elapsed < speedUpDuration ) {
46- // Speed Up Phase: Increase playback rate linearly from 0 to 1.
47- const t = elapsed / speedUpDuration ;
48- videoRef . current . playbackRate = t ;
49- } else if ( elapsed < speedUpDuration + normalDuration ) {
50- // Normal Playback Phase: Maintain a playback rate of 1.
51- videoRef . current . playbackRate = 1 ;
52- } else if ( elapsed < segmentDuration ) {
53- // Slow Down Phase: Decrease playback rate linearly from 1 to 0.
54- const slowdownElapsed = elapsed - ( speedUpDuration + normalDuration ) ;
55- videoRef . current . playbackRate = 1 * ( 1 - slowdownElapsed / slowDownDuration ) ;
56- } else {
57- // End of the segment: pause the video and reset the playback rate.
58- videoRef . current . playbackRate = 0 ;
59- videoRef . current . pause ( ) ;
60- clearInterval ( segmentTimer . current ) ;
61- segmentTimer . current = null ;
62- segmentInProgress . current = false ;
63- }
64- } , 100 ) ; // update every 100ms
65- } ;
66-
67- // Expose playSegment so that parent components can trigger the next segment.
68- useImperativeHandle ( ref , ( ) => ( {
69- playSegment,
70- } ) ) ;
71-
72- // Automatically trigger the initial segment once the video is loaded.
73- useEffect ( ( ) => {
74- if ( isVideoLoaded ) {
75- playSegment ( ) ;
76- }
77- // Cleanup on unmount.
78- return ( ) => {
79- if ( segmentTimer . current ) {
80- clearInterval ( segmentTimer . current ) ;
81- }
82- } ;
83- } , [ isVideoLoaded ] ) ;
848
859 return (
8610 < FocusWrapper name = "Wallpaper" >
@@ -94,23 +18,22 @@ const Wallpaper = forwardRef((props, ref) => {
9418
9519 { /* Background video */ }
9620 < video
97- ref = { videoRef }
98- className = "wallpaper-video"
21+ className = { isVideoLoaded ? 'wallpaper-video' : 'wallpaper-video hidden' }
22+ autoPlay
9923 muted
100- loop = { false }
24+ loop
10125 playsInline
10226 onLoadedData = { ( ) => setIsVideoLoaded ( true ) }
103- onError = { ( e ) => console . error ( 'Video failed to load:' , e ) }
27+ onError = { ( e ) => {
28+ console . error ( 'Video failed to load:' , e ) ;
29+ } }
10430 >
105- < source
106- src = "/WebintoshHD/Wallpapers/SequoiaSunrise.mp4"
107- type = "video/mp4"
108- />
31+ < source src = "/WebintoshHD/Wallpapers/SequoiaSunrise.mp4" type = "video/mp4" />
10932 Your browser does not support the video tag.
11033 </ video >
11134 </ div >
11235 </ FocusWrapper >
11336 ) ;
114- } ) ;
37+ }
11538
116- export default Wallpaper ;
39+ export default Wallpaper ;
0 commit comments