forked from dmotz/TuringType
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turingtype.coffee
48 lines (40 loc) · 1.27 KB
/
turingtype.coffee
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
rand = Math.random
{floor} = Math
class window.TuringType
int: 100
accuracy: .95
keys: 'qwertyuiop[]asdfghjklzxcvbnm,./;-='.split ''
constructor: (@el, @text, @options = {}) ->
return new TuringType arguments... unless @ instanceof TuringType
@el = document.querySelector @el if typeof @el is 'string'
@len = @text.length
@i = 0
{accuracy, interval, @callback} = @options
@accuracy = accuracy ? @accuracy
@int = interval ? @int
if tag = @el.tagName.toLowerCase() is 'textarea' or tag is 'input'
@attr = 'value'
@el.focus()
else
@attr = 'innerText'
@type()
type: =>
return @callback?() if @i is @len
if rand() > @accuracy
@el[@attr] += @keys[floor rand() * @keys.length]
@timer = setTimeout =>
@el[@attr] = @text.slice 0, @i
@timer = setTimeout @type, rand() * @int + @int * .8
, @int * 1.5
else
@el[@attr] += @text[@i++]
@timer = setTimeout @type, do =>
t = rand() * @int + @int * .1
t += rand() * @int if @text[@i] is ' '
t += rand() * @int * 3 if @text[@i] is '.' or @text[@i] is ','
t += @int * 2 if rand() > .97
t += @int if rand() > .95
t
pause: ->
clearTimeout @timer
@el[@attr]