lag()#
functionoav-p5lag(value, [up], [down], [name])Also: OAVp5.chop.lag
Follows value smoothly instead of jumping: up in up seconds, down in down seconds (TouchDesigner's Lag CHOP).
Wrap any number that jumps: circle(width / 2, height / 2, 50 + lag(micLevel, 0.03, 0.4) * 300) is a VU meter. It opens within 0.03 seconds when someone speaks and takes 0.4 seconds to close. up is the time to follow a rise, down the time to follow a fall. Leave down out and both are up. Leave both out and they are 0.1.
The seconds are TouchDesigner's: lag(x, 0.1) moves like a Lag CHOP set to 0.1, so the numbers you learn here carry over. That is about the time the output takes to cover 90% of a jump (underneath, a one-pole lag whose time constant is the seconds divided by 2.3). The curve is the same at 30 and at 144 frames per second.
The first value it sees is taken as it is, so nothing glides in from 0. A boolean counts as 0 or 1: lag(mouseIsPressed, 0.2) is a soft switch.
Before you call it
Nothing to call or declare: write it in draw(). The first call loads @openav/chop from next to oav-p5.js, which takes a few frames.
Parameters
| Name | Type | Description |
|---|---|---|
value | Number | Boolean | The number to follow. A boolean counts as 0 or 1. Anything that is not a finite number (a string, NaN) goes straight through and the lag keeps what it had. |
up | Number | Seconds to follow a rise: about how long the output takes to cover 90% of a jump upward. Negative numbers count as 0. optional default 0.1 |
down | Number | Seconds to follow a fall. Left out, it is the same as up. optional default up |
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 smoothed value. 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. - A sketch with its own variable or function called
lagkeeps it.OAVp5.chop.lag(…)is this function under another name.
Examples
function setup() {
createCanvas(400, 300);
}
function draw() {
background(20);
const x = lag(mouseX, 0.5); // follows the mouse, half a second behind
circle(x, height / 2, 40);
}function setup() {
createCanvas(400, 300);
}
function draw() {
background(20);
const open = lag(mouseIsPressed, 0.15, 0.8); // a boolean counts as 0 or 1
fill(255, 211, 107);
circle(width / 2, height / 2, 40 + open * 240);
}function setup() {
createCanvas(400, 300);
noStroke();
}
function draw() {
background(20);
for (let i = 0; i < 6; i++) {
const seconds = 0.1 + i * 0.12;
const x = lag(mouseX, seconds, seconds, 'dot' + i); // a name per dot
fill(255, 211, 107);
circle(x, 40 + i * 44, 24);
}
}Related
spring()Followsvaluelike a mass on a spring: it overshoots, bounces and settles (TouchDesigner's Spring CHOP).env()A 0..1 pulse that jumps up after each event (a kick, a beat, a key, a press) and then fades out: TouchDesigner's Trigger CHOP.micLevelHow loud the microphone hears things, from 0 (a quiet room) to 1 (shouting).knob1The position of a controller knob, from 0 to 1:knob1is the first knob,knob2the second, up toknob8.trail()The last few seconds ofvalueas an array, 60 values a second, oldest first and newest last (TouchDesigner's Trail CHOP).scope()Showsvaluein a small oscilloscope in the corner of the page and gives it back, so you can wrap any expression to watch it.OAVp5.chopThe CHOP functions under one roof, plus the state of the module behind them:OAVp5.chop.lag(…),.state,.slots(),.reset().