|
|
@@ -0,0 +1,379 @@ |
| 1 |
<!doctype html> |
| 2 |
<html lang="en"> |
| 3 |
<head> |
| 4 |
<meta charset="utf-8"> |
| 5 |
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"> |
| 6 |
<title>Paper Route</title> |
| 7 |
<style> |
| 8 |
:root { --ink: #1c1b19; --muted: #8b877f; --paper: #fff; --accent: #c4503c; } |
| 9 |
* { box-sizing: border-box; } |
| 10 |
html, body { height: 100%; margin: 0; background: var(--paper); color: var(--ink); } |
| 11 |
body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } |
| 12 |
#stage { position: fixed; inset: 0; display: grid; place-items: center; } |
| 13 |
canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; } |
| 14 |
.hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; gap: 16px; pointer-events: none; } |
| 15 |
#scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; } |
| 16 |
#bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } |
| 17 |
.row { display: flex; align-items: center; gap: 8px; color: var(--accent); } |
| 18 |
.row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } |
| 19 |
.row span { color: var(--muted); } |
| 20 |
.dots { display: flex; gap: 3px; } |
| 21 |
.dots i { width: 6px; height: 6px; background: var(--accent); } |
| 22 |
.dots i.off { opacity: .16; } |
| 23 |
#status { color: var(--muted); } |
| 24 |
button { pointer-events: auto; font: inherit; color: var(--muted); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } |
| 25 |
button:hover, button:focus-visible { color: var(--ink); } |
| 26 |
</style> |
| 27 |
</head> |
| 28 |
<body> |
| 29 |
<div id="stage"><canvas id="c" tabindex="0" aria-label="A street of four houses with a cyclist riding past. Tap the picture or press space to throw a paper: porch scores 3, lawn 1, a window costs 2."></canvas></div> |
| 30 |
<div id="scores"> |
| 31 |
<div class="row"><b id="score">0</b><span>score</span></div> |
| 32 |
<div class="row"><b id="best">0</b><span>best</span><span class="dots" id="papers"></span></div> |
| 33 |
</div> |
| 34 |
<div class="hud" id="bottom"> |
| 35 |
<div id="status" role="status" aria-live="polite"></div> |
| 36 |
<button id="mute" type="button" aria-pressed="false">sound on</button> |
| 37 |
</div> |
| 38 |
<script src="../shell.js"></script> |
| 39 |
<script> |
| 40 |
PixelPlays.mount({ |
| 41 |
id: 'paper-route', |
| 42 |
rules: [ |
| 43 |
'Ten papers, one cyclist riding past at a steady pace. Tap or press space to throw.', |
| 44 |
'The moment you tap decides where the paper lands: the bike\'s speed carries it ahead.', |
| 45 |
'Porch 3 points, lawn 1, a window breaks and costs 2. A paper that flies past the end of the street scores nothing.', |
| 46 |
], |
| 47 |
}); |
| 48 |
</script> |
| 49 |
<script>window.PP_SCENE = { originY: 24, shadowX: 1.3, shadowY: -.12 };</script> |
| 50 |
<script src="../engine.js"></script> |
| 51 |
<script src="route.js"></script> |
| 52 |
<script> |
| 53 |
'use strict'; |
| 54 |
|
| 55 |
const ROUTE = window.PaperRoute; |
| 56 |
const KIND_ROAD = 6; |
| 57 |
const ROAD = [[92, 94, 108], [97, 99, 113], [103, 105, 118]], WALK = [[204, 200, 192], [212, 208, 200], [219, 215, 207]]; |
| 58 |
const LANE = rgb(226, 214, 168), JOINT = rgb(176, 172, 166); |
| 59 |
const HOUSE_WALLS = [[228, 198, 160], [166, 196, 210], [216, 164, 150], [196, 208, 158]]; |
| 60 |
const HOUSE_ROOFS = [[150, 84, 72], [92, 100, 130], [128, 92, 70], [84, 112, 98]]; |
| 61 |
const DOORS = [[122, 66, 54], [64, 88, 120], [88, 112, 78], [150, 100, 52]]; |
| 62 |
const SLAB = [204, 196, 184], PAPER_C = [250, 248, 238], TIRE = [38, 38, 50], FRAME = [200, 62, 50], METAL = [206, 206, 212]; |
| 63 |
const RIDER = { shirt: [214, 70, 56], stripe: [244, 236, 218], legs: [52, 62, 88], hair: [58, 40, 30], skin: [222, 170, 132] }; |
| 64 |
const GLASS = [150, 200, 222], WOOD = [236, 228, 210]; |
| 65 |
const BUSH_TONES = [LEAF_DEEP, LEAF_MID, LEAF_MID, LEAF_SILVER], BLOOM = [214, 92, 112]; |
| 66 |
const WALL_H = 3.4, BACK_Y = -5.2, WALL_Y = ROUTE.WALL_Y, BIKE_Y = ROUTE.ROAD_Y; |
| 67 |
const WINDOW_BOTTOM = ROUTE.WINDOW_Z[0], WINDOW_TOP = ROUTE.WINDOW_Z[1]; |
| 68 |
const COLORS = c => rampOf(c); |
| 69 |
|
| 70 |
function groundColor(x, y, ix, iy, i) { |
| 71 |
const edge = Math.pow(Math.pow(Math.abs(x) / 17, 4) + Math.pow(Math.abs(y + 1.4) / 6.2, 4), .25); |
| 72 |
const rough = edge + (noise(x * 1.3, y * 1.3) - .5) * .13; |
| 73 |
if (rough > 1.04) return 0; |
| 74 |
if (rough > .93 && hash(ix, iy) < (rough - .93) / .11) return 0; |
| 75 |
const fine = hash(ix, iy); |
| 76 |
if (y > .45 && y < 2.75) { |
| 77 |
kinds[i] = KIND_ROAD; |
| 78 |
if (Math.abs(y - BIKE_Y) < .09 && ((x % 2.4) + 2.4) % 2.4 < 1.2) return LANE; |
| 79 |
return rgb(...ROAD[clamp(Math.floor(noise(x * .7, y * .9) * 3 + (bayerAt(ix, iy) - .5) * .4), 0, 2)]); |
| 80 |
} |
| 81 |
if (y > -.35 && y <= .45) { |
| 82 |
kinds[i] = KIND_ROAD; |
| 83 |
if (((x % 1.6) + 1.6) % 1.6 < .06) return JOINT; |
| 84 |
return rgb(...WALK[clamp(Math.floor(noise(x * .6, y * 1.4) * 3 + (bayerAt(ix, iy) - .5) * .4), 0, 2)]); |
| 85 |
} |
| 86 |
kinds[i] = KIND_GRASS; |
| 87 |
const t = noise(x * .9, y * .9) * 1.15 + (fine - .5) * .55 - .08; |
| 88 |
return rgb(...GRASS[clamp(Math.floor(t * 4.2), 0, 3)]); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
function wallRect(x0, x1, z0, z1, lift, colorAt) { |
| 93 |
for (let a = x0; a <= x1; a += .05) for (let b = z0; b <= z1; b += .05) { |
| 94 |
const c = colorAt(a, b); |
| 95 |
if (c) plot(a, WALL_Y + lift, b, c); |
| 96 |
} |
| 97 |
} |
| 98 |
const LIFT = .14; |
| 99 |
function houseBox(k) { |
| 100 |
const sp = ROUTE.spans(k), wall = HOUSE_WALLS[k], roof = HOUSE_ROOFS[k]; |
| 101 |
box(sp.left, BACK_Y, 0, sp.right, WALL_Y, WALL_H, wall, k * 3); |
| 102 |
box(sp.left - .3, BACK_Y - .2, WALL_H, sp.right + .3, WALL_Y + .35, WALL_H + .4, roof, k * 5 + 1); |
| 103 |
box(sp.left + .6, BACK_Y + .4, WALL_H + .4, sp.right - .6, WALL_Y - .5, WALL_H + .9, roof, k * 5 + 2); |
| 104 |
const chimneyX = k % 2 ? sp.left + 1 : sp.right - 1.6; |
| 105 |
box(chimneyX, BACK_Y + .7, WALL_H + .4, chimneyX + .6, BACK_Y + 1.3, WALL_H + 1.6, [150, 118, 104], k); |
| 106 |
const [d0, d1] = sp.door, ramp = COLORS(DOORS[k]); |
| 107 |
box(d0 - .35, WALL_Y, 0, d1 + .35, WALL_Y + 1.35, ROUTE.PORCH_Z, SLAB, k + 20); |
| 108 |
wallRect(d0 + .2, d1 - .2, ROUTE.PORCH_Z, 2.35, LIFT, (a, b) => { |
| 109 |
const edge = a < d0 + .3 || a > d1 - .3 || b > 2.25, panel = (a - d0) % .8 < .06 || b > 1.1 && b < 1.15; |
| 110 |
return ramp[edge || panel ? 1 : 2]; |
| 111 |
}); |
| 112 |
wallRect(d1 - .55, d1 - .45, 1.1, 1.2, LIFT + .02, () => ramp[4]); |
| 113 |
} |
| 114 |
function glassAt(sp, a, b) { |
| 115 |
const [w0, w1] = sp.window, u = (a - w0) / (w1 - w0), v = (b - WINDOW_BOTTOM) / (WINDOW_TOP - WINDOW_BOTTOM); |
| 116 |
const ramp = COLORS(GLASS), frame = COLORS(WOOD); |
| 117 |
if (u < .09 || u > .91 || v < .08 || v > .92 || Math.abs(u - .5) < .035 || Math.abs(v - .5) < .045) return frame[3]; |
| 118 |
const glint = u + v > .7 && u + v < .95; |
| 119 |
return ramp[glint ? 4 : v > .5 ? 3 : 2]; |
| 120 |
} |
| 121 |
|
| 122 |
function brokenAt(sp, a, b) { |
| 123 |
const base = glassAt(sp, a, b), [w0, w1] = sp.window; |
| 124 |
const u = (a - w0) / (w1 - w0), v = (b - WINDOW_BOTTOM) / (WINDOW_TOP - WINDOW_BOTTOM); |
| 125 |
if (base === COLORS(WOOD)[3]) return base; |
| 126 |
const dx = u - .55, dy = (v - .5) * .9, ang = Math.atan2(dy, dx), r = Math.hypot(dx, dy); |
| 127 |
const ray = Math.abs(((ang + Math.PI) / (Math.PI / 3.5)) % 1 - .5) < .04 + .02 * r; |
| 128 |
if (ray) return rgb(236, 244, 250); |
| 129 |
return r < .22 || hash(Math.floor(a * 9), Math.floor(b * 9)) < .35 ? rgb(34, 30, 52) : COLORS(GLASS)[1]; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
function growBush(x0, x1, y0, y1, z1, count, seed) { |
| 134 |
for (let n = 0; n < count; n++) { |
| 135 |
const k = n + seed, x = lerp(x0, x1, hash(k, 3)), y = lerp(y0, y1, hash(k, 4)), z = .12 + hash(k, 5) * (z1 - .12); |
| 136 |
const bloom = hash(k, 6) < .09; |
| 137 |
const color = bloom ? BLOOM : BUSH_TONES[Math.min(3, Math.floor(hash(k, 7) * 4 + (z - .3) * .5))]; |
| 138 |
capture = []; |
| 139 |
sphere(x, y, z, bloom ? .13 : .16 + hash(k, 8) * .12, color, 'rough'); |
| 140 |
leaves.push({ x, y, z, k, r: .3, reach: 1.5, pixels: capture }); |
| 141 |
capture = null; |
| 142 |
} |
| 143 |
} |
| 144 |
function houseShadow(sp) { |
| 145 |
for (let x = sp.left; x <= sp.right; x += .45) for (let z = 0; z <= WALL_H + .9; z += .6) for (let y = BACK_Y; y <= WALL_Y; y += .6) { |
| 146 |
castShadow(x, y, z, .5, 1, 0); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
function bake() { |
| 151 |
zb.fill(-1e9); |
| 152 |
pix.fill(rgb(255, 255, 255)); |
| 153 |
for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { |
| 154 |
const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i); |
| 155 |
if (!c) continue; |
| 156 |
pix[i] = c; ground[i] = 1; |
| 157 |
zb[i] = (g.x + g.y) / R2 * COS; |
| 158 |
} |
| 159 |
growGrass(); |
| 160 |
groundZ.set(zb); |
| 161 |
|
| 162 |
for (let k = 0; k < ROUTE.HOUSES; k++) { |
| 163 |
houseBox(k); |
| 164 |
const sp = ROUTE.spans(k); |
| 165 |
houseShadow(sp); |
| 166 |
growBush(sp.window[0] - .1, sp.window[1] + .1, WALL_Y + .1, WALL_Y + .6, .95, 45, k * 211); |
| 167 |
if (k < ROUTE.HOUSES - 1) growTree(sp.right + (ROUTE.PITCH - ROUTE.WIDTH) / 2, WALL_Y - .5, .55, 140, 40 + k * 97); |
| 168 |
} |
| 169 |
growTree(15.2, -4, .7, 170, 5); |
| 170 |
bakeOcclusion(); |
| 171 |
staticShadow.set(shadowMask); |
| 172 |
applyShadows(groundZ); |
| 173 |
bakedPix.set(pix); |
| 174 |
bakedZ.set(zb); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
const nodes = { |
| 179 |
score: document.getElementById('score'), best: document.getElementById('best'), |
| 180 |
papers: document.getElementById('papers'), status: document.getElementById('status'), |
| 181 |
}; |
| 182 |
const G = { time: 0, bikeX: -ROUTE.TRACK, throwT: 9, shards: [], status: '' }; |
| 183 |
let R = ROUTE.newRound(0); |
| 184 |
function renderHud() { |
| 185 |
nodes.score.textContent = R.score; |
| 186 |
nodes.best.textContent = R.best; |
| 187 |
if (nodes.papers.children.length !== ROUTE.PAPERS) nodes.papers.innerHTML = '<i></i>'.repeat(ROUTE.PAPERS); |
| 188 |
[...nodes.papers.children].forEach((el, k) => el.classList.toggle('off', k >= R.left)); |
| 189 |
} |
| 190 |
function setStatus(text) { |
| 191 |
if (text === G.status) return; |
| 192 |
G.status = text; |
| 193 |
nodes.status.textContent = text; |
| 194 |
} |
| 195 |
function tap() { |
| 196 |
const next = ROUTE.again(R); |
| 197 |
if (next) { |
| 198 |
R = next; G.shards = []; |
| 199 |
setStatus('tap or press space to throw'); |
| 200 |
renderHud(); |
| 201 |
return; |
| 202 |
} |
| 203 |
if (!ROUTE.throwPaper(R, G.bikeX)) return; |
| 204 |
G.throwT = 0; |
| 205 |
sound('whoosh'); |
| 206 |
renderHud(); |
| 207 |
} |
| 208 |
function shatter(p) { |
| 209 |
for (let n = 0; n < 18; n++) { |
| 210 |
G.shards.push({ |
| 211 |
x: p.x, y: WALL_Y + .2, z: p.z, age: 0, |
| 212 |
vx: (hash(n, G.time * 7) - .5) * 3, vy: 1.5 + hash(n, 5) * 3, vz: (hash(n, 9) - .2) * 3.5, |
| 213 |
color: n % 3 ? rgb(236, 244, 250) : rgb(150, 200, 222), |
| 214 |
}); |
| 215 |
} |
| 216 |
} |
| 217 |
function settle({ zone, paper: p }) { |
| 218 |
if (zone === 'window') { |
| 219 |
shatter(p); sound('crash'); |
| 220 |
setStatus('crash, a window · −2'); |
| 221 |
} else if (zone === 'miss') { |
| 222 |
setStatus('missed the street · 0'); |
| 223 |
} else { |
| 224 |
sound(zone === 'porch' ? 'thud' : 'plink'); |
| 225 |
setStatus(zone === 'porch' ? 'on the porch · +3' : 'on the lawn · +1'); |
| 226 |
} |
| 227 |
} |
| 228 |
function update(dt) { |
| 229 |
G.time += dt; G.throwT += dt; |
| 230 |
G.bikeX += ROUTE.BIKE_SPEED * dt; |
| 231 |
if (G.bikeX > ROUTE.TRACK) G.bikeX = -ROUTE.TRACK; |
| 232 |
const was = R.phase, settled = ROUTE.advance(R, dt); |
| 233 |
settled.forEach(settle); |
| 234 |
if (settled.length) renderHud(); |
| 235 |
for (const s of G.shards) { |
| 236 |
s.age += dt; s.vz -= 12 * dt; |
| 237 |
s.x += s.vx * dt; s.y += s.vy * dt; s.z = Math.max(0, s.z + s.vz * dt); |
| 238 |
} |
| 239 |
G.shards = G.shards.filter(s => s.age < .9); |
| 240 |
if (was === 'play' && R.phase === 'over') setStatus(`${R.score} points · best ${R.best} · tap to play again`); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
function drawWindows() { |
| 245 |
for (let k = 0; k < ROUTE.HOUSES; k++) { |
| 246 |
const sp = ROUTE.spans(k), pane = R.broken.includes(k) ? brokenAt : glassAt; |
| 247 |
wallRect(sp.window[0], sp.window[1], WINDOW_BOTTOM, WINDOW_TOP, LIFT, (a, b) => pane(sp, a, b)); |
| 248 |
} |
| 249 |
} |
| 250 |
function tube(a, b, r, color) { dynamicAdd(a, b, r, r, () => color); } |
| 251 |
function drawBike(x, y) { |
| 252 |
const rot = x * 1.6, R = .6, wheels = [x - .78, x + .78]; |
| 253 |
for (const cx of wheels) { |
| 254 |
for (let n = 0; n < 14; n++) { |
| 255 |
const a0 = n / 14 * Math.PI * 2, a1 = (n + 1) / 14 * Math.PI * 2; |
| 256 |
tube([cx + Math.cos(a0) * R, y, R + Math.sin(a0) * R], [cx + Math.cos(a1) * R, y, R + Math.sin(a1) * R], .06, TIRE); |
| 257 |
} |
| 258 |
for (let s = 0; s < 2; s++) { |
| 259 |
const a = rot + s * Math.PI / 2; |
| 260 |
tube([cx - Math.cos(a) * R * .9, y, R - Math.sin(a) * R * .9], [cx + Math.cos(a) * R * .9, y, R + Math.sin(a) * R * .9], .025, METAL); |
| 261 |
} |
| 262 |
} |
| 263 |
const f = FRAME; |
| 264 |
const rear = [wheels[0], y, R], front = [wheels[1], y, R], bb = [x - .02, y, .5], seat = [x - .4, y, 1.3], head = [x + .52, y, 1.38]; |
| 265 |
tube(rear, bb, .05, f); tube(bb, seat, .05, f); tube(seat, head, .05, f); tube(bb, head, .05, f); |
| 266 |
tube(seat, rear, .045, f); tube(head, front, .045, METAL); |
| 267 |
tube([x + .46, y - .32, 1.52], [x + .46, y + .32, 1.52], .045, TIRE); |
| 268 |
tube([x - .55, y, 1.36], [x - .25, y, 1.36], .07, TIRE); |
| 269 |
} |
| 270 |
function poseFor() { |
| 271 |
const pedal = Math.floor(G.time * 6) % 2, swing = clamp(G.throwT / .3, 0, 1); |
| 272 |
const armL = swing < 1 ? lerp(-.9, 1.25, ease(swing)) : 1.1; |
| 273 |
return { z: .3, crouch: .5, lean: .5, stance: pedal ? .32 : .12, armR: 1.1, armL: G.throwT < .3 ? armL : 1.1 }; |
| 274 |
} |
| 275 |
function drawPaper(p) { |
| 276 |
sphere(p.x, p.y, p.z, .15, PAPER_C, 'solid'); |
| 277 |
castShadow(p.x, p.y, Math.max(0, p.z - .15), .16, 1, 0); |
| 278 |
} |
| 279 |
function drawShards() { |
| 280 |
for (const s of G.shards) { |
| 281 |
const q = project(s.x, s.y, s.z), ix = q.sx | 0, iy = q.sy | 0; |
| 282 |
if (ix < 0 || iy < 0 || ix >= W - 1 || iy >= H || (s.age > .5 && bayerAt(ix, iy) < (s.age - .5) / .4)) continue; |
| 283 |
pix[iy * W + ix] = s.color; |
| 284 |
if (s.z > .05) pix[iy * W + ix + 1] = s.color; |
| 285 |
} |
| 286 |
} |
| 287 |
function draw() { |
| 288 |
pix.set(bakedPix); |
| 289 |
zb.set(bakedZ); |
| 290 |
pendingShadows.length = 0; |
| 291 |
drawWindows(); |
| 292 |
for (const p of R.landed) { sphere(p.x, p.y, p.z, .14, PAPER_C, 'solid'); castShadow(p.x, p.y, 0, .15, 1, 0); } |
| 293 |
drawBike(G.bikeX, BIKE_Y); |
| 294 |
figure(G.bikeX - .3, BIKE_Y, 0, poseFor(), RIDER, dynamicAdd); |
| 295 |
for (const p of R.flying) drawPaper(p); |
| 296 |
const windT = STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS; |
| 297 |
drawLeaves(windT); |
| 298 |
drawTufts(windT); |
| 299 |
flushShadows(); |
| 300 |
applyShadows(bakedZ); |
| 301 |
shadeEdges(); |
| 302 |
drawShards(); |
| 303 |
ctx.putImageData(image, 0, 0); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
canvas.addEventListener('pointerdown', () => { |
| 308 |
ensureAudio(); |
| 309 |
canvas.focus({ preventScroll: true }); |
| 310 |
tap(); |
| 311 |
}); |
| 312 |
canvas.addEventListener('keydown', e => { |
| 313 |
if (e.key !== ' ' && e.key !== 'Enter') return; |
| 314 |
e.preventDefault(); |
| 315 |
if (e.repeat) return; |
| 316 |
ensureAudio(); |
| 317 |
tap(); |
| 318 |
}); |
| 319 |
|
| 320 |
|
| 321 |
let audio = null, muted = false; |
| 322 |
function ensureAudio() { |
| 323 |
if (audio) { if (audio.state === 'suspended') audio.resume(); return; } |
| 324 |
const AC = window.AudioContext || window.webkitAudioContext; |
| 325 |
if (AC) audio = new AC(); |
| 326 |
} |
| 327 |
function sound(kind) { |
| 328 |
if (!audio || muted) return; |
| 329 |
const now = audio.currentTime, gain = audio.createGain(); |
| 330 |
if (kind === 'crash') { |
| 331 |
const length = Math.floor(audio.sampleRate * .3), buffer = audio.createBuffer(1, length, audio.sampleRate), data = buffer.getChannelData(0); |
| 332 |
for (let n = 0; n < length; n++) data[n] = (hash(n, 1) * 2 - 1) * (1 - n / length); |
| 333 |
const src = audio.createBufferSource(); |
| 334 |
src.buffer = buffer; |
| 335 |
gain.gain.setValueAtTime(.12, now); |
| 336 |
gain.gain.exponentialRampToValueAtTime(.0005, now + .3); |
| 337 |
src.connect(gain).connect(audio.destination); |
| 338 |
src.start(now); |
| 339 |
return; |
| 340 |
} |
| 341 |
const osc = audio.createOscillator(); |
| 342 |
const spec = { |
| 343 |
whoosh: [420, 200, .1, .03, 'triangle'], |
| 344 |
thud: [170, 80, .09, .2, 'sine'], |
| 345 |
plink: [520, 380, .07, .1, 'sine'], |
| 346 |
}[kind]; |
| 347 |
osc.type = spec[4]; |
| 348 |
osc.frequency.setValueAtTime(spec[0], now); |
| 349 |
osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); |
| 350 |
gain.gain.setValueAtTime(spec[3], now); |
| 351 |
gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); |
| 352 |
osc.connect(gain).connect(audio.destination); |
| 353 |
osc.start(now); osc.stop(now + spec[2] + .01); |
| 354 |
} |
| 355 |
const muteButton = document.getElementById('mute'); |
| 356 |
muteButton.addEventListener('click', () => { |
| 357 |
muted = !muted; |
| 358 |
muteButton.textContent = muted ? 'sound off' : 'sound on'; |
| 359 |
muteButton.setAttribute('aria-pressed', String(muted)); |
| 360 |
}); |
| 361 |
|
| 362 |
|
| 363 |
addEventListener('resize', fit); |
| 364 |
fit(); |
| 365 |
bake(); |
| 366 |
setStatus('tap or press space to throw'); |
| 367 |
renderHud(); |
| 368 |
canvas.focus({ preventScroll: true }); |
| 369 |
let last = performance.now(); |
| 370 |
function frame(now) { |
| 371 |
update(Math.min(.04, (now - last) / 1000)); |
| 372 |
last = now; |
| 373 |
draw(); |
| 374 |
requestAnimationFrame(frame); |
| 375 |
} |
| 376 |
requestAnimationFrame(frame); |
| 377 |
</script> |
| 378 |
</body> |
| 379 |
</html> |