XGravity/Public/ecma/Backgrounds/StarsBackground.ecma.js

66 lines
2.2 KiB
JavaScript

"use strict";
const StarsBackground = (function(){
const StarsBackground = function(xgravity){
const self = this;
const stars = [];
const init_stars = (width, height) => {
stars.length = 0;
for(let i = 0; i < xgravity.values.bg_stars_count; i++)
stars.push({
x : Math.random() * width,
y : Math.random() * height,
size : Math.random() * (xgravity.values.bg_stars_size_maximum - xgravity.values.bg_stars_size_minimum) + xgravity.values.bg_stars_size_minimum,
speed : Math.random() * (xgravity.values.bg_stars_speed_maximum - xgravity.values.bg_stars_speed_minimum) + xgravity.values.bg_stars_speed_minimum,
alpha : Math.random() * 0.8 + 0.2
});
};
this.resize = (width, height) => {
init_stars(width, height);
};
this.render = (ctx, width, height, delta_time) => {
ctx.fillStyle = "#020408";
ctx.fillRect(0, 0, width, height);
stars.length || init_stars(width, height);
for(const star of stars){
star.x += xgravity.values.bg_stars_direction_x * star.speed * (delta_time * 60);
star.y += xgravity.values.bg_stars_direction_y * star.speed * (delta_time * 60);
if(star.y > height){
star.y = 0;
star.x = Math.random() * width;
}else if(star.y < 0){
star.y = height;
star.x = Math.random() * width;
};
if(star.x > width){
star.x = 0;
star.y = Math.random() * height;
}else if(star.x < 0){
star.x = width;
star.y = Math.random() * height;
};
ctx.fillStyle = Common.string_variables(xgravity.values.bg_stars_color, {
alpha : star.alpha.toFixed(2)
});
ctx.fillRect(star.x, star.y, star.size, star.size);
};
};
};
return StarsBackground;
})();