diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-02-21 04:10:28 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-02-21 04:10:28 +0100 |
| commit | 9f68262f9eb4720b4d6466e1cf5cf9c0edb9c286 (patch) | |
| tree | 30dab1f29749031680d19c70a6b4a88a553fa9ba /client/index.js | |
smash
Diffstat (limited to 'client/index.js')
| -rw-r--r-- | client/index.js | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/client/index.js b/client/index.js new file mode 100644 index 0000000..024c2d4 --- /dev/null +++ b/client/index.js @@ -0,0 +1,83 @@ +import Tone from 'tone' + +import Sampler from './lib/sampler' + +import draw from './draw' +import keys from './lib/keys' +import color from './lib/color' +import mouse from './lib/mouse' + +import { + browser, requestAudioContext, + randint, randrange, clamp, +} from './lib/util' + +const root = 440 +const s = 50 +const w = window.innerWidth +const h = window.innerHeight +const ws = w/s, hs = h/s + +let samplers = {} + +requestAudioContext( () => { + samplers['smash'] = new Sampler('samples/smash/g{}.mp3', 12) + samplers['glass'] = new Sampler('samples/glass/0{}Particle.mp3', 90) +}) + +let last_index = 0 +keys.listen(index => { + index = Math.abs(index+10) + const freq = 100 * Math.abs(index + 10) + const now = Tone.now() + const count = randrange(2, 6) + if (last_index !== index) { + samplers['smash'].play(randrange(90, 150) + index, 0) + last_index = index + } + else if (Math.random() < 0.09) { + last_index = -1 + } + for (var i = 0; i < count; i++) { + // kalimba.play(freq * (i+1)/4, now + Math.random()/(i+1)) + samplers['glass'].play( + 100 + index*(Math.random() * 10), + now + (Math.random()/2000 + i/10) + ) + } +}) + +mouse.register({ + down: (x, y) => { + samplers['smash'].play(randrange(90, 150) + 50 * (x/window.innerWidth + y/window.innerHeight), 0) + draw.clear() + draw.triangle(x, y, 400) + }, + move: (x, y, dx, dy) => { + let count = Math.abs(dx + dy) / 40 + if (count < 1) return + count = clamp(count, 1, 5) + if (Math.abs(dx) + Math.abs(dy) > 100) { + samplers['smash'].play(randrange(50, 300) + 100 * (x/window.innerWidth + y/window.innerHeight), 0) + draw.clear() + draw.triangle(x, y, 500) + } + let now = Tone.now() + let when, i + for (i = 0; i < count; i++) { + when = Math.random()/2000 + (i+ Math.random()/10)/randrange(2,5) + samplers['glass'].play( + 100 * randrange(2,5) / randrange(2,5), + now + when + ) + } + setTimeout( () => { + draw.triangle(x, y, Math.abs(dx) + Math.abs(dy)) + }, when * 1000) + }, + up: (x, y) => { + }, +}) + + + |
