-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackholeStyle.js
199 lines (167 loc) · 6.04 KB
/
blackholeStyle.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
blackhole("#blackhole");
function blackhole(element) {
var h = $(element).height(),
w = $(element).width(),
cw = w,
ch = h,
maxorbit = 80, // distance from center
centery = ch / 2,
centerx = cw / 2;
var startTime = new Date().getTime();
var currentTime = 0;
var stars = [],
collapse = false, // if hovered
expanse = false; // if clicked
var canvas = $("<canvas/>").attr({ width: cw , height: ch }).appendTo(element),
context = canvas.get(0).getContext("2d");
context.globalCompositeOperation = "multiply";
function setDPI(canvas, dpi) {
// Set up CSS size if it's not set up already
if (!canvas.get(0).style.width)
canvas.get(0).style.width = canvas.get(0).width + "px";
if (!canvas.get(0).style.height)
canvas.get(0).style.height = canvas.get(0).height + "px";
var scaleFactor = dpi / 96;
canvas.get(0).width = Math.ceil(canvas.get(0).width * scaleFactor);
canvas.get(0).height = Math.ceil(canvas.get(0).height * scaleFactor);
var ctx = canvas.get(0).getContext("2d");
ctx.scale(scaleFactor, scaleFactor);
}
function rotate(cx, cy, x, y, angle) {
var radians = angle,
cos = Math.cos(radians),
sin = Math.sin(radians),
nx = cos * (x - cx) + sin * (y - cy) + cx,
ny = cos * (y - cy) - sin * (x - cx) + cy;
return [nx, ny];
}
setDPI(canvas, 192);
var star = function () {
// Get a weighted random number, so that the majority of stars will form in the center of the orbit
var rands = [];
rands.push(Math.random() * (maxorbit / 2) + 1);
rands.push(Math.random() * (maxorbit / 2) + maxorbit);
this.orbital =
rands.reduce(function (p, c) {
return p + c;
}, 0) / rands.length;
// Done getting that random number, it's stored in this.orbital
this.x = centerx; // All of these stars are at the center x position at all times
this.y = centery + this.orbital; // Set Y position starting at the center y + the position in the orbit
this.yOrigin = centery + this.orbital; // this is used to track the particles origin
this.speed = ((Math.floor(Math.random() * 2.5) + 1.5) * Math.PI) / 180; // The rate at which this star will orbit
this.rotation = 0; // current Rotation
this.startRotation = ((Math.floor(Math.random() * 360) + 1) * Math.PI) / 180; // Starting rotation. If not random, all stars will be generated in a single line.
this.id = stars.length; // This will be used when expansion takes place.
this.collapseBonus = this.orbital - maxorbit * 0.7; // This "bonus" is used to randomly place some stars outside of the blackhole on hover
if (this.collapseBonus < 0) {
// if the collapse "bonus" is negative
this.collapseBonus = 0; // set it to 0, this way no stars will go inside the blackhole
}
stars.push(this);
this.color = "rgba(255,255,255," + (1 - this.orbital / 255) + ")"; // Color the star white, but make it more transparent the further out it is generated
this.hoverPos = centery + maxorbit / 2 + this.collapseBonus; // Where the star will go on hover of the blackhole
this.expansePos =
centery + (this.id % 100) * -10 + (Math.floor(Math.random() * 20) + 1); // Where the star will go when expansion takes place
this.prevR = this.startRotation;
this.prevX = this.x;
this.prevY = this.y;
// The reason why I have yOrigin, hoverPos and expansePos is so that I don't have to do math on each animation frame. Trying to reduce lag.
};
star.prototype.draw = function () {
// the stars are not actually moving on the X axis in my code. I'm simply rotating the canvas context for each star individually so that they all get rotated with the use of less complex math in each frame.
if (!expanse) {
this.rotation = this.startRotation + currentTime * this.speed;
if (!collapse) {
// not hovered
if (this.y > this.yOrigin) {
this.y -= 2.5;
}
if (this.y < this.yOrigin - 4) {
this.y += (this.yOrigin - this.y) / 10;
}
} else {
// on hover
this.trail = 1;
if (this.y > this.hoverPos) {
this.y -= (this.hoverPos - this.y) / -5;
}
if (this.y < this.hoverPos - 4) {
this.y += 2.5;
}
}
} else {
this.rotation = this.startRotation + currentTime * (this.speed / 2);
if (this.y > this.expansePos) {
this.y -= Math.floor(this.expansePos - this.y) / -140;
}
}
context.save();
context.fillStyle = this.color;
context.strokeStyle = this.color;
context.beginPath();
var oldPos = rotate(centerx, centery, this.prevX, this.prevY, -this.prevR);
context.moveTo(oldPos[0], oldPos[1]);
context.translate(centerx, centery);
context.rotate(this.rotation);
context.translate(-centerx, -centery);
context.lineTo(this.x, this.y);
context.stroke();
context.restore();
this.prevR = this.rotation;
this.prevX = this.x;
this.prevY = this.y;
};
$(".centerHover").on("click", function () {
collapse = false;
expanse = true;
$(this).addClass("open");
$(".fullpage").addClass("open");
setTimeout(function () {
$(".header .welcome").removeClass("gone");
}, 500);
});
$(".centerHover").on("mouseover", function () {
if (expanse == false) {
collapse = true;
}
});
$(".centerHover").on("mouseout", function () {
if (expanse == false) {
collapse = false;
}
});
window.requestFrame = (function () {
return (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function (callback) {
window.setTimeout(callback, 1000 / 60);
}
);
})();
function loop() {
var now = new Date().getTime();
currentTime = (now - startTime) / 50;
// context.fillStyle = "rgba(0,0,0,0.3)"; // somewhat clear the context, this way there will be trails behind the stars
context.fillRect(0, 0, cw, ch);
for (var i = 0; i < stars.length; i++) {
// For each star
if (stars[i] != stars) {
stars[i].draw(); // Draw it
}
}
requestFrame(loop);
}
function init(time) {
// context.fillStyle = "rgba(0,0,0,1)"; // Initial clear of the canvas, to avoid an issue where it all gets too dark
context.fillRect(0, 0, cw, ch);
for (var i = 0; i < 2500; i++) {
// create 2500 stars
new star();
}
loop();
}
init();
}