Reference
Everything your code can use, in one place: the inputs every shader has, and the Canvas2D layer's inputs, drawing helpers, fonts and libraries. Coding specification and The Canvas2D layer explain them, with examples.
Shader inputs
Your shader sets the colour of each pixel in a mainImage function:
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
}fragCoord is the pixel's position, from (0, 0) at the bottom left to iResolution at the top right.
Set fragColor to its colour, with red, green and blue from 0 to 1. The alpha is always 1.
| type | name | what it is |
|---|---|---|
float |
iTime |
The time into the animation, in seconds. |
float |
iDuration |
The length of the animation, in seconds. |
vec2 |
iResolution |
The size of the frame in pixels. (Shadertoy's is a vec3.) |
int |
iFrame |
The frame number, counting from 0 at 60 a second. It goes back to 0 when you build, when the animation loops or goes back to the start, and when iPrevFrame is cleared. |
sampler2D |
iPrevFrame |
The frame drawn just before this one. |
sampler2D |
iTexture0 … iTexture7 |
The 8 images or videos set in the Textures tab. |
float |
iFloat0 … iFloat15 |
The 16 numbers set on the timeline or with sliders. |
sampler2D |
iBuffer0 … iBuffer3 |
What each buffer drew most recently, when the animation has buffers. |
sampler2D |
iCanvas2D |
What the Canvas2D layer drew this step, with premultiplied alpha. Transparent without a Canvas2D layer. |
Textures
texture(iTexture0, uv) reads a texture's colour at uv, which runs from 0 to 1 across the texture with
(0, 0) at the bottom left. fragCoord / iResolution fills the frame with it. Textures repeat outside 0 to 1.
A texture can also be a video. At time iTime it shows the video's frame at that time, and a video shorter
than the animation loops.
iPrevFrame and iFrame
iPrevFrame is a texture like the others, so you can read the last frame and build on it, for trails or
simulations. It starts black, and is cleared each time you build, the canvas changes size or a texture
changes. iFrame goes back to 0 then too, and when the animation loops or goes back to the start, so
if (iFrame == 0) is the place to set things up. While it plays, iPrevFrame, iFrame and the
buffers move on 60 times a second, whatever the screen's refresh rate or the export's frame rate, so a
simulation runs at the same speed everywhere.
Buffers and shared code
The ⊕ above the code adds a buffer, the Canvas2D layer or shared GLSL code. A buffer is another shader that
draws before the image each frame. Every tab can read what it drew as iBuffer0, iBuffer1 and so on.
The tabs go in the order they draw, so reading an earlier buffer gives this frame's picture, and reading
the same buffer or a later one gives the last frame's. Buffers hold any numbers, not just colours, in all
four channels, so they can run simulations.
Functions, constants and s in the shared.glsl tab, which ⊕ adds, can be used in every shader tab.
Coming from Shadertoy
Shaders are GLSL ES 3.00 (WebGL 2). smoothstep.io adds the version, precision and these inputs before
your code. Shadertoy's iChannel0, iMouse and iDate don't exist here: use iTexture0 for
iChannel0, and iBuffer0 to iBuffer3 for buffers.
Canvas2D layer
The Canvas2D layer is JavaScript that draws on a 2D canvas, on the same 60-a-second steps as the buffers.
Its tab comes first, so every shader reads this step's drawing as iCanvas2D. With "Draw over the image"
on, the drawing also goes over the image.
// Runs after each build and restart. Optional; what it returns is data.
function setup({width, height, resolution, duration, floats, textures}) {
return {};
}
function draw(ctx, {time, frame, duration, width, height, resolution, floats, textures, data}) {
clear(ctx);
ctx.fillText("Hello", 60, 110);
}The canvas is 1080 units tall whatever its real size, with y down from the top left. It keeps what's drawn
until you clear(ctx). Variables outside the functions keep their values between steps. console.log
prints in the Console tab under the canvas, with the step it was printed on.
| type | name | what it is |
|---|---|---|
context |
ctx |
The canvas's 2D drawing context, scaled so the canvas is 1080 units tall. |
number |
time |
The time of this step, in seconds, the same as iTime. |
number |
frame |
The step number, the same as iFrame. It goes back to 0 when the layer restarts. |
number |
duration |
The length of the animation, in seconds. |
number |
width |
The canvas's width in canvas units: 1920 for 16:9, as the canvas is 1080 units tall. |
number |
height |
The canvas's height in canvas units, always 1080. |
number[] |
resolution |
The frame's size in pixels, [width, height], the same as iResolution. height / resolution[1] is one pixel in canvas units. |
number[] |
floats |
The 16 timeline values at this time: floats[0] is iFloat0. |
ImageBitmap[] |
textures |
The 8 texture slots, ready for ctx.drawImage, or null for an empty slot. A video's slot holds its frame at this step's time. |
any |
data |
Whatever setup returned. Only draw gets it. |
Drawing helpers
drawList draws nested lists of commands with y up from -1 to 1, like a shader's uv.
| helper | what it does |
|---|---|
drawList(ctx, list, view = {xmin, xmax, ymin, ymax}) |
Draws a nested list of commands with y up from -1 to 1, like a shader's uv. view shows another part of the plane. |
clear(ctx)clear(ctx, colour) |
Clears the whole canvas to transparent, or to a colour. |
seededRandom(seed = 1) |
A function that returns random numbers from 0 up to 1, the same ones each time for the same seed. |
Set({fillStyle, strokeStyle, lineWidth, font, globalAlpha, lineDash, ...}) |
Sets the context's properties for the rest of its list. lineDash takes an array. new Set() is still JavaScript's Set. |
Disk({center = [0, 0], radius = 1, fill = true, edge = false, startAngle = 0, endAngle = 2π, sector = false, radiusInPixels = false}) |
A filled circle, or its outline with edge: true and fill: false. Angles draw an arc; sector joins its ends to the centre. |
Polygon({pts, fill = true, edge = false}) |
A filled shape through the points, like [[0, 0], [1, 0], [0, 1]]. |
Line({pts, closed = false}) |
A line through the points. closed joins the last point to the first. |
Text({at = [0, 0], text, align, baseline, fill = true, edge = false}) |
Text at a point in the current font, upright and the same size whatever the list's scale. |
Rotate({angle, center = [0, 0]}) |
Turns what follows in its list anticlockwise by angle radians, about center. |
Translate({offset}) |
Moves what follows in its list by offset, like [0.5, 0]. |
Scale({scale = 1, scaleX = scale, scaleY = scale, center = [0, 0]}) |
Makes what follows in its list bigger or smaller about center. scaleX and scaleY scale one direction. |
AbsoluteLineWidth(units) |
Sets the line width in canvas units, whatever the list's scale. Lines in a drawList start 2 units wide. |
Fonts
Text can use Inter, Arimo, Exo 2, Tinos and Courier Prime, as in
ctx.font = "600 64px Inter".
Libraries
Import a library on its own line at the top of the code.
| import | library | licence |
|---|---|---|
import planck from "planck"; |
planck.js: 2D physics (a JavaScript Box2D) | MIT |
import Matter from "matter-js"; |
matter.js: 2D physics | MIT |
import {createNoise2D} from "simplex-noise"; |
simplex-noise: Smooth noise in 2D, 3D and 4D | MIT |
import {Delaunay} from "d3-delaunay"; |
d3-delaunay: Delaunay triangulations and Voronoi diagrams | ISC |
Click a helper, an input or a ctx method in the code to see what it does.