1+ import { WorleyNoise } from "./worleyNoise.js" ;
2+
3+ export class Background {
4+ constructor ( game , context ) {
5+ this . game = game ;
6+ this . context = context ;
7+ this . canvas = context . canvas ;
8+
9+ this . width = screen . width ;
10+ this . height = screen . height ;
11+ this . totalHeight = this . height * 2 ;
12+ this . y = this . height - this . totalHeight ;
13+ }
14+
15+ start ( ) {
16+ // const noiseGen = new Worker(`./js/worleyNoise.js`, {type:'module'});
17+ // this.noiseGen = noiseGen;
18+
19+ // const { width, height } = screen;
20+ // this.totalHeight = height;
21+ // this.screenPixelsArrLen = width * height * 4;
22+ // noiseGen.onmessage = e => {
23+ // const { data } = e.data;
24+ // this.currentRow = 0;
25+ // this.data = data;
26+ // this.currentData = new Uint8ClampedArray(this.screenPixelsArrLen).fill(255);
27+ // this.updateBackgroundImage();
28+ // console.log('Data recieved');
29+ // noiseGen.terminate();
30+ // };
31+ // noiseGen.onerror = e => {
32+ // console.log(e);
33+ // noiseGen.terminate();
34+ // };
35+
36+ // noiseGen.postMessage({ action: 'start', width, height: this.totalHeight });
37+ // Worker
38+ // noiseGen.postMessage({ action: 'animate' });
39+
40+ // In Real-Time
41+ const { width, height } = screen ;
42+ this . noise = new WorleyNoise ( width , this . totalHeight , { maxDistForRatio : 350 , ratioPower : 1.5 } ) ;
43+ this . noise . createRandomPoints ( ) ;
44+ this . noise . createBackgroundImage ( ) ;
45+
46+ const canvas = document . createElement ( 'canvas' ) ;
47+ canvas . width = width ;
48+ canvas . height = this . totalHeight ;
49+ const context = canvas . getContext ( '2d' ) ;
50+
51+ this . noise . draw ( context ) ;
52+ const background = new Image ( ) ;
53+ background . src = canvas . toDataURL ( ) ;
54+ background . onload = ( ) => {
55+ this . backgroundImage = background ;
56+ }
57+ }
58+
59+ updateBackgroundImage ( ) {
60+ // const { width, height } = screen;
61+ // const startIndex = (width * (this.totalHeight - this.currentRow));
62+ // const endIndex = (startIndex + this.screenPixelsArrLen * 0.25);
63+ // const currentData = this.data.slice(startIndex * 4, endIndex * 4);
64+
65+ // No need for this, instead i will use parallax of Images
66+ // let row = this.currentRow;
67+ // for(let j = 0; j < height; j++) {
68+ // row = (row - 1 + this.totalHeight) % this.totalHeight;
69+ // for(let i = 0; i < width; i++) {
70+ // const index = (i + j * width) * 4;
71+ // const fetchIndex = (i + row * width) * 4;
72+ // this.currentData[index] = this.data[fetchIndex];
73+ // this.currentData[index+1] = this.data[fetchIndex+1];
74+ // this.currentData[index+2] = this.data[fetchIndex+2];
75+ // }
76+ // }
77+ // this.backgroundImage = new ImageData(this.currentData, width, height);
78+ }
79+
80+ update ( deltaTime ) {
81+ // Worker
82+ // if(this.noiseGen) {
83+ // const { baseSpeed: speed } = this.game.player;
84+ // this.noiseGen.postMessage({ action: 'update', speed });
85+ // }
86+
87+ // Real-Time
88+ // const { baseSpeed: speed } = this.game.player;
89+ // this.noise.update(speed);
90+ // this.backgroundImage = this.noise.imageData;
91+
92+ // Bulk
93+ // if(this.backgroundImage) {
94+ // const { speedX, speedY, baseSpeed } = this.game.player;
95+ // const windSpeed = 10;
96+ // const isMoving = speedX || speedY;
97+ // const frameMultiplier = deltaTime * 0.001 * 20;
98+ // this.currentRow += Math.floor(((isMoving ? windSpeed : 0) + baseSpeed) * frameMultiplier);
99+ // this.updateBackgroundImage();
100+ // }
101+ if ( this . backgroundImage ) {
102+ const { speedX, speedY, baseSpeed } = this . game . player ;
103+ const windSpeed = 10 ;
104+ const isMoving = speedX || speedY ;
105+ const frameMultiplier = deltaTime * 0.001 * 20 ;
106+ this . y += ( windSpeed + ( isMoving ? baseSpeed : 0 ) ) * frameMultiplier ;
107+
108+ if ( this . y > this . height ) {
109+ this . y -= this . totalHeight ;
110+ }
111+ }
112+ }
113+
114+ draw ( ) {
115+ if ( this . backgroundImage ) {
116+ const { context, width, height, totalHeight } = this ;
117+ context . drawImage ( this . backgroundImage , 0 , 0 , width , totalHeight , 0 , this . y - totalHeight , width , totalHeight ) ;
118+ context . drawImage ( this . backgroundImage , 0 , 0 , width , totalHeight , 0 , this . y , width , totalHeight ) ;
119+ // this.context.putImageData(this.backgroundImage, 0, 0);
120+ }
121+ }
122+ }
0 commit comments