open-audiovisual
Search GitHub

oav-p5 reference · Values over time (CHOP)

spring()#

functionoav-p5
spring(value, [stiffness], [damping], [name])

Also: OAVp5.chop.spring

Follows value like a mass on a spring: it overshoots, bounces and settles (TouchDesigner's Spring CHOP).

Wrap a target that jumps: circle(width / 2, height / 2, spring(mouseIsPressed ? 300 : 100, 100, 6)). The size does not just glide to the new target (that is lag()): it swings past it, comes back and settles.

Stiffness and damping are the two knobs. A stiff, lightly damped spring rings like a ruler on a table. A soft, heavily damped one just eases. A big jump or a low frame rate does not break it: the motion is the same at any frame rate.

Before you call it

Nothing to call or declare: write it in draw(). The first call loads @openav/chop, which takes a few frames.

Parameters

NameTypeDescription
valueNumber | BooleanThe target the spring is pulled toward. A boolean counts as 0 or 1.
stiffnessNumberHow fast it swings: 100 swings about 1.6 times a second. Higher is snappier. Negative numbers count as 0. optional default 100
dampingNumberHow soon it settles: 6 gives two or three bounces, 20 and up none. The edge where it stops bouncing is 2 * sqrt(stiffness). optional default 6
nameString | NumberA name for this call's memory, written as the last argument. Optional. Without one, oav-p5 tells calls apart by their order in draw(); with one, the call is the same wherever it is made. optional read from arguments

Returns

Number The spring's position now. The first value it sees is taken as it is. It returns value as it is until @openav/chop has loaded (a few frames), and for an unnamed call outside draw().

Notes

  • Unnamed calls find their memory by their order in draw(): the first lag() is the first lag, the second is the second. Put one inside an if, or in a loop whose length changes, and the order slips: a card says so, and from then on oav-p5 matches each call by the line it is written on. A name at the end is the sure way. Outside draw() an unnamed call has nothing to remember by: it returns what Returns says and a card says so.
  • OAVp5.chop.spring(…) is this function under another name.

Examples

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
}

function draw() {
  background(20);
  const size = spring(mouseIsPressed ? 300 : 100, 100, 6);   // overshoots, then settles
  circle(width / 2, height / 2, size);
}
Hold the mouse: the circle springs open, overshoots and settles. Let go and it bounces back down.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
}

function draw() {
  background(20);
  const x = spring(mouseX, 60, 4);   // soft and bouncy
  const y = spring(mouseY, 60, 4);
  circle(x, y, 40);
}
A circle hung on the mouse by a soft spring: it swings around the pointer instead of sticking to it.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  noStroke();
}

function draw() {
  background(20);
  const target = mouseIsPressed ? 340 : 60;
  const dampings = [2, 6, 20];
  for (let i = 0; i < 3; i++) {
    const x = spring(target, 100, dampings[i]);   // same pull, three dampings
    circle(x, 60 + i * 90, 36);
  }
}
Hold the mouse: three circles are pulled right by the same spring with different damping. The top one bounces many times, the middle one a few, the bottom one not at all.

Related

  • lag() Follows value smoothly instead of jumping: up in up seconds, down in down seconds (TouchDesigner's Lag CHOP).
  • speed() Adds rate to a running total every second, so rotate(speed(TWO_PI)) turns once a second and the rate can change without the total ever jumping.
  • slope() How fast value is changing, per second: positive while it rises, negative while it falls, 0 while it holds still (TouchDesigner's Slope CHOP).
  • OAVp5.chop The CHOP functions under one roof, plus the state of the module behind them: OAVp5.chop.lag(…), .state, .slots(), .reset().

p5/oav-p5.js, line 3068

↑ Back to top · All names