-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdancingtext.js
87 lines (87 loc) · 2.64 KB
/
dancingtext.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
var DancingText = (function() {
var module = {};
module.WaveCharacter = function(let) {
this.letter = let;
this.size = "100%";
this.html = function() {
return "<span class=\"dancingcharacter\" style=\"font-size: " + this.size + "\">" + this.letter + "</span>";
}
};
module.htmlFromWaveCharacterArray = function(arr) {
var ret = "";
arr.forEach(function(wavechar, index, array) {
ret += wavechar.html();
});
return ret;
};
module.addCSSRule = function(selector, rules, index) {
sheet = document.styleSheets[0];
if(sheet.insertRule) {
sheet.insertRule(selector + "{" + rules + "}", index);
} else {
sheet.addRule(selector, rules, index);
}
};
module.getRandomInt = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
module.dancetext = function(elm) {
var characters = [];
var index = 0;
var newinner = "";
var inner = elm.innerHTML;
console.log("got inner: " + inner);
var index = -1;
for (i = 0; i < inner.length; i++) {
characters[i] = new this.WaveCharacter(inner.charAt(i));
}
elm.innerHTML = this.htmlFromWaveCharacterArray(characters);
var children = elm.children;
var timeoutfunc = function() {
children.objectAtIndex(index - 1).style.fontSize = "100%";
children.objectAtIndex(index).style.fontSize = "100%";
children.objectAtIndex(index + 1).style.fontSize = "100%";
oldindex = index;
while (index == oldindex)
index = module.getRandomInt(0, characters.length - 1);
if (index > 0)
children.objectAtIndex(index - 1).style.fontSize = "120%";
children.objectAtIndex(index).style.fontSize = "150%";
if (index < children.length - 1)
children.objectAtIndex(index + 1).style.fontSize = "120%";
setTimeout(timeoutfunc, 700);
}
setTimeout(timeoutfunc, 10);
};
module.dance = function() {
this.addCSSRule(".dancingcharacter", "transition: all 500ms; -webkit-transition: all 500ms;", 1);
elms = document.getElementsByClassName("dancingtext");
console.log("merh " + elms.length + " " + elms);
for (var i = 0; i < elms.length; i++) {
(function(){
var j = i;
module.dancetext(elms[j]);
}());
}
};
return module;
}());
if (typeof Array.prototype.objectAtIndex === 'undefined') {
var oai = function(index) {
if (index < 0) {
index = this.length + index;
} else if (index >= this.length) {
index = this.length % index;
}
return this[index];
};
Object.defineProperty(Array.prototype, "objectAtIndex", {
value : oai
});
Object.defineProperty(NodeList.prototype, "objectAtIndex", {
value : oai
});
Object.defineProperty(HTMLCollection.prototype, "objectAtIndex", {
value : oai
});
}