-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathband.coffee
60 lines (50 loc) · 1.69 KB
/
band.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
Notes = new Meteor.Collection 'notes'
if Meteor.isClient
Meteor.setInterval ->
$('#background').css('background-color', "hsl(#{Math.random() * 255}, 70%, 30%)")
, 1000
Notes.find({}).observe
added: (note) ->
playInstrument = if note.instrument is 'piano' then playPiano else playDrums
playInstrument note.pitch
setTimeout ->
Notes.remove note._id
, 1000
keyPress = (event) ->
event.keyName = "|" if event.keyName is "\\" # for fix jQuery selector bug
instrument = if event.keyName in ['z','x','c','v','b','n','m'] then "drums" else "piano"
$("##{instrument}").find("[data-key='#{event.keyName}']").trigger('click')
event.preventDefault()
keyguru 'tab,q,w,e,r,t,y,u,i,o,p,[,],\\,z,x,c,v,b,n,m,1,2,3,5,6,8,9,0,+,=,backspace'.split(','), keyPress
Template.piano.events
"click li": (e) ->
$pianoKey = $(e.target)
$pianoKey.addClass 'clicked'
Meteor.setTimeout ->
$pianoKey.removeClass 'clicked'
, 300
Notes.insert
pitch: $pianoKey.data('pitch')
instrument: 'piano'
Template.drums.events
"click #drums > div": (e) ->
$drum = $(e.target).closest('#drums > div')
$drum.addClass 'clicked'
Meteor.setTimeout ->
$drum.removeClass 'clicked'
, 300
Notes.insert
pitch: $drum.data('pitch')
instrument: 'drums'
Template.notes.helpers
notes: Notes.find {}
Template.note.helpers
getStyle: ->
if @instrument is "drums"
offset = 17
else
offset = 0
"top: #{(@pitch + 15 + offset) * 2}vh; background-color: hsl(#{Math.random() * 255}, 100%, 70%)"
if Meteor.isServer
Meteor.startup ->
Notes.remove({})