-
Notifications
You must be signed in to change notification settings - Fork 1
/
RadioActive.js
91 lines (74 loc) · 2.05 KB
/
RadioActive.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
function randHexColor(){
return '#'+Math.floor(Math.random()*16777215).toString(16);
}
function rand(max, min){
min = min || 0;
max = max || 1;
return min + Math.random() * (max-min);
}
function GeigerEmmiter(el, radioactivity){
this.radioactivity = radioactivity;
this.tmin = rand(500);
this.tmax = this.tmin + rand(1500);
this.el = el;
}
GeigerEmmiter.prototype.start = function(){
if(!this.started){
var self = this;
var geg = function (){
var evt = new CustomEvent('activity',{
detail: {
value: rand(1, 0) * (+self.el.dataset.radioactivity),
type: self.el.dataset.type
},
bubbles: true,
cancelable: true
});
self.el.dispatchEvent(evt);
self.started = setTimeout(geg, rand(self.tmax, self.tmin));
};
self.started = setTimeout(geg, rand(self.tmax, self.tmin));
}
};
GeigerEmmiter.prototype.stop = function(){
clearTimeout(this.started);
};
var RadioactiveEl = {
docWidth : document.body.clientWidth,
docHeight : document.body.clientHeight,
create : function () {
var size = rand(250, 10);
var el = document.createElement('div');
var radioactivity = rand();
el.dataset.type = el.style.background = randHexColor();
el.dataset.radioactivity = radioactivity;
el.style.height = el.style.width = size + 'px';
el.style.top = rand(1,0) * (this.docHeight - size)+ 'px';
el.style.left = rand(1,0) * (this.docWidth - size) + 'px';
el.style.position = 'absolute';
el.style.borderRadius = '50%';
el.style.opacity = '0.5';
var rEl = new GeigerEmmiter(el, radioactivity);
rEl.start();
return rEl;
}
};
var App = {
init: function(){
this.styleDocument();
this.markup();
},
styleDocument:function(){
document.body.style.width= '100%';
document.body.style.height= '100%';
document.body.parentElement.style.width = '100%';
document.body.parentElement.style.height = '100%';
},
markup: function(){
var count = 100;
while (count--){
var rEl = RadioactiveEl.create();
document.body.appendChild(rEl.el);
}
}
}