File tree Expand file tree Collapse file tree 1 file changed +28
-17
lines changed
Animated Car/Animated Car Expand file tree Collapse file tree 1 file changed +28
-17
lines changed Original file line number Diff line number Diff line change 1- // const audio = new Audio('caraudio.mp3');
2- // var resp = audio.play();
3-
4- // if(resp != undefined){
5- // resp.then(_ => {
6- // //autoplay starts
7- // }).catch(error => {
8- // //shoe error
9- // });
10- // }
11- // audio.loop()=true;
12-
131var audio = document . createElement ( "audio" ) ;
142document . body . appendChild ( audio ) ;
15- audio . src = "caraudio.mp3"
3+ audio . src = "caraudio.mp3" ;
4+
5+ let isMoving = false ;
6+
7+ // Start playing the audio when the mouse moves
8+ document . body . addEventListener ( "mousemove" , function ( ) {
9+ if ( ! isMoving ) {
10+ audio . play ( ) ;
11+ audio . loop = true ; // Set the audio to loop
12+ isMoving = true ; // Indicate that the car is moving
13+ }
14+ } ) ;
1615
16+ // Stop playing the audio when the left mouse button is released
17+ document . body . addEventListener ( "mouseup" , function ( event ) {
18+ if ( event . button === 0 ) { // 0 is the left mouse button
19+ audio . pause ( ) ;
20+ audio . currentTime = 0 ; // Optionally reset audio to the beginning
21+ isMoving = false ; // Indicate that the car has stopped
22+ }
23+ } ) ;
1724
18- document . body . addEventListener ( "mousemove" , function ( ) {
19- audio . play ( ) ;
20- audio . loop ( ) ;
21- } )
25+ // Optional: Start playing audio again if the mouse is moved while stopped
26+ document . body . addEventListener ( "mousedown" , function ( ) {
27+ if ( ! isMoving ) {
28+ audio . play ( ) ;
29+ audio . loop = true ;
30+ isMoving = true ;
31+ }
32+ } ) ;
You can’t perform that action at this time.
0 commit comments