spring()#
functionoav-p5spring(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
| Name | Type | Description |
|---|---|---|
value | Number | Boolean | The target the spring is pulled toward. A boolean counts as 0 or 1. |
stiffness | Number | How fast it swings: 100 swings about 1.6 times a second. Higher is snappier. Negative numbers count as 0. optional default 100 |
damping | Number | How 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 |
name | String | Number | A 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 firstlag()is the first lag, the second is the second. Put one inside anif, 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. Outsidedraw()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
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);
}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);
}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);
}
}Related
lag()Followsvaluesmoothly instead of jumping: up inupseconds, down indownseconds (TouchDesigner's Lag CHOP).speed()Addsrateto a running total every second, sorotate(speed(TWO_PI))turns once a second and the rate can change without the total ever jumping.slope()How fastvalueis changing, per second: positive while it rises, negative while it falls, 0 while it holds still (TouchDesigner's Slope CHOP).OAVp5.chopThe CHOP functions under one roof, plus the state of the module behind them:OAVp5.chop.lag(…),.state,.slots(),.reset().