open-audiovisual
Search GitHub

oav-p5 reference · Values over time (CHOP)

trail()#

functionoav-p5
trail(value, [seconds], [name])

Also: OAVp5.chop.trail

The last few seconds of value as an array, 60 values a second, oldest first and newest last (TouchDesigner's Trail CHOP).

A history to draw or measure: drawTrail(trail(micLevel, 3), 20, 20, width - 40, 100) plots the last three seconds of the microphone level, and Math.max(...trail(x, 2)) is a two-second peak. The values are sampled 60 times a second whatever the frame rate, and the gaps between two frames are interpolated, so 30 fps and 144 fps give the same shape.

Change seconds between frames and the history is resized, keeping the newest values. The array you got last frame is never changed afterwards.

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 number to record. A boolean counts as 0 or 1. Anything that is not a finite number is skipped.
secondsNumberHow many seconds of history to keep, from 0.05 to 600. optional default 2
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

Array<Number> About seconds * 60 + 1 numbers (121 for 2 seconds), a new array every call. The length is fixed from the first call and filled with the first value, so you can draw it from frame one. It is an empty array until @openav/chop has loaded, 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.trail(…) is this function under another name. To see the line on the canvas use drawTrail(), to see it in the corner of the page use scope().

Examples

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

function draw() {
  background(20);
  const wave = 0.5 + 0.5 * sin(frameCount * 0.05);   // 0..1
  stroke(255);
  drawTrail(trail(wave, 3), 20, 100, width - 40, 100);   // the last 3 seconds
}
A sine wave drawn from its own history: it scrolls to the left as new values arrive at the right.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  noStroke();
}

function draw() {
  background(20);
  const t = trail(mouseX, 1);   // where the mouse was during the last second
  for (let i = 0; i < t.length; i += 6) {
    fill(255, 211, 107, 10 + (i / t.length) * 200);
    circle(t[i], mouseY, 24);
  }
}
A ghost trail behind the pointer: the older the copy, the fainter it is.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
}

function draw() {
  background(20);
  const t = trail(mouseX, 3);   // the last 3 seconds of the mouse position
  const left = Math.min(...t), right = Math.max(...t);
  noStroke();
  fill(255, 211, 107, 90);
  rect(left, 0, right - left, height);   // the band the mouse covered
  stroke(255);
  drawTrail(t, 0, 20, width, 60, 0, width);
}
A band shows how far left and right the mouse went in the last three seconds, with the trail itself drawn across the top.

Related

  • drawTrail() Draws a line through values on the canvas, so it is in the work and in a recording: the oldest at x, lo at the bottom of the box, hi at the top.
  • 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.
  • lag() Follows value smoothly instead of jumping: up in up seconds, down in down seconds (TouchDesigner's Lag CHOP).
  • 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 3079

↑ Back to top · All names