-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypo.js
151 lines (124 loc) · 3.92 KB
/
typo.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
//ALPHA VERSION PLAN
//1. Input word
//2. Grab each character. (array)
//3. Split it in four pieces (array of arrays)
//4. Get a piece from each character and swap it with one of the next.
//Improvements
//Step 3. 'Randomize' the splitting, changing angle.
//Step 4. 'Randomize' the amount of pieces that get swapped.
var variation = 0;
var wrapper;
var headline = "";
var positions = ["top-left", "top-right", "bot-left", "bot-right"]
var k = 0;
var i = 0;
var variationLevel = 0; //0 to 100 -- higher => crazier results
var news;
var source;
var refreshRate = 15000;
//NEWS API
const headlines =
[
{
title: "Coronavirus: How Europeans are preparing for Christmas and New Year",
source: "BBC.com"
},
{
title: "National Geographic reveals its best destinations for 2021",
source: "CNN"
},
{
title: "Spain's unemployment rate leaps to 16 percent amid coronavirus crisis",
source: "The Local"
},
{
title: "UK aid budget cuts undermine trust ahead of Cop26 summit, experts warn",
source: "Climate Home News"
},
{
title: "Armenians flee Nagorno-Karabakh after brutal war with Azerbaijan",
source: "The Guardian"
},
{
title: "Trump falsely claims victory - 'The campaign, which I won, by the way'",
source: "BBC.com"
}
];
document.onkeypress = function() {
document.getElementById('overlay').style.display = "none";
};
initFunction();
//INITIALISE
function initFunction() {
headline = headlines[k].title;
source = headlines[k].source;
wrapper = document.getElementById('headline-container');
document.getElementById('source').innerHTML = source;
generateHeadline();
//KEY PRESS FOR PC
document.onkeypress = function() {
handleKeyPress();
};
document.onkeyup = function() {
wrapper.classList.remove('shake');
};
//SHAKE FOR PHONES
var shakeEvent = new Shake({threshold: 15});
shakeEvent.start();
window.addEventListener('shake', function(){
handleKeyPress();
//alert("Shaked");
}, false);
}
//Creates div for each character
function handleKeyPress(){
destroyNews();
wrapper.classList.add('shake');
variationLevel = variationLevel + 5;
document.getElementById('overlay').style.display = "none";
generateHeadline();
}
function generateHeadline() {
for (i = 0; i < headline.length; i++) {
var character = document.createElement('div');
wrapper.appendChild(character);
character.classList.add("char");
character.classList.add("clearfix");
for (var j = 0; j < positions.length; j++) {
createPiece(character, positions[j], generateVariation(variationLevel));
}
}
}
//Creates div for each piece
function createPiece(character, position, variation) {
var piece = document.createElement('div');
piece.classList.add("piece");
piece.classList.add(position);
var index = i+variation;
piece.innerHTML = headline[index].toUpperCase();
character.appendChild(piece);
}
function generateVariation(variationLevel){
var random = Math.floor(Math.random()*(100-1+1)+1);
if (random < variationLevel){
variation = Math.floor(Math.random()*(4-1+1)+1);;
} else {
variation = 0;
}
return variation;
}
function destroyNews(){
while(wrapper.firstChild){
wrapper.removeChild(wrapper.firstChild);
}
}
window.setInterval(function(){
if (k == headlines.length - 1){
k = 0;
} else {
k++;
}
variationLevel = 0;
destroyNews();
initFunction();
}, refreshRate)