-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.typewriter.coffee
67 lines (43 loc) · 1.06 KB
/
jquery.typewriter.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
$(document).ready ->
log = (m) ->
console.log m
$.fn.typewriter = (options) ->
the_text = @.data('text')
location = this
n = 0
settings = $.extend(
typing_speed: 100
delay: 0
random: false
randomMax: 400
, options)
loadRandom = (params) ->
speedRand = []
i = 0
while i < params.text_len
rand = Math.floor( Math.random() * params.randMax )
if rand < 100
continue
speedRand.push(rand)
i++
return speedRand
return @each ->
speed = settings.typing_speed
settings.randomMax = 101 if settings.randomMax < 101
params =
speed : settings.typing_speed
randMax : settings.randomMax
text_len : the_text.length
speedRandArray = loadRandom(params) if settings.random
typeChar = (the_text, n) ->
if n < the_text.length
speed = speedRandArray[n] if settings.random
$(location).html the_text.substring(0, n + 1)
n++
setTimeout (->
typeChar the_text, n
), speed
log speed
setTimeout (->
typeChar the_text, 0
), settings.delay