Early development: IFLoom can change until 1.0. What's stable
IFLoom
Render it yourself

Render pictures and play sound

Render a scene's layered picture with its hotspots, and play the game's music, sounds and voices, in a UI of your own.

Render a scene's picture, its layers and hotspots, and play the game's music, sounds and voiced lines. Set up the runner first, as Start your own UI shows. A picture or a clip inside the prose is a beat, which Render the scene covers.

Render the scene's picture

view.scene.picture holds the scene's layered picture as a PictureView, and view.scene.image a single picture when the scene has no layers. The React UI's Scene renders the picture above the words with LayeredPicture, which you can also place yourself.

import type { PictureView, RunnerSnapshot } from '@ifloom/engine'
// runner and add, as in the vanilla template's src/main.ts.
import { add, runner } from './runner.ts'

/** The scene's layered picture, else its image. */
export function drawPicture(parent: HTMLElement, { view }: RunnerSnapshot): void {
  if (view.scene.picture) drawLayers(parent, view.scene.picture)
  else if (view.scene.image) Object.assign(add(parent, 'img'), { src: view.scene.image, alt: '' })
}

/** Layers stacked bottom first, with each hotspot a button placed by its rect, in percent of the picture. */
function drawLayers(parent: HTMLElement, picture: PictureView): void {
  const box = add(parent, 'div', '', 'picture')
  Object.assign(box.style, { position: 'relative', aspectRatio: picture.aspect ?? 'auto' })
  for (const layer of picture.layers) {
    const shown = add(box, layer.video ? 'video' : 'img')
    Object.assign(shown, { src: layer.src, alt: '' })
    Object.assign(shown.style, { position: 'absolute', inset: '0', width: '100%', height: '100%' })
  }
  for (const hotspot of picture.hotspots) {
    const [left, top, width, height] = hotspot.rect
    const button = add(box, 'button', '', 'hotspot')
    button.setAttribute('aria-label', hotspot.label)
    button.title = hotspot.disabled ?? hotspot.label
    button.disabled = Boolean(hotspot.disabled)
    Object.assign(button.style, { position: 'absolute', left: `${left}%`, top: `${top}%`, width: `${width}%`, height: `${height}%` })
    button.onclick = () => runner.apply(hotspot.command)
  }
}

Stack the layers bottom first. A layer with video set is a clip rather than a picture. The engine lists only the layers whose when holds and resolves a pool's pick, so render each layer in the list. Put each of the hotspots over the layers as a button, placed by its rect, the left, top, width and height in percent of the picture. A hotspot is a HotspotView, greyed out with disabled like any button, and it sends its command.

The places plugin gives a location's picture in its own view, and Render the calendar, locations and people renders it the same way.

Play sound

view.audio holds what each channel should play and the sounds that are due, as an AudioView, and the audio player plays it. The React UI's mountGame plays the game's sound itself, and Scene puts a replay button on each spoken line with a voice.

import { startAudio } from '@ifloom/audio'
import { audioSrc, type RunnerSnapshot } from '@ifloom/engine'
// runner and add, as in the vanilla template's src/main.ts.
import { add, runner } from './runner.ts'

// Once, beside the runner: plays what view.audio asks for, from the player's first click or key press.
// Keep the player it returns to play a file on demand.
const player = startAudio(runner)

/** A Play again button for each spoken line in the scene that has a voice. */
export function drawVoices(parent: HTMLElement, { view }: RunnerSnapshot): void {
  for (const beat of view.scene.beats) {
    if (beat.type !== 'say' || !beat.voice) continue
    // The voice names a track in the game's audio.tracks: a file, or a playlist, which has no one file to play.
    const track = runner.game.audio?.tracks[beat.voice]
    if (track && 'src' in track) add(parent, 'button', 'Play again').onclick = () => player.play(audioSrc(track.src), 'voice', track.volume ?? 1)
  }
}

Start startAudio once, beside the runner, never inside a component that comes and goes. It follows the runner rather than your components, so it plays the same whatever renders the page. Nothing plays until the player first clicks or presses a key, because browsers allow no sound sooner. Pass { log: console.debug } to see each thing the player does in the console.

The player sets each channel's volume from the ui setting the game's audio names for it, so a volume slider on your settings screen works with no code of yours. A spoken line with a voice is a say beat with voice, a track id. To play it again, look the track up in the game's audio.tracks and call the player's play with its file, through audioSrc.

For more on what these sections render, see:

Last updated on

On this page