# In the browser


Every player in these docs is the library itself. The scene under it is an Odin procedure, compiled together with `fx` to WebAssembly, and drawn in your browser by Skia. Nothing was recorded. This page explains how that works and how to use it.

```odin
landing_hello :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	rise := 30 * (1 - fx.ep(s.t, s.start, s.start + 0.6))
	split := knob("rgb_split", 6, 0, 24)
	if fx.group({o = fx.fade(s), y = rise, post = fx.rgb_split(split)}) {
		fx.eyebrow("Scene 01 / intro", {160, 360}, fx.theme.na)
		fx.caption("No browser\nin the loop.", {160, 420}, 170, fx.theme.nt)
	}
	fx.vignette(0.35)
	fx.grain(0.035)
}
```

## How it fits together

Natively, `fx` draws by calling a small C layer over Skia (`shim/sk.h`, about seventy functions: draw a rect, save a layer, lay out a paragraph). In the browser those same functions exist, written in JavaScript on top of [CanvasKit](https://skia.org/docs/user/modules/canvaskit/), which is Skia compiled to WebAssembly by the Skia project.

| Native | Browser |
| --- | --- |
| Your scenes and `fx`, compiled by Odin to a shared library | The same source, compiled with `-target:js_wasm32` to a `.wasm` file |
| `shim/sk.cpp`, C++ calling Skia | `skshim.js`, JavaScript calling CanvasKit |
| Skia's CPU rasterizer, one surface per render thread | Skia's WebGL backend, one surface for the page |
| Frames go to ffmpeg | Frames go to a `<canvas>` |

`fx` does not know which one it is talking to. The only parts of the library that differ are in two small files: `fx/native.odin` and `fx/web.odin`.

## What works and what does not

Everything that draws, animates and synthesizes works: shapes, text, groups, grading, post filters, shaders, 3D, layout, the interface kit, and the synth (the sounds on the [music](/docs/music/) page are rendered in your browser when you press play).

What needs files or ffmpeg is not there:

| Missing | Why |
| --- | --- |
| [`footage`](/docs/footage/) | Decoding goes through an ffmpeg process. In the browser it draws nothing. |
| Audio files, `onsets`, `drops`, `spectrum` | Decoded by ffmpeg, and cached on disk. |
| The mixer (`Video.sounds`) | Not compiled into the browser build. |
| Voice-over timelines | Read from a file. |
| Images and fonts outside `assets/` | There is no file system; the page fetches a fixed list up front. |

Shaders behave the other way round from a render: in the browser they run on the GPU and are the cheap part.

## Differences worth knowing

The picture is the same code path through Skia, and matches a native render closely, but three things are done differently because CanvasKit's JavaScript API lacks them:

- **Post filters.** Skia's runtime-shader image filter is not exposed, so a group with a `post` is drawn into an offscreen surface and the filter's shader is run over the parent with that surface as its input.
- **Trimmed paints.** There is no trim path effect, so a shape drawn with `trimmed` is turned into a path and the path is trimmed.
- **Text.** A laid-out paragraph keeps the paint it was built with, so a text drawn in many colors has one paragraph per color, cached for a few frames.

## Adding a demo

The players, the JavaScript side of Skia and these pages live in their own repository, [effects-website](https://github.com/fipso/effects-website). A demo is a scene with a marker comment above it, in any file in its `demos/` directory:

```odin
//demo post-rgb-split 3
post_rgb_split :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	if fx.group({post = fx.rgb_split(knob("px", 8, 0, 24))}) {
		fx.caption("split", {960, 540}, 220, fx.theme.nt, {0.5, 0.5})
	}
}
```

`//demo <name> <seconds>` registers it; `knob(name, default, lo, hi)` is a value the reader can drag. In a docs page, `@demo post-rgb-split` on a line of its own places the player, with the procedure's source under it. `//sound <name> <seconds>` above a `proc(out: []fx.Sample)` does the same for a sound, placed with `@sound`.

```bash
$ git clone https://github.com/fipso/effects-website   # next to your effects checkout
$ cd effects-website
$ npm install
$ npm run dev                           # build, serve on localhost:4173, rebuild on change
$ node tools/check.mjs demos/post.odin  # compile one file, write a sheet of frames per demo
```

`check.mjs` draws with CanvasKit in Node, on the CPU, so a demo can be checked without a browser. `content/WRITING.md` has the conventions the pages follow.

## Using it elsewhere

Nothing here is specific to documentation. The pieces are:

1. A package with `host.odin` (it exports `web_init`, `web_frame`, `web_knobs`, `web_set_knob`) and your scenes, compiled with `odin build <dir> -target:js_wasm32 -collection:effects=<repo>`.
2. `src/runtime/skshim.js`, which provides the module's imports: `createSk(CanvasKit, {files})`.
3. A surface: `CanvasKit.MakeWebGLCanvasSurface(canvas)`, registered with `sk.target(surface)`.

`src/js/player.js` in that repository is about 300 lines and does exactly this; `tools/shot.mjs` does it in Node. A web page that plays a whole project, or an editor that runs in a tab, would be built from the same three pieces.
