This repository has been archived by the owner on Sep 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
95 lines (86 loc) · 3.02 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
/**
* Created by Burak on 18.04.2016.
*/
(function() {
"use strict";
var timer;
var word = 0;
var frames = [];
window.onload = function() {
var start = document.getElementById("start");
start.onclick = goTimer;
document.getElementById("stop").disabled = true;
var stop = document.getElementById("stop");
stop.onclick = stopIt;
var speed = document.getElementById("speed");
speed.onchange = changeSpeed;
var size = document.getElementById("size");
size.onchange = changeSize;
};
//a function to start the time
function goTimer() {
var input = document.getElementById("mytextarea").value;
var raw = input.replace(/\n/g, ' ');
var wordStrings = raw.split(' ');
document.getElementById("start").disabled = true;
document.getElementById("mytextarea").disabled = true;
document.getElementById("stop").disabled = false;
frames = setPunctuation(wordStrings);
timer = setInterval(frame, 171);
}
//a function to generate the output
function frame() {
if (word < frames.length) {
document.getElementById("output").innerHTML = frames[word];
word++;
} else {
stopIt();
}
}
//a function to check if there are words end with any punctuation
//if there is, the last punctuation with the specific requirement will be removed
//and the word with thse specific punctuation required will be added to the array again
function setPunctuation(wordStrings) {
var words = [];
for (var i = 0; i < wordStrings.length; i++) {
var eachWord = wordStrings[i];
if (eachWord.match(/[,!?;:.]$/g)) {
eachWord = eachWord.replace(/[,!?;:.]?$/g, "");
words.push(eachWord);
}
words.push(eachWord);
}
return words;
}
//a function to stop the animation
function stopIt() {
clearInterval(timer);
timer = null;
word = 0;
document.getElementById("output").innerHTML = "";
document.getElementById("start").disabled = false;
document.getElementById("mytextarea").disabled = false;
document.getElementById("stop").disabled = true;
}
//a function to change the size
function changeSize() {
var wordSize = document.querySelectorAll("input");
for (var i = 0; i < wordSize.length; i++) {
if (wordSize[i].checked) {
document.getElementById("output").style.fontSize = parseInt(wordSize[i].value) + "pt";
}
}
}
//a function to change the speed
function changeSpeed() {
var speeds = document.querySelectorAll("option");
for (var i = 0; i < speeds.length; i++) {
if (timer) {
if (speeds[i].selected) {
clearInterval(timer);
timer = setInterval(frame, speeds[i].value);
}
}
}
}
})();