diff options
Diffstat (limited to 'client/index.js')
| -rw-r--r-- | client/index.js | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/client/index.js b/client/index.js index d6dad6a..edc5281 100644 --- a/client/index.js +++ b/client/index.js @@ -25,7 +25,7 @@ let notes = [] requestAudioContext( () => { for (var i = 0; i < ws; i++) { - notes[i] = [] + notes[i] = [] for (var j = 0; j < hs; j++) { notes[i][j] = add(i, j) } @@ -34,6 +34,7 @@ requestAudioContext( () => { }) function play(freq) { + if (freq.playing) return freq.playing = true instrument.play(freq.frequency) if (instrument === organ || hash || life.isRunning()) { @@ -42,6 +43,7 @@ function play(freq) { life.assign_item(freq, true) } function pause(freq) { + if (!freq.playing) return freq.playing = false instrument.pause(freq.frequency) freq.div.classList.remove('playing') @@ -55,7 +57,7 @@ function assign(freq, state) { } } function toggle(freq) { - assign(freq, freq.playing = !freq.playing) + assign(freq, !freq.playing) } const gliderShape = [ [0,0,0,0,0], @@ -74,7 +76,6 @@ const gliderShapes = [ function glider() { const x = Math.floor(Math.random() * ws) const y = Math.floor(Math.random() * hs) - console.log("glider at", x, y) const shape = choice(gliderShapes) weave(x,y,shape) } @@ -95,44 +96,58 @@ function forEach(f){ for (i = 0; i < ws; i++) { for (j = 0; j < hs; j++) { note = notes[i][j] - s = f(i,j,note.playing) + s = f(i, j, note.playing) assign(note, s) } } } -function clear(){ +function clone(){ + let i, j; + let a = [] + for (i = 0; i < ws; i++) { + a[i] = [] + for (j = 0; j < hs; j++) { + a[i][j] = notes[i][j].playing + } + } + return a +} +function move(dx,dy){ + let a = clone() forEach((x,y,state) => { - return false + x = (x+dx+ws)%ws + y = (y+dy+hs)%hs + return a[x][y] }) } -function white(){ - forEach((x,y,state) => { +function clear(){ + forEach(() => { return false }) } function stripex(odd){ odd = !! odd - forEach((x,y,state) => { + forEach((x) => { return x % 2 ? odd : !odd }) } function stripey(odd){ odd = !! odd - forEach((x,y,state) => { + forEach((x,y) => { return y % 2 ? odd : !odd }) } function checker(odd, n){ odd = !! odd n = n || 1 - forEach((x,y,state) => { + forEach((x,y) => { return ((Math.floor(x/n)%2) ^ (Math.floor(y/n)%2)) ? odd : !odd }) } function noise(n){ n = n || 0.5 n = n * n - forEach((x,y,state) => { + forEach(() => { return Math.random() < n }) } @@ -229,21 +244,33 @@ function swap_instrument(){ let life_bpm = 50 window.addEventListener("keydown", keydown, true) function keydown(e){ - console.log(e.keyCode) + // console.log(e.keyCode) if (e.altKey || e.ctrlKey || e.metaKey) return switch (e.keyCode){ case 32: // space life.toggle() break - case 38: // up + case 188: // comma life_bpm += e.shiftKey ? 1 : 5 life.setTempo(life_bpm) break - case 40: // down + case 190: // period life_bpm -= e.shiftKey ? 1 : 5 life_bpm = Math.max(1, life_bpm) life.setTempo(life_bpm) break + case 37: // left + move(1, 0) + break + case 38: // up + move(0, 1) + break + case 39: // right + move(-1, 0) + break + case 40: // down + move(0, -1) + break case 71: // g glider() break @@ -254,7 +281,7 @@ function keydown(e){ clear() break case 87: // w - white() + clear() break case 78: // n noise(0.5) |
