-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackground.js
70 lines (55 loc) · 1.67 KB
/
Background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//Описание структуры Звезда и всех связных методов и функций.
//Глобальная переменная, в которой хранятся звезды
var stars = [];
//Конструктор
function Star() {
this.x = random(-width, width);
this.y = random(-height, height);
this.z = random(width);
this.pz = this.z;
}
//Обновление положения звезды
Star.prototype.update = function(deltaTime) {
this.z = this.z - starSpeed * deltaTime;
if (this.z < 1) {
this.z = width;
this.x = random(-width, width);
this.y = random(-height, height);
this.pz = this.z;
}
}
//Отображение звезды
Star.prototype.show = function() {
fill(255);
noStroke();
var sx = map(this.x / this.z, 0, 1, 0, width);
var sy = map(this.y / this.z, 0, 1, 0, height);
var r = map(this.z, 0, width, 16, 0);
var px = map(this.x / this.pz, 0, 1, 0, width);
var py = map(this.y / this.pz, 0, 1, 0, height);
this.pz = this.z;
stroke(255);
line(px, py, sx, sy);
}
//Инициализация звезд
function initializeBackground() {
for (var i = 0; i < STAR_COUNT; i++) {
stars[i] = new Star();
}
}
//Обновления положения всех звезд
function updateBackground(deltaTime) {
for (var i = 0; i < stars.length; i++) {
stars[i].update(deltaTime);
}
}
//Отображения всех звезд
function drawBackground() {
background(0);
push();
translate(width / 2, height / 2);
for (var i = 0; i < stars.length; i++) {
stars[i].show();
}
pop();
}