-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.bgterminal.js
102 lines (81 loc) · 2.24 KB
/
jquery.bgterminal.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
(function($) {
var _page = "";
var _tlen = 0;
var $terminal;
var $cursor;
var $code;
function _blink() {
$cursor.toggle(0);
}
$.fn.bgTerminal = function (options, callback) {
$terminal = $(this);
var settings = $.extend({
url: null,
speed: 200,
wait: 1000,
glow: false
}, options );
function _emulateTerminal() {
var text = _page.substring(0, _tlen);
$code.text(text);
if (_tlen < _page.length) {
setTimeout(_emulateTerminal, (_page[_tlen] == '\n') ? settings.wait : settings.speed);
_tlen++;
}
}
function _setSpeed(speed, wait) {
speed = parseInt(speed);
wait = parseInt(wait);
settings.speed = (speed < 0) ? 1 : speed;
settings.wait = (wait < 0) ? 1 : wait;
}
function speed() {
return {
speed: settings.speed,
wait: settings.wait
}
}
function speedUp(amount) {
amount = parseInt(amount);
if (!amount || amount < 0) {
amount = 1.5;
}
_setSpeed(settings.speed / amount, settings.wait / amount);
}
function speedDown(amount) {
amount = parseInt(amount);
if (!amount || amount < 0) {
amount = 1.5;
}
_setSpeed(settings.speed * amount, settings.wait * amount);
}
if (settings.url) {
$.get(settings.url, function(res) {
_page = res;
$terminal.addClass('bgterminal');
// create cursor
$cursor = $('<span>');
$cursor.addClass('cursor');
$cursor.html(' ');
setInterval(_blink, 400);
// add code element
$code = $('<span>');
$terminal.append($code);
$terminal.append($cursor);
if (settings.glow) {
$terminal.addClass('bgterminal-glow');
}
$terminal.data('bgterminal', {
speedUp: speedUp,
speedDown: speedDown,
speed: speed
});
_emulateTerminal();
});
}
if (typeof callback == "function") {
callback(this);
}
return this;
}
})(jQuery);