setup()
setup() runs once at the start and is the natural place to size your canvas and populate any arrays your animation depends on, like the per-segment position arrays used here.
function setup() {
createCanvas(windowWidth, windowHeight);
segLength = min(width, height) / (numSegments + 3);
// Initialize all segments at center
for (let i = 0; i < numSegments; i++) {
baseX[i] = width / 2;
baseY[i] = height / 2;
endX[i] = width / 2;
endY[i] = height / 2;
angles[i] = 0;
}
// Gradient: purple (head) → cyan (tail)
headColor = color(180, 80, 255);
tailColor = color(0, 255, 255);
strokeCap(ROUND);
// Start target in the center
targetX = width / 2;
targetY = height / 2;
}
Line-by-line explanation (8 lines)
🔧 Subcomponents:
for (let i = 0; i < numSegments; i++) {
Places every segment's base and end point at the center of the screen so the tentacle starts as a single point before unfurling.
createCanvas(windowWidth, windowHeight);- Makes the canvas fill the entire browser window.
segLength = min(width, height) / (numSegments + 3);- Calculates how long each segment should be based on screen size, so the whole tentacle fits nicely regardless of window dimensions.
baseX[i] = width / 2;- Sets the starting x position of this segment's base to the horizontal center of the screen.
angles[i] = 0;- Initializes the segment's angle to zero radians (pointing right) before any motion begins.
headColor = color(180, 80, 255);- Defines the purple color used at the head end of the gradient.
tailColor = color(0, 255, 255);- Defines the cyan color used at the tail end of the gradient.
strokeCap(ROUND);- Makes the ends of each line segment rounded instead of squared off, so the tentacle looks smooth and continuous rather than jointed.
targetX = width / 2;- Sets the initial IK target to the center of the screen, matching the segments' starting position.