-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (104 loc) · 4.35 KB
/
script.js
File metadata and controls
129 lines (104 loc) · 4.35 KB
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
// nav bar
$(document).ready(function(){
$(window).scroll(function(){
var scroll = $(window).scrollTop();
if (scroll > 10) {
$(".navbar").css("background" , "rgba(43, 38, 38)");
}
else{
$(".navbar").css("background" , "rgba(43, 38, 38,0.5)");
}
})
})
// Text section
const resolver = {
resolve: function resolve(options, callback) {
// The string to resolve
const resolveString = options.resolveString || options.element.getAttribute('data-target-resolver');
const combinedOptions = Object.assign({}, options, {resolveString: resolveString});
function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
function randomCharacter(characters) {
return characters[getRandomInteger(0, characters.length - 1)];
};
function doRandomiserEffect(options, callback) {
const characters = options.characters;
const timeout = options.timeout;
const element = options.element;
const partialString = options.partialString;
let iterations = options.iterations;
setTimeout(() => {
if (iterations >= 0) {
const nextOptions = Object.assign({}, options, {iterations: iterations - 1});
// Ensures partialString without the random character as the final state.
if (iterations === 0) {
element.textContent = partialString;
} else {
// Replaces the last character of partialString with a random character
element.textContent = partialString.substring(0, partialString.length - 1) + randomCharacter(characters);
}
doRandomiserEffect(nextOptions, callback)
} else if (typeof callback === "function") {
callback();
}
}, options.timeout);
};
function doResolverEffect(options, callback) {
const resolveString = options.resolveString;
const characters = options.characters;
const offset = options.offset;
const partialString = resolveString.substring(0, offset);
const combinedOptions = Object.assign({}, options, {partialString: partialString});
doRandomiserEffect(combinedOptions, () => {
const nextOptions = Object.assign({}, options, {offset: offset + 1});
if (offset <= resolveString.length) {
doResolverEffect(nextOptions, callback);
} else if (typeof callback === "function") {
callback();
}
});
};
doResolverEffect(combinedOptions, callback);
}
}
/* Some GLaDOS quotes from Portal 2 chapter 9: The Part Where He Kills You
* Source: http://theportalwiki.com/wiki/GLaDOS_voice_lines#Chapter_9:_The_Part_Where_He_Kills_You
*/
const strings = [
'Thats one small step for a man, one giant leap for mankind.',
'Discover the force of the skies O Men: once recognised it can be put to use.',
'Across the sea of space, the stars are other suns.',
'The history of astronomy is a history of receding horizons.',
'There can be no centre in infinity.',
'Curiosity is the essence of our existence.',
'We are limited only by our imagination and our will to act.',
'......'
];
let counter = 0;
const options = {
// Initial position
offset: 0,
// Timeout between each random character
timeout: 5,
// Number of random characters to show
iterations: 10,
// Random characters to pick from
characters: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'x', '#', '%', '&', '-', '+', '_', '?', '/', '\\', '='],
// String to resolve
resolveString: strings[counter],
// The element
element: document.querySelector('[data-target-resolver]')
}
// Callback function when resolve completes
function callback() {
setTimeout(() => {
counter ++;
if (counter >= strings.length) {
counter = 0;
}
let nextOptions = Object.assign({}, options, {resolveString: strings[counter]});
resolver.resolve(nextOptions, callback);
}, 1000);
}
resolver.resolve(options, callback);