diff options
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/sampler.js | 7 | ||||
| -rw-r--r-- | src/lib/stars.js | 56 | ||||
| -rw-r--r-- | src/lib/util.js | 1 |
3 files changed, 63 insertions, 1 deletions
diff --git a/src/lib/sampler.js b/src/lib/sampler.js index 0db53e1..0a00792 100644 --- a/src/lib/sampler.js +++ b/src/lib/sampler.js @@ -27,7 +27,7 @@ export default class Sampler { }); } - play(time, options) { + play(time, options, velocity) { const sound = options.index ? this.samples[options.index] : choice(this.samples); @@ -41,6 +41,11 @@ export default class Sampler { (options.frequency * choice([0.5, 1]) + randrange(0, 10)) / sound.root; } + if (velocity) { + player.volume.value = (1 - velocity) * -18; + console.log(player.volume.value); + } + player.start(time || 0); } } diff --git a/src/lib/stars.js b/src/lib/stars.js new file mode 100644 index 0000000..c4a0ea7 --- /dev/null +++ b/src/lib/stars.js @@ -0,0 +1,56 @@ +/** + * Stars + * @module src/lib/stars.js + */ + +export default function Stars() { + var canvas = document.createElement("canvas"), + ctx = canvas.getContext("2d"); + document.body.appendChild(canvas); + canvas.style.width = "100%"; + canvas.style.height = "100%"; + canvas.style.position = "absolute"; + canvas.style.top = "0px"; + canvas.style.left = "0px"; + canvas.style.zIndex = -1; + document.body.addEventListener("resize", go); + document.body.parentNode.style.backgroundColor = "black"; + ctx.strokeStyle = "white"; + var s = Math.sin, + c = Math.cos; + go(); + function ri(n) { + return Math.random() * n; + } + function rr(a, b) { + return (b - a) * Math.random() + a; + } + function go() { + var w = (canvas.width = window.innerWidth); + var h = (canvas.height = window.innerHeight); + ctx.clearRect(0, 0, w, h); + var n = Math.sqrt(w * h) | 0; + while (n--) { + var x = ri(w); + var y = ri(h); + var r0 = rr(0, 1); + var r1 = rr(0, 1); + var r2 = rr(0, 1); + var t0 = ri(2 * Math.PI); + var t1 = ri(2 * Math.PI); + var t2 = ri(2 * Math.PI); + var x0 = x + c(t0) * r0; + var y0 = y + s(t0) * r0; + var x1 = x + c(t1) * r1; + var y1 = y + s(t1) * r1; + var x2 = x + c(t2) * r2; + var y2 = y + s(t2) * r2; + ctx.beginPath(); + ctx.moveTo(x, y); + ctx.bezierCurveTo(x0, y0, x1, y1, x2, y2); + var color = rr(0, 255) | 0; + ctx.strokeStyle = "rgb(" + color + "," + color + "," + color + ")"; + ctx.stroke(); + } + } +} diff --git a/src/lib/util.js b/src/lib/util.js index 7980220..ff6dcf5 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -11,6 +11,7 @@ const isDesktop = !isMobile; document.body.classList.add(isMobile ? "mobile" : "desktop"); export const browser = { isIphone, isIpad, isMobile, isDesktop }; +export const clamp = (n, a, b) => (n < a ? a : n < b ? n : b); export const choice = (a) => a[Math.floor(Math.random() * a.length)]; export const mod = (n, m) => n - m * Math.floor(n / m); export const random = () => Math.random(); |
