-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·131 lines (116 loc) · 3.57 KB
/
script.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
/**
* @description
* @param {any} container
* @param {any} density
* @param {any} interval
*/
function Ball(container, density, interval, iconPth) {
this.container = jQuery(container);
this.height = this.container.height();
this.width = this.container.width();
this.density = density || 5;
this.interval = interval || 1000;
this.logos = [];
this.logoCount = 1;
this.iconPth = iconPth;
}
/**
* @description
* @param {any} width
* @param {any} height
*/
Ball.prototype.renderLogos = function (width, height) {
var ball = null;
var ballValue = null;
for (var i = 0; i < this.density; i++) {
var x = Math.random() * this.width;
var y = Math.random() * this.height;
var maxMinDistance = null;
var validPositioning = true;
// Calculate max min distance
for (var j = 0; j < this.logos.length; j++) {
var logo = this.logos[j];
var distX = (x >= logo.x) ? (x - (logo.x + logo.width)) : (logo.x - (x + width));
var distY = (y >= logo.y) ? (y - (logo.y + logo.height)) : (logo.y - (y + height));
// If both distances are negative, there was an overlap
if (distX < 0 && distY < 0) {
validPositioning = false;
break;
}
// Estimate distance
var dist = Math.sqrt((distX < 0 ? 0 : distX * distX) + (distY < 0 ? 0 : distY * distY));
// Update max-min value
maxMinDistance = (maxMinDistance == null) ? dist : Math.min(dist, maxMinDistance);
}
// Update ball if we found a better one
if (validPositioning && (maxMinDistance == null || maxMinDistance >= ballValue)) {
ball = { x: x, y: y };
ballValue = maxMinDistance;
}
}
if (ball) {
var newLogo = { x: ball.x, y: ball.y, height: height, width: width };
newLogo.node = this.createLogo(newLogo);
newLogo.node.appendTo(this.container);
newLogo.node.css('opacity', 1)
.css({ transform: 'scale(0.9,0.9)' }).animate({
opacity: '-=.5',
transform: 'scale(50%,50%)',
left: (this.width / 2)-(newLogo.node.width()/2),
top: (this.height / 2)-(newLogo.node.height()/2)
},
2500, 'linear', function () {
jQuery(this).fadeOut(300);
});
this.logos.push(newLogo);
return true;
}
return false;
};
/**
* @description
* @param {any} spec
*/
Ball.prototype.createLogo = function (spec) {
var logo = jQuery('<div class="moving-ball"><img src="' + this.iconPth + '/' + this.logoCount + '.png" /></div>');
logo.css('height', spec.height);
logo.css('width', spec.width);
logo.css('top', spec.y);
logo.css('left', spec.x);
return logo;
};
/**
* @description
*/
Ball.prototype.createParentLogo = function (spec) {
var b = jQuery('<div class="parent-ball"><img src="' + this.iconPth + '/15.png" /></div>');
b.css({
left: (this.width / 2) - 100,
top: (this.height / 2) - 100
})
b.appendTo(this.container);
};
Ball.prototype.init = function () {
this.createParentLogo();
window.setInterval(function () {
this.logoCount++;
if (this.logoCount === 14) {
this.logoCount = 1;
}
// Replace with actual coordinates for logo
var r = Math.random();
var width = r * 100 + 50;
var height = r * 100 + 50;
// Try 100 times to place it
var attempts = 100;
while (--attempts > 0 && !this.renderLogos(width, height));
if (this.logos.length > 14) {
$(this.logos[0].node).fadeOut(400, function () { $(this).remove(); });
this.logos.splice(0, 1);
}
}.bind(this), this.interval);
}
jQuery(function () {
var ball = new Ball('#animation-container', 14, 1500, 'icons');
ball.init();
});