-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypewriter.coffee
56 lines (49 loc) · 1.4 KB
/
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
module.exports = class TypeWriter
constructor: (config, el)->
@fullSentence = ""
@el = el
@config = config
@currentIndex = 0
@totalLength = config.length
start: ()->
@_type @_setup(@config)
done: (doneFn, wait)->
@_doneFn = ()->
setTimeout (=>
doneFn()
), wait
_setup: (config)->
wordConfigs = config.map (obj, wordIndex)=>
letters = []
words = obj.words.split("")
words.forEach (word, i)->
letters.push
letter: word
return letters
return wordConfigs
_type: (words)=>
return if @_done(@totalLength is @currentIndex)
@_startSentence words[@currentIndex], =>
@fullSentence += ' '
@fullSentence += '<br>' if @config[@currentIndex].break
@fullSentence = "" if @config[@currentIndex].clearAll
setTimeout (=>
@currentIndex++
@_type(words)
), @config[@currentIndex].wait or 220
_startSentence: (sentence, callback)->
sentenceLength = sentence.length - 1
sentence.forEach (letter, idx)=>
setTimeout (=>
@fullSentence += letter.letter
@el.innerHTML = @fullSentence
callback() if sentenceLength is idx
), @_rando(idx)
_done: (done)->
if done
if typeof @_doneFn is 'function'
@_doneFn()
return done
_rando: (i)->
timeOut = (120 * i) + (Math.floor(Math.random() * 100))
return timeOut