so recently I have found this awesome galaxy effect on codepen: https://codepen.io/zeztron/pen/MPNxxR
I tried to modify the JavaScript and couldn’t figure out a way to change it so instead of it feeling like Zooming out, making it to feel like Zooming in.
Can anyone help? Thank you!!
Here are the codes:
JavaScript
x
4
1
<body>
2
<canvas id="canvas"></canvas>
3
</body>
4
JavaScript
1
16
16
1
body {
2
background: black;
3
height: 100%;
4
min-height: 100%;
5
}
6
7
8
#canvas {
9
height: 100%;
10
width: 100%;
11
position: fixed;
12
top: 0;
13
left: 0;
14
opacity: 0;
15
}
16
JavaScript
1
94
94
1
var Space = {
2
init: function(){
3
var self = this;
4
this.config = {
5
perspective: 3,
6
star_color: '255, 255, 255',
7
speed: 1,
8
stars_count: 2
9
};
10
this.canvas = document.getElementById('canvas');
11
this.context = canvas.getContext('2d');
12
this.start();
13
window.onresize = function(){
14
self.start();
15
};
16
},
17
18
start: function(){
19
var self = this;
20
21
this.canvas.width = this.canvas.offsetWidth;
22
this.canvas.height = this.canvas.offsetHeight;
23
this.canvas_center_x = this.canvas.width / 2;
24
this.canvas_center_y = this.canvas.height / 2;
25
26
this.stars_count = this.canvas.width / this.config.stars_count;
27
this.focal_length = this.canvas.width / this.config.perspective;
28
this.speed = this.config.speed * this.canvas.width / 2000;
29
30
this.stars = [];
31
32
for(i = 0; i < this.stars_count; i++){
33
this.stars.push({
34
x: Math.random() * this.canvas.width,
35
y: Math.random() * this.canvas.height,
36
z: Math.random() * this.canvas.width,
37
});
38
}
39
40
window.cancelAnimationFrame(this.animation_frame);
41
this.canvas.style.opacity = 1;
42
43
this.cow = new Image();
44
this.cow.src = 'https://gallery.yopriceville.com/var/resizes/Free-Clipart-Pictures/Fast-Food-PNG-Clipart/Hamburger_PNG_Vector_Picture.png?m=1507172108';
45
this.cow.onload = function(){
46
self.render();
47
}
48
},
49
50
render: function(){
51
var self = this;
52
this.animation_frame = window.requestAnimationFrame(function(){
53
self.render();
54
});
55
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
56
for(var i = 0, length = this.stars.length; i < length; i += 1){
57
var star = this.stars[i];
58
star.z -= this.speed;
59
if(star.z <= 0) {
60
this.stars[i] = {
61
x: Math.random() * this.canvas.width,
62
y: Math.random() * this.canvas.height,
63
z: this.canvas.width,
64
};
65
}
66
67
var star_x = (star.x - this.canvas_center_x) * (this.focal_length / star.z) + this.canvas_center_x;
68
var star_y = (star.y - this.canvas_center_y) * (this.focal_length / star.z) + this.canvas_center_y;
69
var star_radius = Math.max(0, 1.4 * (this.focal_length / star.z) / 2);
70
var star_opacity = 1.2 - star.z / this.canvas.width;
71
var cow_width = Math.max(0.1, 100 * (this.focal_length / star.z) / 2);
72
73
if(star.cow){
74
this.context.save();
75
this.context.translate((star_x-cow_width)+(cow_width/2), (star_y-cow_width)+(cow_width/2));
76
this.context.rotate(star.z/star.rotation_speed);
77
this.context.translate(-((star_x-cow_width)+(cow_width/2)), -((star_y-cow_width)+(cow_width/2)));
78
this.context.globalAlpha = star_opacity;
79
this.context.drawImage(this.cow, 0, 0, this.cow.width, this.cow.width, star_x-cow_width, star_y-cow_width, cow_width, cow_width);
80
this.context.restore();
81
} else {
82
this.context.fillStyle = 'rgba(' + this.config.star_color + ',' + star_opacity + ')';
83
this.context.beginPath();
84
this.context.arc(star_x, star_y, star_radius, 0, Math.PI * 2);
85
this.context.fill();
86
}
87
}
88
}
89
};
90
91
window.onload = function(){
92
Space.init();
93
};
94
Advertisement
Answer
The z-axis
of each star is decreasing from a random point inside the canvas
to 0
to create a zooming-out effect. To reverse it, simply increase the z-axis
, not the speed.
This is the zooming-out:
JavaScript
1
10
10
1
var star = this.stars[i];
2
star.z -= this.speed; // decreasing
3
if(star.z <= 0) {
4
this.stars[i] = {
5
x: Math.random() * this.canvas.width,
6
y: Math.random() * this.canvas.height,
7
z: this.canvas.width, // reset the z-axis
8
};
9
}
10
And this is the zooming-in:
JavaScript
1
9
1
star.z += this.speed; // increasing
2
if(star.z > this.canvas.width){
3
this.stars[i] = {
4
x: Math.random() * this.canvas.width,
5
y: Math.random() * this.canvas.height,
6
z: Math.random() * this.canvas.width, // reset to a random point
7
};
8
}
9
You need to reset the z-axis
when it reaches a certain point so that it could run infinitely.
JavaScript
1
93
93
1
var Space = {
2
init: function(){
3
var self = this;
4
this.config = {
5
perspective: 3,
6
star_color: '255, 255, 255',
7
speed: 1,
8
stars_count: 2
9
};
10
this.canvas = document.getElementById('canvas');
11
this.context = canvas.getContext('2d');
12
this.start();
13
window.onresize = function(){
14
self.start();
15
};
16
},
17
18
start: function(){
19
var self = this;
20
21
this.canvas.width = this.canvas.offsetWidth;
22
this.canvas.height = this.canvas.offsetHeight;
23
this.canvas_center_x = this.canvas.width / 2;
24
this.canvas_center_y = this.canvas.height / 2;
25
26
this.stars_count = this.canvas.width / this.config.stars_count;
27
this.focal_length = this.canvas.width / this.config.perspective;
28
this.speed = this.config.speed * this.canvas.width / 2000;
29
30
this.stars = [];
31
32
for(i = 0; i < this.stars_count; i++){
33
this.stars.push({
34
x: Math.random() * this.canvas.width,
35
y: Math.random() * this.canvas.height,
36
z: Math.random() * this.canvas.width,
37
});
38
}
39
40
window.cancelAnimationFrame(this.animation_frame);
41
this.canvas.style.opacity = 1;
42
43
this.cow = new Image();
44
this.cow.src = 'https://gallery.yopriceville.com/var/resizes/Free-Clipart-Pictures/Fast-Food-PNG-Clipart/Hamburger_PNG_Vector_Picture.png?m=1507172108';
45
this.cow.onload = function(){
46
self.render();
47
}
48
},
49
50
render: function(){
51
var self = this;
52
this.animation_frame = window.requestAnimationFrame(function(){
53
self.render();
54
});
55
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
56
for(var i = 0, length = this.stars.length; i < length; i += 1){
57
var star = this.stars[i];
58
star.z += this.speed;
59
if(star.z > this.canvas.width) {
60
this.stars[i] = {
61
x: Math.random() * this.canvas.width,
62
y: Math.random() * this.canvas.height,
63
z: Math.random() * this.canvas.width,
64
};
65
}
66
67
var star_x = (star.x - this.canvas_center_x) * (this.focal_length / star.z) + this.canvas_center_x;
68
var star_y = (star.y - this.canvas_center_y) * (this.focal_length / star.z) + this.canvas_center_y;
69
var star_radius = Math.max(0, 1.4 * (this.focal_length / star.z) / 2);
70
var star_opacity = 1.2 - star.z / this.canvas.width;
71
var cow_width = Math.max(0.1, 100 * (this.focal_length / star.z) / 2);
72
73
if(star.cow){
74
this.context.save();
75
this.context.translate((star_x-cow_width)+(cow_width/2), (star_y-cow_width)+(cow_width/2));
76
this.context.rotate(star.z/star.rotation_speed);
77
this.context.translate(-((star_x-cow_width)+(cow_width/2)), -((star_y-cow_width)+(cow_width/2)));
78
this.context.globalAlpha = star_opacity;
79
this.context.drawImage(this.cow, 0, 0, this.cow.width, this.cow.width, star_x-cow_width, star_y-cow_width, cow_width, cow_width);
80
this.context.restore();
81
} else {
82
this.context.fillStyle = 'rgba(' + this.config.star_color + ',' + star_opacity + ')';
83
this.context.beginPath();
84
this.context.arc(star_x, star_y, star_radius, 0, Math.PI * 2);
85
this.context.fill();
86
}
87
}
88
}
89
};
90
91
window.onload = function(){
92
Space.init();
93
};
JavaScript
1
15
15
1
body {
2
background: black;
3
height: 100%;
4
min-height: 100%;
5
}
6
7
8
#canvas {
9
height: 100%;
10
width: 100%;
11
position: fixed;
12
top: 0;
13
left: 0;
14
opacity: 0;
15
}
JavaScript
1
3
1
<body>
2
<canvas id="canvas"></canvas>
3
</body>