summaryrefslogtreecommitdiff
path: root/public/assets/stars/flyingthroughspace.js
diff options
context:
space:
mode:
author“Ryder <“r@okfoc.us”>2016-04-12 20:46:44 -0400
committer“Ryder <“r@okfoc.us”>2016-04-12 20:46:44 -0400
commitedb135e0d2a441e072571601d17c858319c7b6fa (patch)
tree036d6c260d745b5671545136aef45d426c365815 /public/assets/stars/flyingthroughspace.js
parentfbacf50ceca813f1682ab458fa4c275fb8630991 (diff)
stars dude
Diffstat (limited to 'public/assets/stars/flyingthroughspace.js')
-rwxr-xr-xpublic/assets/stars/flyingthroughspace.js81
1 files changed, 81 insertions, 0 deletions
diff --git a/public/assets/stars/flyingthroughspace.js b/public/assets/stars/flyingthroughspace.js
new file mode 100755
index 0000000..7587c71
--- /dev/null
+++ b/public/assets/stars/flyingthroughspace.js
@@ -0,0 +1,81 @@
+(function() {
+
+ var STAR_COUNT = 500;
+ var WARP_SPEED = 0.02;
+ var FPS = 30;
+
+ var getRandom = function(lbound, ubound) {
+ return window.Math.random() * (ubound - lbound) + lbound;
+ };
+
+ var windowCenter = {
+ x: window.innerWidth / 2,
+ y: window.innerHeight / 2
+ };
+
+ var Star = function() {
+ this.element = document.createElement('div');
+ this.element.className = 'star';
+ this.randomizeSpawn();
+ document.body.appendChild(this.element);
+ };
+
+ Star.prototype.randomizeSpawn = function() {
+ this.x = getRandom(1, window.innerWidth);
+ this.y = getRandom(1, window.innerHeight);
+ this.brightness = 1;//getRandom(1, 5);
+ this.distance = getRandom(0.01, 6);
+ }
+
+ Star.prototype.isOutsideField = function() {
+ return this.x < 0 ||
+ this.x > window.innerWidth ||
+ this.y > window.innerHeight ||
+ this.y < 0;
+ };
+
+ Star.prototype.setPosition = function() {
+ this.element.style.left = this.x + 'px';
+ this.element.style.top = this.y + 'px';
+ this.element.style.width = this.brightness + 'px';
+ this.element.style.height = this.brightness + 'px';
+ };
+
+ var stars = [];
+
+ var incrementFrame = function() {
+
+ for (var i = 0; i < STAR_COUNT; i++) {
+
+ var star = stars[i] = stars[i] || new Star();
+
+ // Split position by splitting into x & y vectors
+ star.x -= (windowCenter.x - star.x) * WARP_SPEED * star.distance;
+ star.y -= (windowCenter.y - star.y) * WARP_SPEED * star.distance;
+
+ if (star.isOutsideField()) {
+ // Reloate star
+ star.randomizeSpawn();
+ } else {
+ // Increase brightness
+ star.brightness += (star.distance * 0.05);
+ }
+
+ star.setPosition();
+ }
+ };
+
+ // Follow cursor
+ window.addEventListener('mousemove', function(e) {
+ windowCenter.x = e.clientX;
+ windowCenter.y = e.clientY;
+ });
+
+ // Start drawing
+ window.setInterval(function(){
+ if (document.body) {
+ incrementFrame();
+ }
+ }, 1000 / FPS);
+
+})(); \ No newline at end of file