Scripted visuals (preview)
Draw a visual in JavaScript, connect it to a track, and edit it during playback.
Preview. A visual() call runs your JavaScript once per requested frame. The examples use the built-in synth and work with the 1.9.0 macOS build.
Make a beat-driven visual
Create a folder and save this score there as pulse.sanic.
let drums = [D2 _ D2@0.3 _] |> sine |> adsr(0.002, 0.16, 0, 0.04)
play drums |> visual("./pulse.js")Save the following JavaScript as pulse.js beside the score.
function draw({ ctx, width, height, audio, transport }) {
ctx.fillStyle = "#21192f";
ctx.fillRect(0, 0, width, height);
const lift = Math.cos(2 * Math.PI * transport.beatPhase);
ctx.fillStyle = "#deb4eb";
ctx.fillRect(width * 0.2, height * (0.5 - 0.12 * lift),
width * 0.6, 2 + height * Math.min(0.22, audio.low));
}In Sanic, enable Inline visuals and press Play. Watch the line lift on every beat and thicken when the bass drum sounds. Change 0.12 * lift to 0.24 * lift and save the JavaScript to increase its travel.
Frame reference
| Frame field | Meaning |
|---|---|
width, height | Drawing area in logical pixels, with the origin at the top left |
deltaSeconds | Seconds of audio playback since this instance’s previous draw. The first frame and paused redraws receive zero. Use this for motion and decay that should keep time when frame rates change. |
audio.rms | Overall RMS level on a linear scale from 0 to 1 |
audio.low, audio.mid, audio.high | Low, mid and high frequency energy from a recent 1,024-sample window, clamped to 0–1. Apply your own smoothing in a variable outside draw. |
audio.waveform | 64 signed mono sample values from the same window, between −1 and 1 |
transport.bar, transport.beat | One-based bar and beat within the score's meter |
transport.beatPhase | Fraction of the current beat, from 0 up to 1 |
transport.bpm, transport.playing | Tempo in quarter notes per minute and playback state |
Drawing and sprites
| To draw | Use |
|---|---|
| A solid rectangle | Set ctx.fillStyle and call ctx.fillRect(x, y, width, height). |
| A shape or line | Call ctx.beginPath(), ctx.moveTo(x, y) and ctx.lineTo(x, y). Use ctx.closePath() to join the ends. Call ctx.fill() or set ctx.strokeStyle and ctx.lineWidth and call ctx.stroke(). |
| A rotated or scaled object | Call ctx.save(), apply translate, scale or rotate (in radians), draw the object, then call ctx.restore(). |
For a sprite, place a PNG beside the script and read assets from the frame. This example centers a square sprite and sizes it to fit the shorter side of the cell.
function draw({ ctx, assets, width, height }) {
const side = Math.min(width, height);
ctx.drawImage(assets.image("witch.png"), (width - side) / 2,
(height - side) / 2, side, side);
}Keep the script inside the saved score’s folder or a subfolder. PNG paths resolve relative to the script and can include subfolders. Package the actual files with your score. Symbolic links are unsupported. Each visual can use up to eight PNGs, each up to 4 MiB and 2,048 pixels per side.
Audio source and placement
Use master |> visual("./pulse.js") for the mixed output. Add global: true to change the placement while retaining the audio source. Local visuals also fit into viz_grid. A score supports eight scripted visuals. Each call has its own JavaScript state. Resizing, switching between the editor and the panel, or changing placement preserves that state. Stopping playback disposes the instance.
Sanic runs each script in a separate helper. On macOS the helper runs in App Sandbox, and Sanic supplies the script and its PNGs. The maximum script size is 256 KiB. A stalled draw or a memory limit shows an error in the visual.
Edit the Moonroom demo
Download Moonroom with its JavaScript and artwork. Unzip the folder, open moonroom.sanic and press Play. The candle follows the kick. During a bar of drum rests, its flame settles while the witch keeps nodding to the beat. In witch.js, change the nod multiplier from 1 to 2 and save during playback. The witch nods twice per beat. A successful save restarts the script's variables. A broken save shows an error over the last working visual. Correct the file and save again to recover.
Inline GPU rendering (preview)
In the editor, Sanic can draw fills, strokes and PNG sprites on the GPU. Use the same draw(frame) function in a single cell or a viz_grid. JavaScript and PNG decoding run in the helper, and the editor caches sprites across frames.
Sanic chooses the renderer automatically. On the OpenGL path, if a frame exceeds the drawing or sprite budget, the helper finishes that frame as an image. Your script advances once either way. The Visualizations panel, global overlay and software renderer use the helper’s image output.
Draw in logical pixels using width and height. Sanic caps the backing resolution at 1280 × 720 and targets about 30 frames per second. Use deltaSeconds for animation speed. Scrolling a cell out of view or collapsing it suspends frame requests. When the cell returns, its next draw uses the current transport position.