home/ modules/ interaction-motion-and-transforms

Interaction, motion, and coordinate transforms in p5.js

  • learner can capture mouse, keyboard, and event input to make a sketch interactive
  • learner can animate with easing, frame-count timing, and motion-blur trails
  • learner can compose transforms with translate/rotate/scale and isolate them with push/pop matrix stacks

Produce an interactive kinetic sketch where mouse and keyboard input steer eased, looping motion of transformed shapes, using push/pop so local transforms and motion trails compose cleanly.

This module is where a static sketch becomes an instrument. In a live-visuals or VJ context — p5.js projected behind a set, reacting to the performer at the keyboard — the difference between a demo and a performance is exactly this trio: input you can play, motion that feels alive rather than teleported, and transforms that stay local so one gesture never corrupts the rest of the scene.

Start supported: wire the cursor to a single shape using the current-versus-previous mouse position idiom, then add one key command via the event functions that fire once per press instead of every frame. Next, replace direct position-setting with easing — move a fraction of the remaining distance each frame — so the shape chases the cursor organically, and let frame-count-modulo drive a seamlessly looping sweep. Swap the full-canvas clear for the semi-transparent-rectangle fade to earn motion trails. Finally, rebuild the shape as a locally transformed object: translate to its center, rotate and scale there, and wrap the whole thing in push/pop so trails and multiple shapes compose without transform drift.

Every required atom gates the capstone directly: without event handling the sketch is not steerable, without easing and the fade technique the motion reads as mechanical, and without matrix isolation the composed scene falls apart the moment a second shape appears. The supporting atoms widen the craft — mapping input ranges onto rotation angles, arrays for scaling one shape into a swarm, the WEBGL center-origin gotcha when you later go 3D, and Bret Victor’s gesture-recording idea as a lens on why performed motion beats keyframed motion. Drill easing updates, push/pop pairing, and modulo looping until they are reflexes; on stage there is no time to derive them.

Runnable examples

Generated from the context/ instrument corpus by concept (redistributable idioms only). Do not edit — regenerate with gen-module-examples.mjs.

rotation-animation

uv = rot(u_time * 0.3) * uv;

glsl-0029 · public-domain

osc(10).rotate(() => time * 0.2).out()

hydra-0006 · CC0-1.0

oscillation

let y = height/2 + sin(frameCount * 0.05) * 100

p5live-0004 · CC0-1.0

float rings = abs(sin(length(uv)*20.0 - u_time*2.0));

glsl-0039 · public-domain

easing-curve

x = lerp(x, targetX, 0.1)

p5live-0032 · CC0-1.0

noise-drift

let x = noise(frameCount*0.002)*width, y = noise(frameCount*0.003)*height

p5live-0007 · CC0-1.0

Atoms in this module

Required — these gate the capstone

mouseX, mouseY and pmouseX, pmouseY give current and previous cursor positions for interaction
Concept L2 First instrument H
Processing keyboard events use keyPressed and key variables, with keyCode for special keys
Concept L2 First instrument H
Processing event functions like mousePressed() interrupt draw() cleanly, running once per event
Concept L2 First instrument H
Easing moves a value toward a target by a fraction of the remaining distance each frame
Concept L2 First instrument H
frameCount % width produces seamlessly looping horizontal motion in p5.js
Procedure L2 First instrument H
Drawing a semi-transparent black rectangle each frame creates a motion trail by gradual fade
Concept L2 First instrument H
translate() shifts Processing's coordinate origin, and pushMatrix/popMatrix save and restore transform state
Concept L2 First instrument H
rotate() and scale() in Processing transform geometry in local coordinate space, not screen space
Concept L2 First instrument H
translate() and rotate() transform the drawing origin; pushMatrix/popMatrix save and restore it
Procedure L2 First instrument H
push() and pop() isolate transformations so they do not accumulate globally in p5.js
Procedure L2 First instrument H

Supporting — enrichment, not gating

norm(), lerp(), and map() convert and interpolate values between numeric ranges
Concept L2 First instrument H
Arrays in Processing store multiple values under one name, accessed by zero-based index
Concept L2 First instrument H
In p5.js WEBGL mode the coordinate origin is at the canvas center, not the top-left
Concept L2 First instrument HG
Recording performed gestures as animation data creates more natural motion than keyframing
Concept L2 First instrument HI
Every visual animation is a parameter driven by a function of time — the character of motion is entirely in that function
Concept L2 First instrument HG
Geometric motion should be restrained and mechanical-but-eased so the eye can follow every edge
Principle L2 First instrument HG
Minimal motion is a single well-timed easing-curve drift — one move is the whole event
Concept L2 First instrument HL