39 Organize a source program
Once a short example works, put it in a reusable Racket module. Use provide to export the value another module should load. Keep rendering commands in a runner or module+, so loading the descriptions does not write a movie.
39.1 A reusable source file
The Quick Start already gives you a reusable module. Its export is simply:
(provide animation)
Loading the completed "scribblings/examples/moving-circle.rkt" defines the animation but does not render it. Keep render-frames! in a separate runner.
This is intentionally shown as module source instead of an interactive examples form: provide describes a module boundary, and that boundary is the point of this example.
A source file for a slide project instead exports its storyboard, normally as film.
39.2 Add blocks when you need frequent edits
A source program can divide a longer animation into named blocks. Each block receives the Scene made so far and returns the next Scene. This lets the preview rerun the changed block and later blocks instead of starting over.
The block program itself is headless, so the manual can construct and compile it directly:
(define-scene-program hot-reload-demo #:initial (make-scene) (scene-block setup (scene) (scene-wait (scene-add scene (circle #:id 'dot #:center (vec2 -3 0) #:radius 1/2 #:fill "tomato" #:stroke "firebrick")) 1)) (scene-block move-dot (scene) (scene-play scene (move-to 'dot (vec2 3 0)) #:duration 2)) (scene-block hold (scene) (scene-wait scene 1))) (define compiled (compile-scene-program hot-reload-demo)) (define animation (compiled-scene-program-scene compiled))
> (scene-program? hot-reload-demo) #t
> (compiled-scene-program? compiled) #t
> (scene-duration animation) 4
> (scene->pict animation 4)
0 s — setup
2 s — movement
4 s — held result
Sample times are measured from the start of this example.
The blocks do not edit the incoming Scene. Their names appear in the preview’s block selector. Changing move-dot can reuse setup; changing code outside the blocks rebuilds the source program.
The complete maintained GUI example is "examples/source-block-hot-reload.rkt". The next chapter opens that exact example in a preview. Do not replace its filename with an undefined placeholder such as "derivative.rkt".
