open-audiovisual
Search GitHub

oav-p5 reference · Values over time (CHOP)

lag()#

functionoav-p5
lag(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

NameTypeDescription
valueNumber | BooleanThe 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.
upNumberSeconds 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
downNumberSeconds to follow a fall. Left out, it is the same as up. optional default up
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 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 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.
  • A sketch with its own variable or function called lag keeps it. OAVp5.chop.lag(…) is this function under another name.

Examples

jsOpenProcessing ↗
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);
}
A circle that chases the mouse: it never jumps, it glides, and takes about half a second to catch up.
jsOpenProcessing ↗
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);
}
Hold the mouse down: the circle swells quickly and, when you let go, shrinks back slowly.
jsOpenProcessing ↗
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);
  }
}
Six dots follow the mouse, each a little slower than the one above. The names keep each dot's memory its own even if you later add or remove dots.

Related

  • spring() Follows value like 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.
  • micLevel How loud the microphone hears things, from 0 (a quiet room) to 1 (shouting).
  • knob1 The position of a controller knob, from 0 to 1: knob1 is the first knob, knob2 the second, up to knob8.
  • trail() The last few seconds of value as an array, 60 values a second, oldest first and newest last (TouchDesigner's Trail CHOP).
  • scope() Shows value in a small oscilloscope in the corner of the page and gives it back, so you can wrap any expression to watch it.
  • 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 3037

↑ Back to top · All names