-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathturingtype.js
More file actions
110 lines (97 loc) · 3.38 KB
/
turingtype.js
File metadata and controls
110 lines (97 loc) · 3.38 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
// Generated by CoffeeScript 1.10.0
(function() {
var TuringType, floor, rand,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
rand = Math.random;
floor = Math.floor;
TuringType = (function() {
TuringType.prototype.int = 100;
TuringType.prototype.accuracy = .95;
TuringType.prototype.keys = 'qwertyuiopasdfghjklzxcvbnm,./;-=[]'.split('');
function TuringType(el, text, options) {
var accuracy, interval, ref, tag;
this.el = el;
this.text = text;
this.options = options != null ? options : {};
this.clear = bind(this.clear, this);
this.type = bind(this.type, this);
if (!(this instanceof TuringType)) {
return new TuringType(this.el, this.text, this.options);
}
if (typeof this.el === 'string') {
this.el = document.querySelector(this.el);
}
this.len = this.text.length;
this.i = 0;
ref = this.options, accuracy = ref.accuracy, interval = ref.interval, this.callback = ref.callback;
this.accuracy = accuracy != null ? accuracy : this.accuracy;
this.int = interval != null ? interval : this.int;
if (tag = this.el.tagName.toLowerCase() === 'textarea' || tag === 'input') {
this.attr = 'value';
this.el.focus();
} else {
this.attr = 'innerText';
}
this.type();
}
TuringType.prototype.type = function() {
if (this.i === this.len + 1) {
return typeof this.callback === "function" ? this.callback() : void 0;
}
if (rand() > this.accuracy) {
this.el[this.attr] = this.text.slice(0, this.i) + this.keys[floor(rand() * this.keys.length)];
return this.timer = setTimeout((function(_this) {
return function() {
_this.el[_this.attr] = _this.text.slice(0, _this.i);
return _this.timer = setTimeout(_this.type, rand() * _this.int + _this.int * .8);
};
})(this), this.int * 1.5);
} else {
this.el[this.attr] = this.text.slice(0, this.i++);
return this.timer = setTimeout(this.type, (function(_this) {
return function() {
var t;
t = rand() * _this.int + _this.int * .1;
if (_this.text[_this.i] === ' ') {
t += rand() * _this.int;
}
if (_this.text[_this.i] === '.' || _this.text[_this.i] === ',') {
t += rand() * _this.int * 3;
}
if (rand() > .97) {
t += _this.int * 2;
}
if (rand() > .95) {
t += _this.int;
}
return t;
};
})(this)());
}
};
TuringType.prototype.pause = function() {
clearTimeout(this.timer);
return this.el[this.attr];
};
TuringType.prototype.clear = function(n) {
if (n == null) {
n = this.len;
}
if (n === -2) {
return;
}
this.el[this.attr] = this.text.slice(0, this.i--);
return setTimeout(this.clear.bind(this, --n), rand() * this.int);
};
return TuringType;
})();
if (typeof module !== "undefined" && module !== null ? module.exports : void 0) {
module.exports = TuringType;
} else if (typeof define !== "undefined" && define !== null ? define.amd : void 0) {
define(function() {
return TuringType;
});
} else {
window.TuringType = TuringType;
}
}).call(this);