@@ -4,71 +4,72 @@ class ImageBackgroundScroller {
44 this . virtualWidth = options . virtualWidth || 1080 ;
55 this . virtualHeight = options . virtualHeight || 1080 ;
66 this . scrollSpeed = options . scrollSpeed || 100 ;
7- this . offsetY = options . offsetY || 0 ;
8- this . scrollY = 0 ;
97
10- // Load both background images
11- this . images = [ new Image ( ) , new Image ( ) , new Image ( ) , new Image ( ) , new Image ( ) , new Image ( ) , new Image ( ) , new Image ( ) , new Image ( ) , new Image ( ) ] ;
12- this . images [ 0 ] . src = './backgrounds/4.png' ;
13- this . images [ 1 ] . src = './backgrounds/7.png' ;
14- this . images [ 2 ] . src = './backgrounds/3.png' ;
15- this . images [ 3 ] . src = './backgrounds/4.png' ;
16- this . images [ 4 ] . src = './backgrounds/5.png' ;
17- this . images [ 5 ] . src = './backgrounds/6.png' ;
18- this . images [ 6 ] . src = './backgrounds/7.png' ;
19- this . images [ 7 ] . src = './backgrounds/8.png' ;
20- this . images [ 8 ] . src = './backgrounds/9.png' ;
21- this . images [ 9 ] . src = './backgrounds/10.png' ;
22- this . currentImageIndex = 0 ;
23- this . currentImage = this . images [ 0 ] ;
8+ // Load all background images
9+ this . images = [
10+ './backgrounds/1.png' ,
11+ './backgrounds/3.png' ,
12+ './backgrounds/4.png' ,
13+ './backgrounds/5.png' ,
14+ './backgrounds/6.png' ,
15+ './backgrounds/7.png' ,
16+ './backgrounds/8.png' ,
17+ './backgrounds/9.png' ,
18+ './backgrounds/10.png'
19+ ] . map ( src => {
20+ const img = new Image ( ) ;
21+ img . src = src ;
22+ return img ;
23+ } ) ;
2424
25- // Track two image positions
25+ this . currentImageIndex = 0 ;
26+ this . nextImageIndex = 1 ;
2627 this . position1 = 0 ;
2728 this . position2 = - this . virtualHeight ;
28- this . activeImage = 0 ; // Track which image is currently visible
29+ this . transitionPoint = this . virtualHeight ;
2930 }
3031
3132 update ( delta ) {
3233 // Move both positions down
3334 this . position1 += this . scrollSpeed * delta ;
3435 this . position2 += this . scrollSpeed * delta ;
3536
36- // When the first image moves completely out of view
37- if ( this . position1 >= this . virtualHeight ) {
37+ // When first image moves out of view
38+ if ( this . position1 >= this . transitionPoint ) {
3839 this . position1 = this . position2 - this . virtualHeight ;
39- this . activeImage = 1 ;
40+ this . currentImageIndex = ( this . currentImageIndex + 1 ) % this . images . length ;
4041 }
4142
42- // When the second image moves completely out of view
43- if ( this . position2 >= this . virtualHeight ) {
43+ // When second image moves out of view
44+ if ( this . position2 >= this . transitionPoint ) {
4445 this . position2 = this . position1 - this . virtualHeight ;
45- this . activeImage = 0 ;
46+ this . nextImageIndex = ( this . nextImageIndex + 1 ) % this . images . length ;
4647 }
4748 }
4849
49- draw ( scale ) {
50- if ( ! this . images [ 0 ] . complete || ! this . images [ 1 ] . complete ) return ;
50+ draw ( ) {
51+ if ( ! this . images [ this . currentImageIndex ] ?. complete ||
52+ ! this . images [ this . nextImageIndex ] ?. complete ) return ;
5153
52- const ctx = this . ctx ;
53- ctx . save ( ) ;
54- ctx . beginPath ( ) ;
55- ctx . rect ( 0 , 0 , this . virtualWidth , this . virtualHeight ) ;
56- ctx . clip ( ) ;
54+ this . ctx . save ( ) ;
55+ this . ctx . beginPath ( ) ;
56+ this . ctx . rect ( 0 , 0 , this . virtualWidth , this . virtualHeight ) ;
57+ this . ctx . clip ( ) ;
5758
58- // Draw both images at their current positions
59- ctx . drawImage (
60- this . images [ 0 ] ,
59+ // Draw current and next images
60+ this . ctx . drawImage (
61+ this . images [ this . currentImageIndex ] ,
6162 0 , this . position1 ,
6263 this . virtualWidth , this . virtualHeight
6364 ) ;
6465
65- ctx . drawImage (
66- this . images [ 1 ] ,
66+ this . ctx . drawImage (
67+ this . images [ this . nextImageIndex ] ,
6768 0 , this . position2 ,
6869 this . virtualWidth , this . virtualHeight
6970 ) ;
7071
71- ctx . restore ( ) ;
72+ this . ctx . restore ( ) ;
7273 }
7374
7475 getColorAt ( x , y ) {
@@ -79,8 +80,8 @@ class ImageBackgroundScroller {
7980 tempCanvas . height = 20 ;
8081
8182 // Draw the currently visible portion of background
82- const activeImg = this . images [ this . activeImage ] ;
83- const activePos = this . activeImage === 0 ? this . position1 : this . position2 ;
83+ const activeImg = this . images [ this . currentImageIndex ] ;
84+ const activePos = this . currentImageIndex === 0 ? this . position1 : this . position2 ;
8485
8586 tempCtx . drawImage (
8687 activeImg ,
0 commit comments