Documentation / Reference

shader_with

Prepare tracked shader inputs independently, then render their committed snapshot.

Construction

Use shader_with(prepare) -> ShaderView, where prepare: Fn(&mut DrawingInputs<'_, '_>) -> Shader + 'static. Unlike canvas_with, this constructor takes one preparation closure; WGSL supplies the drawing program. Import the API from refract::prelude::*.

The supplied program in this example declares intensity: f32 and tint: vec4<f32> in its Uniforms structure, as in the Shaders guide.

shader_with.rs
use refract::prelude::*;

fn reactive_wave(program: Shader, intensity: Binding<f32>) -> impl View {
    shader_with(move |inputs| {
        program.clone()
            .uniform("intensity", inputs.read(&intensity))
            .uniform("tint", Color::BLUE)
    })
    .animatable_inputs()
    .width(320.0)
    .height(180.0)
}

Tracked preparation and committed inputs

Read bindings through DrawingInputs::read or with, and inherited values through environment::<K>(). Those dependencies belong to the shader mount, so passing bindings into preparation avoids subscribing its parent to the same values.

Return a complete bound Shader, including its uniforms, image, and fallback color. Preparation must be pure because it can run again or be discarded before commit. Keep expensive source parsing and image decoding outside the closure; clone an existing program and image instead.

GPU frames use committed inputs without accessing the graph or reevaluating view bodies. Parameter interpolation and scheduled time can advance independently. .animatable_inputs() enables interpolation; a subsequent update still needs animation intent. See animated uniforms.

View-specific reference

These methods preserve ShaderView. Configure them before wrappers consume the concrete builder.

MethodPurpose
scheduleChooses OnChange (the default), Animation, or Paused. OnChange renders at time zero when inputs or dimensions change. Animation advances visible time and requests frames; Paused retains the clock phase while still accepting input and size updates. ShaderSchedule aliases CanvasSchedule and follows its visibility and reduced-motion policy.
animatable_inputsOpts bound scalar and vector uniforms into animation transactions. Use with_animation or a value-scoped animation wrapper to supply animation intent. Keep bound names and vector dimensions stable. Images and fallback colors switch immediately. Uniform transitions can continue while the shader clock is paused.

schedule

Chooses OnChange (the default), Animation, or Paused. OnChange renders at time zero when inputs or dimensions change. Animation advances visible time and requests frames; Paused retains the clock phase while still accepting input and size updates. ShaderSchedule aliases CanvasSchedule and follows its visibility and reduced-motion policy.

Signature
fn schedule(self, schedule: ShaderSchedule) -> Self

animatable_inputs

Opts bound scalar and vector uniforms into animation transactions. Use with_animation or a value-scoped animation wrapper to supply animation intent. Keep bound names and vector dimensions stable. Images and fallback colors switch immediately. Uniform transitions can continue while the shader clock is paused.

Signature
fn animatable_inputs(self) -> Self

Common view modifiers

Dimensions, paint, labels, and typed event callbacks preserve ShaderView. Configure schedule and animatable_inputs before padding, frame, animation, transaction, or other wrappers. Shader views have no view_box or child methods: use WGSL coordinates for pixels and compose separate views around the surface.

Allocate dimensions through the view or its parent; shaders have no intrinsic size. Use radius with clipped for rounded clipping. The containing view owns input and accessibility metadata; pixels are not individual controls. See View Modifiers and Shaders.

Source reference

The implementation is in crates/core/src/drawing/shader.rs. The shared example is in examples/shader.