canvaskit-wasm 0.39
build 2026-04-29
RuntimeEffect
Compile your own SkSL shader source at runtime, get back a RuntimeEffect, instantiate Shader instances from it. SkSL is Skia's GLSL-like shading language — same syntax for vertex / fragment math, same uniforms model.
const sksl = `
uniform float u_time;
uniform float2 u_resolution;
half4 main(float2 xy) {
float2 uv = xy / u_resolution;
float r = 0.5 + 0.5 * sin(u_time + uv.x * 6.28);
float g = 0.5 + 0.5 * sin(u_time + uv.y * 6.28 + 2.0);
float b = 0.5 + 0.5 * sin(u_time + (uv.x + uv.y) * 6.28 + 4.0);
return half4(r, g, b, 1.0);
}
`;
const effect = CK.RuntimeEffect.Make(sksl);
const paint = new CK.Paint();
let t = 0;
loop(() => {
t += 0.02;
const shader = effect.makeShader([t, w, h]);
paint.setShader(shader);
canvas.drawRect(CK.LTRBRect(0, 0, w, h), paint);
shader.delete();
surface.flush();
});
effect.makeShader(uniforms) allocates a new Shader per call — delete inside the loop or you'll leak one shader per frame. Uniforms are passed as a flat number[]; their order matches the uniform declarations in the SkSL source.
Members
| Member | Args | Returns | Notes |
|---|---|---|---|
delete | — | void | Free the WASM memory. Required. |
getUniformCount | — | number | Number of uniforms in the program. |
getUniformFloatCount | — | number | Total float slots across all uniforms (the array length you pass to makeShader). |
getUniform | i: number | SkSLUniform | Metadata for the i-th uniform. |
getUniformName | i: number | string | Name of the i-th uniform. |
makeShader | uniforms: number[] | Float32Array, localMatrix?: Matrix | Shader | Instantiate a shader with these uniform values. |
makeShaderWithChildren | uniforms, children: Shader[], localMatrix?: Matrix | Shader | Instantiate with child shaders that the SkSL program samples (declared as uniform shader in source). |
Static factories
| Factory | Args | Returns | Notes |
|---|---|---|---|
CK.RuntimeEffect.Make | sksl: string, errorCallback?: (msg: string) => void | RuntimeEffect | null | Compile SkSL source. Returns null on compile error; errorCallback receives the diagnostic. |
CK.RuntimeEffect.MakeForBlender | sksl: string, errorCallback? | RuntimeEffect | null | Compile a SkSL blender (operates on src+dst rather than just src). |
See also
Shader— whatmakeShaderreturns.Paint.setShader— install on a paint.- SkSL runtime effects — language reference (planned).