This reference covers the 2D drawing and timeline values used in the rotating ring and staggered dot wave projects. Use it to check a method's inputs or understand coordinate behavior. It is not a complete API catalog.
The motion object is supplied by the code-layer runtime. These examples belong in Motion Lab's code editor.
Frame callback
motion.onFrame(callback) registers a synchronous function that runs for a rendered frame. Read timeline values and issue drawing commands inside this function.
motion.onFrame(() => {
motion.draw.clear();
motion.draw.setBackground("#090d1a");
motion.draw.circle(0.5, 0.5, 0.02, {
color: "#65e7ff",
fillOnly: true,
});
});
Keep one frame callback for this example. Replace the previous source when trying a new example instead of appending another callback.
The callback must be synchronous; do not declare it async or return a Promise. Calculate the current frame from the supplied timeline values. You do not need to create a browser animation loop with requestAnimationFrame().
Timeline values
motion.time: elapsed time in seconds from the code clip's start.motion.duration: code clip length in seconds.motion.progress: clip-relative time divided by clip duration, clamped to the range from zero to one.
Read these values during the callback so they reflect the current frame. For the reasoning behind time-based drawing, see Timeline time and loops.
Clear and background
motion.draw.clear() clears the drawing canvas.
motion.draw.setBackground(color) fills the canvas with the supplied paint. The tutorial uses a hexadecimal color string.
Call these before drawing the shapes for the current frame when you want each frame to replace the previous one.
Draw a circle
motion.draw.circle(x, y, radius, options) draws a circle with normalized coordinates.
xis relative to the frame width;0.5is the horizontal center.yis relative to the frame height;0.5is the vertical center.radiusis relative to the shorter frame dimension.options.colorsets the paint color used in this example.options.fillOnly: truedraws a filled shape without a stroke.
The top-left corner is (0, 0) and the bottom-right corner is (1, 1).
Equal changes in normalized x and y represent different pixel distances on a rectangular frame. Use a square frame for the circular orbit in the tutorial, or account for aspect ratio in your position calculations.
For example, in a 1920 × 1080 frame, circle(0.5, 0.5, 0.02, options) places the center at pixel (960, 540) with a radius of 21.6 pixels. The circle remains round because its radius uses the shorter dimension. An orbit using the same normalized displacement on x and y becomes an ellipse because those displacements use different dimensions.
Use finite numbers for coordinates and a nonnegative radius. Check divisions in your own formulas: a grid with one column cannot divide by columns - 1. Invalid numeric inputs should be fixed at their source rather than hidden by changing colors or layer settings.
Keep an orbit circular in any aspect ratio
motion.width and motion.height provide the drawing canvas dimensions in pixels. Scale normalized offsets by the shorter dimension to give x and y equal pixel distances:
motion.onFrame(() => {
motion.draw.clear();
motion.draw.setBackground("#090d1a");
const phase = (motion.time / Math.max(0.001, motion.duration)) * Math.PI * 2;
const shortest = Math.min(motion.width, motion.height);
const x = 0.5 + Math.cos(phase) * 0.28 * shortest / motion.width;
const y = 0.5 + Math.sin(phase) * 0.28 * shortest / motion.height;
motion.draw.circle(x, y, 0.02, {
color: "#65e7ff",
fillOnly: true,
});
});
This complete example draws a dot on a circular orbit whose radius is 28% of the shorter frame dimension. The path stays circular in portrait, landscape, and square projects. Normalized geometry remains proportional when output resolution changes without changing the aspect ratio.
Drawing order and layering
Within a callback, later drawing commands can cover earlier ones. Clear the canvas first, fill the background if needed, then draw shapes. Calling setBackground() after drawing the dots would paint over them.
For a code layer intended to sit over other visuals, omit setBackground() and keep clear(). That clears this layer's drawing canvas; it does not remove other layers from the composition. Layer order, opacity, and blending still affect the assembled result.
To diagnose an empty frame, reduce the source to the single-dot callback above and follow Code draws nothing. To understand timing rather than method signatures, read Timeline time and loops.