diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-28 21:07:13 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-12 13:22:02 -0300 |
| commit | 590ba7b9b579b97e34b51ab7e3460ebc85713343 (patch) | |
| tree | 6a73a7570ff4f79f41deff078455877b9bc6571b /web | |
| parent | bc8e6eb1bfef0c5e9b3c40fc1e4fd5a1659e2b03 (diff) | |
| download | sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.tar.gz sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.zip | |
sketchbook: Zig 0.17, a Store on disk, requests, a scope (waveform, spectrum, spectrogram), audio codecs and fonts, web host
Diffstat (limited to 'web')
| -rw-r--r-- | web/host.js | 537 | ||||
| -rw-r--r-- | web/index.html | 1 | ||||
| -rw-r--r-- | web/sw.js | 24 |
3 files changed, 539 insertions, 23 deletions
diff --git a/web/host.js b/web/host.js index 9f92a75..aa45840 100644 --- a/web/host.js +++ b/web/host.js @@ -1,15 +1,19 @@ -// sketchbook web host: forwards input to the sketch and draws what it asks. -// It knows nothing about any particular sketch. +// sketchbook web host: forwards input to the sketch and does what it asks: +// drawing on a 2D canvas, GPU work on WebGPU (shaders arrive as WGSL), sound +// and the microphone on Web Audio, files through a picker. It knows nothing +// about any particular sketch. const canvas = document.querySelector('canvas'); const status = document.querySelector('#status'); const ctx = canvas.getContext('2d', {alpha: false}); const decoder = new TextDecoder(); +const encoder = new TextEncoder(); const images = [null]; const byPath = new Map(); const loads = []; -let wasm, clipped = false, size = [960, 600]; +let wasm, clipped = false, size = [960, 600], frameNumber = 0; -const bytes = (ptr, len) => new Uint8Array(wasm.memory.buffer, ptr, len); +// An empty Zig slice may point anywhere, even past the memory. +const bytes = (ptr, len) => len ? new Uint8Array(wasm.memory.buffer, ptr, len) : new Uint8Array(0); const string = (ptr, len) => decoder.decode(bytes(ptr, len)); const rgba = (r, g, b, a) => `rgba(${r},${g},${b},${a / 255})`; const Kind = {move: 1, down: 2, up: 3, wheel: 4, keyDown: 5, keyUp: 6, text: 7, cancel: 8}; @@ -27,6 +31,380 @@ function fit() { ctx.imageSmoothingEnabled = false; } +// Hands bytes to the sketch: into memory it allocates, which the export +// it is passed to takes over. Views are made after allocating, which may +// grow (and so replace) the memory. +function give(source) { + const view = source instanceof Float32Array ? new Uint8Array(source.buffer, source.byteOffset, source.byteLength) : source; + const ptr = wasm.sk_alloc(view.length); + bytes(ptr, view.length).set(view); + return ptr; +} + +// --------------------------------------------------------------------------- +// GPU: abi.gpu on WebGPU. Buffers and images by handle; shader modules and +// pipelines cached by their description; one queue submission per dispatch +// or pass, in order, as on native. + +const gpu = { + device: null, + buffers: [null], + modules: new Map(), + pipelines: new Map(), + // Uniforms and vertices go through rings written before each submission; + // the queue orders those writes with the work, so wrapping is safe. + rings: {}, + spare: [], + + // Why there is no device, for the page to say. + missing: '', + + async start() { + if (!window.isSecureContext) return void (gpu.missing = `WebGPU needs a secure context: open the page on localhost or over https, or tell the browser to trust ${location.origin}.`); + if (!navigator.gpu) return void (gpu.missing = 'This browser has WebGPU turned off. In Firefox: about:config, set dom.webgpu.enabled to true.'); + try { + const adapter = await navigator.gpu.requestAdapter(); + if (!adapter) return void (gpu.missing = 'WebGPU found no usable GPU: the browser may block this one (in Firefox: about:config, gfx.webgpu.ignore-blocklist).'); + const limits = adapter.limits; + gpu.device = await adapter.requestDevice({requiredLimits: { + maxComputeWorkgroupStorageSize: limits.maxComputeWorkgroupStorageSize, + maxStorageBufferBindingSize: limits.maxStorageBufferBindingSize, + maxBufferSize: limits.maxBufferSize, + }}); + gpu.device.addEventListener('uncapturederror', e => console.error('WebGPU:', e.error.message)); + gpu.device.lost.then(info => { console.error('WebGPU device lost:', info.message); gpu.device = null; }); + } catch (error) { + gpu.missing = `WebGPU failed to start: ${error.message ?? error}`; + } + }, + // Says why, once, the first time a sketch asks for the GPU and finds none. + absent() { + if (gpu.missing && !gpu.told) { + gpu.told = true; + console.warn(gpu.missing); + status.textContent = gpu.missing; + status.hidden = false; + } + return 0; + }, + + ring(name, usage, need) { + let r = gpu.rings[name]; + if (!r || r.size < need) { + const size = Math.max(need, 4 << 20); + r = gpu.rings[name] = {buffer: gpu.device.createBuffer({size, usage: usage | GPUBufferUsage.COPY_DST}), size, at: 0}; + } + return r; + }, + // Writes `data` into a ring, returning where. + put(name, usage, data, align) { + const r = gpu.ring(name, usage, data.length); + let at = Math.ceil(r.at / align) * align; + if (at + data.length > r.size) at = 0; + gpu.device.queue.writeBuffer(r.buffer, at, data); + r.at = at + data.length; + return {buffer: r.buffer, offset: at, size: data.length}; + }, + uniforms([ptr, len]) { + return len ? gpu.put('uniforms', GPUBufferUsage.UNIFORM, bytes(ptr, len).slice(), 256) : null; + }, + + module(shader) { + let m = gpu.modules.get(shader.hash); + if (!m) { + m = gpu.device.createShaderModule({code: string(...shader.code)}); + m.getCompilationInfo().then(info => { for (const msg of info.messages) if (msg.type === 'error') console.error(`WGSL ${msg.lineNum}:${msg.linePos} ${msg.message}`); }); + gpu.modules.set(shader.hash, m); + } + return m; + }, + + // Bind groups 0..3 in SDL_GPU's numbering; an empty one where a later + // one is used. + bind(encoder, pipeline, groups) { + for (let i = 0; i < groups.length; i++) { + const entries = groups[i] ?? []; + if (!entries.length && !groups.slice(i + 1).some(g => g?.length)) break; + encoder.setBindGroup(i, gpu.device.createBindGroup({layout: pipeline.getBindGroupLayout(i), entries})); + } + }, + storage(ids) { return ids.map((id, binding) => ({binding, resource: {buffer: gpu.buffers[id]}})); }, + uniform(u) { return u ? [{binding: 0, resource: u}] : []; }, + + newBuffer(size) { + const usage = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC; + gpu.buffers.push(gpu.device.createBuffer({size: Math.ceil(size / 4) * 4, usage})); + return gpu.buffers.length - 1; + }, + + compute(d) { + const k = d.kernel; + const key = `compute ${k.hash}`; + let pipeline = gpu.pipelines.get(key); + if (!pipeline) { + pipeline = gpu.device.createComputePipeline({layout: 'auto', compute: {module: gpu.module(k)}}); + gpu.pipelines.set(key, pipeline); + } + const uniforms = gpu.uniforms(d.uniforms); + const commands = gpu.device.createCommandEncoder(); + const pass = commands.beginComputePass(); + pass.setPipeline(pipeline); + gpu.bind(pass, pipeline, [gpu.storage(d.readonly), gpu.storage(d.readwrite), gpu.uniform(uniforms)]); + pass.dispatchWorkgroups(...d.groups); + pass.end(); + gpu.device.queue.submit([commands.finish()]); + }, + + // The texture a pass renders into, and the canvas that shows it. + target(id, w, h) { + const entry = images[id]; + if (!entry) return null; + if (!entry.texture || entry.texture.width !== w || entry.texture.height !== h) { + entry.texture?.destroy(); + entry.texture = gpu.device.createTexture({size: [w, h], format: 'rgba8unorm', usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC | GPUTextureUsage.TEXTURE_BINDING}); + entry.shown = document.createElement('canvas'); + entry.shown.width = w; entry.shown.height = h; + entry.view = entry.shown.getContext('webgpu'); + entry.view.configure({device: gpu.device, format: 'rgba8unorm', usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT, alphaMode: 'premultiplied'}); + entry.w = w; entry.h = h; + entry.attachments = {}; + } + return entry; + }, + // Multisample and stencil attachments, kept per image. + attachment(entry, kind, format, samples) { + const key = `${kind} ${samples}`; + if (!entry.attachments[key]) entry.attachments[key] = gpu.device.createTexture({size: [entry.w, entry.h], format, sampleCount: samples, usage: GPUTextureUsage.RENDER_ATTACHMENT}); + return entry.attachments[key]; + }, + // Copies the latest render into the image's canvas, once a frame, for + // the 2D canvas to draw. + present(entry) { + if (entry.presented === frameNumber) return; + entry.presented = frameNumber; + const commands = gpu.device.createCommandEncoder(); + commands.copyTextureToTexture({texture: entry.texture}, {texture: entry.view.getCurrentTexture()}, [entry.w, entry.h]); + gpu.device.queue.submit([commands.finish()]); + }, + + pipeline(p, samples, stencil) { + const key = JSON.stringify([p.vertex.hash, p.fragment.hash, p.stride, p.attributes, p.primitive, p.blend, p.write_color, p.stencil, samples, stencil]); + let pipeline = gpu.pipelines.get(key); + if (pipeline) return pipeline; + const formats = {float: 'float32', float2: 'float32x2', float3: 'float32x3', float4: 'float32x4', ubyte4_norm: 'unorm8x4'}; + const blends = { + none: undefined, + premultiplied: {color: {srcFactor: 'one', dstFactor: 'one-minus-src-alpha'}, alpha: {srcFactor: 'one', dstFactor: 'one-minus-src-alpha'}}, + additive: {color: {srcFactor: 'one', dstFactor: 'one'}, alpha: {srcFactor: 'one', dstFactor: 'one'}}, + }; + const dashed = s => s.replaceAll('_', '-').replace('-or-equal', '-equal').replace('-and-clamp', '-clamp').replace('-and-wrap', '-wrap'); + const face = f => ({compare: dashed(f.compare), passOp: dashed(f.pass), failOp: dashed(f.fail), depthFailOp: 'keep'}); + const keep = {compare: 'always', passOp: 'keep', failOp: 'keep', depthFailOp: 'keep'}; + pipeline = gpu.device.createRenderPipeline({ + layout: 'auto', + vertex: { + module: gpu.module(p.vertex), + buffers: p.attributes.length ? [{arrayStride: p.stride, attributes: p.attributes.map(a => ({shaderLocation: a.location, offset: a.offset, format: formats[a.format]}))}] : [], + }, + fragment: {module: gpu.module(p.fragment), targets: [{format: 'rgba8unorm', blend: blends[p.blend], writeMask: p.write_color ? GPUColorWrite.ALL : 0}]}, + primitive: {topology: dashed(p.primitive)}, + multisample: {count: samples}, + depthStencil: stencil ? { + format: 'depth24plus-stencil8', depthWriteEnabled: false, depthCompare: 'always', + stencilFront: p.stencil ? face(p.stencil.front) : keep, stencilBack: p.stencil ? face(p.stencil.back) : keep, + } : undefined, + }); + gpu.pipelines.set(key, pipeline); + return pipeline; + }, + + pass(p) { + const entry = gpu.target(p.image, p.width, p.height); + if (!entry) return; + const samples = p.samples > 1 ? 4 : 1; // WebGPU offers 1 and 4. + const view = entry.texture.createView(); + const color = { + view: samples > 1 ? gpu.attachment(entry, 'msaa', 'rgba8unorm', samples).createView() : view, + resolveTarget: samples > 1 ? view : undefined, + loadOp: p.clear ? 'clear' : 'load', + clearValue: p.clear ? {r: p.clear[0], g: p.clear[1], b: p.clear[2], a: p.clear[3]} : undefined, + storeOp: 'store', + }; + const depthStencil = p.stencil ? { + view: gpu.attachment(entry, 'stencil', 'depth24plus-stencil8', samples).createView(), + depthLoadOp: 'clear', depthClearValue: 1, depthStoreOp: 'discard', + stencilLoadOp: 'clear', stencilClearValue: 0, stencilStoreOp: 'discard', + } : undefined; + // Every draw's bytes are written before the pass is submitted. + const draws = p.draws.map(d => ({ + d, + vertices: d.vertices[1] ? gpu.put('vertices', GPUBufferUsage.VERTEX, bytes(...d.vertices).slice(), 4) : null, + vertexUniforms: gpu.uniforms(d.vertex_uniforms), + fragmentUniforms: gpu.uniforms(d.fragment_uniforms), + })); + const commands = gpu.device.createCommandEncoder(); + const pass = commands.beginRenderPass({colorAttachments: [color], depthStencilAttachment: depthStencil}); + for (const {d, vertices, vertexUniforms, fragmentUniforms} of draws) { + const pipeline = gpu.pipeline(p.pipelines[d.pipeline], samples, p.stencil); + pass.setPipeline(pipeline); + gpu.bind(pass, pipeline, [gpu.storage(d.vertex_storage), gpu.uniform(vertexUniforms), gpu.storage(d.fragment_storage), gpu.uniform(fragmentUniforms)]); + if (vertices) pass.setVertexBuffer(0, vertices.buffer, vertices.offset, vertices.size); + if (p.stencil) pass.setStencilReference(d.stencil_reference); + const s = d.scissor; + if (s) { + const x = Math.max(0, Math.floor(s.x)), y = Math.max(0, Math.floor(s.y)); + pass.setScissorRect(x, y, Math.max(0, Math.min(p.width, Math.ceil(s.x + s.w)) - x), Math.max(0, Math.min(p.height, Math.ceil(s.y + s.h)) - y)); + } else pass.setScissorRect(0, 0, p.width, p.height); + pass.draw(d.vertex_count); + } + pass.end(); + gpu.device.queue.submit([commands.finish()]); + }, + + // Reads a buffer back; it reaches the sketch before a later frame. + arrived: [], + download(id, offset, size, key) { + const staging = gpu.spare.find(b => b.size >= size) ?? gpu.device.createBuffer({size: Math.max(size, 256), usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST}); + gpu.spare = gpu.spare.filter(b => b !== staging); + const commands = gpu.device.createCommandEncoder(); + commands.copyBufferToBuffer(gpu.buffers[id], offset, staging, 0, size); + gpu.device.queue.submit([commands.finish()]); + staging.mapAsync(GPUMapMode.READ, 0, size).then(() => { + gpu.arrived.push({key, bytes: new Uint8Array(staging.getMappedRange(0, size)).slice()}); + staging.unmap(); + gpu.spare.push(staging); + }, () => {}); + }, +}; + +// --------------------------------------------------------------------------- +// Sound: streams queued and mixed on the main thread, the microphone +// collected the same way, both at the context's rate (48 kHz if the browser +// agrees). Browsers start sound only after a click or a key. + +const sound = { + context: null, + queues: new Map(), + heard: [], + microphone: null, + + start() { + // Safari: play as a media app does, through the ringer's silent switch. + if (navigator.audioSession) navigator.audioSession.type = 'playback'; + try { sound.context = new AudioContext({sampleRate: 48000, latencyHint: 'interactive'}); } catch { sound.context = new AudioContext(); } + const player = sound.context.createScriptProcessor(1024, 1, 1); + player.onaudioprocess = e => { + sound.pulled = performance.now(); + const out = e.outputBuffer.getChannelData(0); + out.fill(0); + for (const q of sound.queues.values()) { + let i = 0; + while (i < out.length && q.chunks.length) { + const chunk = q.chunks[0]; + const n = Math.min(out.length - i, chunk.length - q.at); + for (let j = 0; j < n; j++) out[i + j] += chunk[q.at + j]; + i += n; q.at += n; q.queued -= n; + if (q.at === chunk.length) { q.chunks.shift(); q.at = 0; } + } + } + }; + player.connect(sound.context.destination); + const resume = () => sound.context.resume().then(() => { + if (sound.hinted && !gpu.told) status.hidden = true; + }); + // On touch screens browsers count only a tap's end (or the click after + // it) as the gesture that may start sound. + for (const type of ['pointerdown', 'pointerup', 'touchend', 'click', 'keydown']) window.addEventListener(type, resume); + }, + // Browsers start sound only after a gesture: say so, once, while a + // sketch that plays or listens waits for one. + hinted: false, + hint() { + if (sound.context.state === 'running' || sound.hinted || gpu.told) return; + sound.hinted = true; + status.textContent = 'Click, tap or press a key to start sound and the microphone.'; + status.hidden = false; + }, + // While sound is not playing (no tap yet, the page hidden, or a browser + // whose mixer has stopped asking for samples), the queues drain by the + // clock as if heard, so sketches that pace themselves by what is queued + // keep running, silently. + pulled: 0, + drain(seconds) { + if (sound.context.state === 'running' && performance.now() - sound.pulled < 300) return; + for (const q of sound.queues.values()) { + let n = Math.round(seconds * sound.context.sampleRate); + while (n > 0 && q.chunks.length) { + const take = Math.min(n, q.chunks[0].length - q.at); + q.at += take; q.queued -= take; n -= take; + if (q.at === q.chunks[0].length) { q.chunks.shift(); q.at = 0; } + } + } + }, + queue(key) { + if (!sound.queues.has(key)) sound.queues.set(key, {chunks: [], at: 0, queued: 0}); + return sound.queues.get(key); + }, + listen() { + sound.hint(); + if (sound.microphone) return; + sound.microphone = 'asked'; + if (!navigator.mediaDevices) return console.warn('The microphone needs a secure context: localhost or https.'); + navigator.mediaDevices.getUserMedia({audio: {echoCancellation: false, noiseSuppression: false, autoGainControl: false}}).then(stream => { + const source = sound.context.createMediaStreamSource(stream); + const recorder = sound.context.createScriptProcessor(1024, 1, 1); + recorder.onaudioprocess = e => { + sound.heard.push(e.inputBuffer.getChannelData(0).slice()); + e.outputBuffer.getChannelData(0).fill(0); + }; + source.connect(recorder); + recorder.connect(sound.context.destination); + sound.microphone = stream; + }, error => console.warn('No microphone:', error)); + }, +}; + +// A file the user picked: decoded by the browser, handed to the sketch by +// name, then announced as the console line `<command> <name>`. +async function openFile(command, pattern) { + const input = document.createElement('input'); + input.type = 'file'; + if (pattern) input.accept = pattern.split(';').map(e => `.${e}`).join(','); + input.onchange = async () => { + const file = input.files[0]; + if (!file) return; + try { + const decoded = await sound.context.decodeAudioData(await file.arrayBuffer()); + const mono = new Float32Array(decoded.length); + for (let c = 0; c < decoded.numberOfChannels; c++) { + const channel = decoded.getChannelData(c); + for (let i = 0; i < mono.length; i++) mono[i] += channel[i] / decoded.numberOfChannels; + } + const name = encoder.encode(file.name); + wasm.sk_sound(give(name), name.length, give(mono), mono.length); + const line = encoder.encode(`${command} ${file.name}`); + wasm.sk_command(give(line), line.length); + } catch (error) { + console.error(`Cannot open ${file.name}:`, error); + } + }; + input.click(); +} + +// Microphone samples and readbacks, into the sketch before each frame. +function deliver() { + if (sound.heard.length) { + const total = sound.heard.reduce((n, c) => n + c.length, 0); + const all = new Float32Array(total); + let at = 0; + for (const chunk of sound.heard) { all.set(chunk, at); at += chunk.length; } + sound.heard = []; + wasm.sk_microphone(give(all), total); + } + for (const {key, bytes: b} of gpu.arrived) wasm.sk_download(key, give(b), b.length); + gpu.arrived = []; +} + const imports = { host: { log(ptr, len) { console.log(string(ptr, len)); }, @@ -52,12 +430,29 @@ const imports = { images.push({source, w, h, ctx: source.getContext('2d')}); return images.length - 1; }, + freeImage(id) { + if (!images[id]) return; + images[id].texture?.destroy(); + for (const t of Object.values(images[id].attachments ?? {})) t.destroy(); + images[id] = null; + }, + openFile(command, commandLen, pattern, patternLen) { openFile(string(command, commandLen), string(pattern, patternLen)); }, }, canvas: { clear(r, g, b, a) { ctx.fillStyle = rgba(r, g, b, a); ctx.fillRect(0, 0, size[0], size[1]); }, fill(x, y, w, h, r, g, b, a) { ctx.fillStyle = rgba(r, g, b, a); ctx.fillRect(x, y, w, h); }, image(id, x, y, w, h, sx, sy, sw, sh, alpha) { const entry = images[id]; + if (entry?.texture) { + // GPU renders scale smoothly, as on native. + gpu.present(entry); + ctx.globalAlpha = alpha / 255; + ctx.imageSmoothingEnabled = true; + if (sw > 0) ctx.drawImage(entry.shown, sx, sy, sw, sh, x, y, w, h); else ctx.drawImage(entry.shown, x, y, w, h); + ctx.imageSmoothingEnabled = false; + ctx.globalAlpha = 1; + return; + } if (!entry?.source || (entry.source instanceof Image && !entry.w)) { ctx.fillStyle = '#6e1e46'; ctx.fillRect(x, y, w, h); return; } ctx.globalAlpha = alpha / 255; if (sw > 0) ctx.drawImage(entry.source, sx, sy, sw, sh, x, y, w, h); else ctx.drawImage(entry.source, x, y, w, h); @@ -65,9 +460,10 @@ const imports = { }, text(x, y, scale, ptr, len, r, g, b, a) { ctx.fillStyle = rgba(r, g, b, a); - ctx.font = `${12 * scale}px monospace`; + // The native font is a fixed 8px cell: the same advance, and a size + // whose capitals fill the cell as native ones do. + ctx.font = `${10 * scale}px monospace`; ctx.textBaseline = 'top'; - // The native font is a fixed 8px cell; keep the same geometry. for (const [i, ch] of Array.from(string(ptr, len)).entries()) ctx.fillText(ch, x + i * 8 * scale, y); }, clip(enabled, x, y, w, h) { @@ -87,6 +483,26 @@ const imports = { entry.ctx.putImageData(new ImageData(data, w, h), 0, 0); }, }, + gpu: { + available: () => gpu.device ? 1 : gpu.absent(), + newBuffer: size => gpu.device ? gpu.newBuffer(size) : 0, + freeBuffer(id) { gpu.buffers[id]?.destroy(); gpu.buffers[id] = null; }, + write(id, offset, ptr, len) { if (gpu.buffers[id]) gpu.device.queue.writeBuffer(gpu.buffers[id], offset, bytes(ptr, len).slice()); }, + compute(ptr, len) { if (gpu.device) gpu.compute(JSON.parse(string(ptr, len))); }, + pass(ptr, len) { if (gpu.device) gpu.pass(JSON.parse(string(ptr, len))); }, + download(id, offset, size, key) { if (gpu.buffers[id]) gpu.download(id, offset, size, key); }, + }, + audio: { + rate: () => sound.context.sampleRate, + push(key, ptr, len, clear) { + if (len) sound.hint(); + const q = sound.queue(key); + if (clear) { q.chunks = []; q.at = 0; q.queued = 0; } + if (len) { q.chunks.push(new Float32Array(wasm.memory.buffer.slice(ptr, ptr + len * 4))); q.queued += len; } + }, + queued: key => sound.queues.get(key)?.queued ?? 0, + listen: () => sound.listen(), + }, }; // DOM `code` to USB HID usage, the numbering recordings and sketches use. @@ -101,39 +517,99 @@ const hid = code => { const floatBits = f => new Uint32Array(new Float32Array([f]).buffer)[0]; try { + await gpu.start(); + sound.start(); const response = await fetch('sketch.wasm'); if (!response.ok) throw new Error('The sketch could not be loaded.'); wasm = (await WebAssembly.instantiate(await response.arrayBuffer(), imports)).instance.exports; + // The room for the canvas, which responsive sketches fill. + const room = () => wasm.sk_window(window.visualViewport?.width ?? window.innerWidth, window.visualViewport?.height ?? window.innerHeight); + room(); + window.addEventListener('resize', room); + window.visualViewport?.addEventListener('resize', room); wasm.sk_init(); wasm.sk_frame(0); await Promise.all(loads); - status.hidden = true; + if (!gpu.told && !(sound.hinted && sound.context.state !== 'running')) status.hidden = true; - const send = (kind, x = 0, y = 0, code = 0, value = 0) => wasm.sk_event(kind, code, x, y, value); + // `flags`: for pointer events, the modifiers held (abi.Event.withModifiers). + const send = (kind, x = 0, y = 0, code = 0, value = 0, flags = 0) => wasm.sk_event(kind, code, x, y, value, flags); + const held = e => ((e.shiftKey ? 1 : 0) | (e.ctrlKey ? 2 : 0) | (e.altKey ? 4 : 0) | (e.metaKey ? 8 : 0)) << 4; const point = e => { const r = canvas.getBoundingClientRect(); return [(e.clientX - r.left) * size[0] / r.width, (e.clientY - r.top) * size[1] / r.height]; }; + // Mouse and pen: the pointer, one at a time. let pointer = null; + // Touch: one finger is the mouse's left button. Two fingers pan, sent as + // the wheel (60 px a notch), and pinch, sent as the wheel with ctrl in its + // flags (one notch per 20 % of spread), the way browsers report a + // trackpad's pinch; both around the point between the fingers. Once two fingers are down, the + // rest of the touch is a gesture until every finger lifts. + const touches = new Map(); + let gesture = null; + const measure = () => { + const [a, b] = [...touches.values()]; + return {mid: [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2], spread: Math.hypot(a[0] - b[0], a[1] - b[1])}; + }; + const ctrl = 2 << 4; + // Synthetic pointers (tests) cannot be captured; real ones always can. + const capture = id => { try { canvas.setPointerCapture(id); } catch {} }; canvas.addEventListener('pointerdown', e => { - if (pointer !== null) return; e.preventDefault(); - pointer = e.pointerId; canvas.setPointerCapture(pointer); - send(Kind.down, ...point(e), e.button + 1); + if (e.pointerType === 'touch') { + capture(e.pointerId); + touches.set(e.pointerId, point(e)); + if (touches.size === 1 && !gesture) { + send(Kind.move, ...point(e)); + send(Kind.down, ...point(e), 1); + } else if (touches.size === 2) { + if (!gesture) send(Kind.cancel); + gesture = measure(); + } + return; + } + if (pointer !== null) return; + pointer = e.pointerId; capture(pointer); + send(Kind.down, ...point(e), e.button + 1, 0, held(e)); + }); + canvas.addEventListener('pointermove', e => { + if (e.pointerType !== 'touch') return send(Kind.move, ...point(e), 0, 0, held(e)); + if (!touches.has(e.pointerId)) return; + touches.set(e.pointerId, point(e)); + if (!gesture) return send(Kind.move, ...point(e)); + if (touches.size < 2) return; + const now = measure(); + const pan = now.mid[1] - gesture.mid[1]; + if (pan) send(Kind.wheel, ...now.mid, 0, floatBits(pan / 60)); + if (gesture.spread > 0 && now.spread > 0 && now.spread !== gesture.spread) { + send(Kind.wheel, ...now.mid, 0, floatBits(Math.log(now.spread / gesture.spread) / Math.log(1.2)), ctrl); + } + gesture = now; }); - canvas.addEventListener('pointermove', e => send(Kind.move, ...point(e))); - canvas.addEventListener('pointerup', e => { + const lift = e => { + if (e.pointerType === 'touch') { + if (!touches.delete(e.pointerId)) return; + if (!gesture) send(e.type === 'pointercancel' ? Kind.cancel : Kind.up, ...point(e), 1); + else if (touches.size === 0) gesture = null; + return; + } if (e.pointerId !== pointer) return; - send(Kind.up, ...point(e), e.button + 1); + if (e.type === 'pointercancel') send(Kind.cancel); else send(Kind.up, ...point(e), e.button + 1, 0, held(e)); const id = pointer; pointer = null; if (canvas.hasPointerCapture(id)) canvas.releasePointerCapture(id); - }); - const cancel = () => { pointer = null; send(Kind.cancel); }; - canvas.addEventListener('pointercancel', cancel); - canvas.addEventListener('lostpointercapture', () => { if (pointer !== null) cancel(); }); - canvas.addEventListener('pointerleave', () => { if (pointer === null) send(Kind.move, -1, -1); }); - canvas.addEventListener('wheel', e => { e.preventDefault(); send(Kind.wheel, ...point(e), 0, floatBits(-Math.sign(e.deltaY))); }, {passive: false}); + }; + canvas.addEventListener('pointerup', lift); + canvas.addEventListener('pointercancel', lift); + canvas.addEventListener('contextmenu', e => e.preventDefault()); + const cancel = () => { pointer = null; touches.clear(); gesture = null; send(Kind.cancel); }; + canvas.addEventListener('lostpointercapture', e => { if (e.pointerId === pointer) cancel(); }); + canvas.addEventListener('pointerleave', e => { if (pointer === null && e.pointerType !== 'touch') send(Kind.move, -1, -1); }); + // A trackpad's pinch arrives as the wheel with ctrlKey set. + canvas.addEventListener('wheel', e => { e.preventDefault(); send(Kind.wheel, ...point(e), 0, floatBits(-Math.sign(e.deltaY)), held(e)); }, {passive: false}); window.addEventListener('keydown', e => { + // Keys the sketch may use, not the page. + if (['Space', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'PageUp', 'PageDown', 'Home', 'End', 'Backspace'].includes(e.code)) e.preventDefault(); send(Kind.keyDown, 0, 0, hid(e.code), 0); if (e.key.length === 1 && !e.ctrlKey && !e.metaKey) send(Kind.text, 0, 0, 0, e.key.codePointAt(0)); }); @@ -145,23 +621,38 @@ try { // Tests read state by field path, like the native probe. window.sketch = { field(path) { - const encoded = new TextEncoder().encode(path); + const encoded = encoder.encode(path); new Uint8Array(wasm.memory.buffer, wasm.sk_buffer(), encoded.length).set(encoded); return wasm.sk_field(encoded.length); }, + // Runs a console line; with `sound`, opens sounds without the picker. + command(line) { const encoded = encoder.encode(line); wasm.sk_command(give(encoded), encoded.length); }, + sound(name, samples) { const encoded = encoder.encode(name); wasm.sk_sound(give(encoded), encoded.length, give(samples), samples.length); }, size: () => size, + // 'running', or 'suspended' until a tap or key starts sound. + audio: () => sound.context.state, }; + let previous = performance.now(); const frame = ms => { try { + frameNumber++; + sound.drain(Math.min((ms - previous) / 1000, 0.25)); + previous = ms; + deliver(); wasm.sk_frame(ms); requestAnimationFrame(frame); } catch (error) { - status.textContent = 'The sketch stopped. Reload to start again.'; status.hidden = false; console.error(error); + status.textContent = `The sketch stopped (${error?.message ?? error}). Reload to start again.`; status.hidden = false; console.error(error); } }; requestAnimationFrame(frame); + + // Built as an installable app (a manifest in the page): keep an offline copy. + if (document.querySelector('link[rel="manifest"]') && navigator.serviceWorker) { + navigator.serviceWorker.register('sw.js').catch(error => console.warn('No offline copy:', error)); + } } catch (error) { - status.textContent = 'The sketch could not start. Reload to try again.'; + status.textContent = `The sketch could not start (${error?.message ?? error}). Reload to try again.`; console.error(error); } diff --git a/web/index.html b/web/index.html index 4b55799..bb2e463 100644 --- a/web/index.html +++ b/web/index.html @@ -4,6 +4,7 @@ <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"> <title>{title}</title> <link rel="icon" href="data:,"> +{head} <style> * { box-sizing: border-box; } html, body { margin: 0; width: 100%; height: 100%; background: #0b1112; color: #e8e8d6; } diff --git a/web/sw.js b/web/sw.js new file mode 100644 index 0000000..8298b85 --- /dev/null +++ b/web/sw.js @@ -0,0 +1,24 @@ +// sketchbook's service worker, for sketches built as installable apps: it +// keeps a copy of everything the page fetches, so the app opens offline. +// Network first, so a new build shows as soon as there is a network; the +// copy only when there is none. +const cache = `sketchbook ${self.registration.scope}`; +const core = ['./', 'index.html', 'host.js', 'sketch.wasm', 'manifest.webmanifest']; + +self.addEventListener('install', event => { + event.waitUntil(caches.open(cache).then(c => Promise.all(core.map(url => c.add(url).catch(() => {}))))); + self.skipWaiting(); +}); +self.addEventListener('activate', event => event.waitUntil(self.clients.claim())); + +self.addEventListener('fetch', event => { + const request = event.request; + if (request.method !== 'GET' || !request.url.startsWith(self.registration.scope)) return; + event.respondWith(fetch(request).then(response => { + if (response.ok) { + const copy = response.clone(); + event.waitUntil(caches.open(cache).then(c => c.put(request, copy))); + } + return response; + }, () => caches.open(cache).then(c => c.match(request, {ignoreSearch: true})).then(found => found ?? Response.error()))); +}); |
