open-audiovisual
Search GitHub

oav-p5 reference · Values over time (CHOP)

drawTrail()#

functionoav-p5
drawTrail(values, [x], [y], [w], [h], [lo], [hi], [name])

Also: OAVp5.chop.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.

The picture scope() shows in the corner, but drawn on the canvas: stroke(255); drawTrail(trail(micLevel, 3), 0, height - 100, width, 100). The line is one shape in the sketch's own stroke() colour and weight (white while noStroke() is in effect), with no fill, and the style you had before is put back afterwards.

The oldest value is at the left edge x, the newest at x + w. A number instead of an array is the shortcut: drawTrail(micLevel, 0, 0, width, 100) draws the last 2 seconds without a separate trail() call.

Before you call it

Call it in draw() after createCanvas(). With a number, the first call loads @openav/chop, which takes a few frames, and nothing is drawn until then.

Parameters

NameTypeDescription
valuesArray<Number> | NumberAn array such as the one trail() returns. Or a single number: then drawTrail keeps its own trail of the last 2 seconds.
xNumberLeft edge of the box. optional default 0
yNumberTop edge of the box. optional default 0
wNumberWidth of the box. Left out, it is the canvas width. optional default width
hNumberHeight of the box. Left out, it is the canvas height. optional default height
loNumberThe value drawn at the bottom of the box. optional default 0
hiNumberThe value drawn at the top of the box. Values outside lo..hi sit on the edge of the box. If hi equals lo, the line runs through the middle. optional default 1
nameString | NumberOnly when values is a single number: a name for its own trail, as the 8th argument (after hi). optional read from arguments

Returns

Array<Number> The array that was drawn: the one you passed, or the number's own trail. Nothing is drawn when it has fewer than 2 values.

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.drawTrail(…) is this function under another name.

Examples

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

function draw() {
  background(20);
  const wave = 0.5 + 0.5 * sin(frameCount * 0.05);
  stroke(255, 211, 107);
  strokeWeight(2);
  drawTrail(wave, 20, 100, width - 40, 100);   // a number: keeps its own 2 seconds
}
A moving sine line drawn on the canvas itself, so it would appear in a recording.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  noFill();
  strokeWeight(2);
}

function draw() {
  background(20);
  const raw = frameCount % 120 < 60 ? 1 : 0;
  const smooth = lag(raw, 0.3, 0.6);
  stroke(95, 224, 138);
  drawTrail(trail(raw, 3), 20, 60, width - 40, 80);       // 0 at the bottom, 1 at the top
  stroke(255, 211, 107);
  drawTrail(trail(smooth, 3), 20, 60, width - 40, 80);    // same box: drawn on top
}
The raw square wave in green and its lagged version in yellow, in the same box so you can see the delay.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  noFill();
  stroke(255);
}

function draw() {
  background(20);
  const wave = sin(frameCount * 0.05);   // -1..1
  drawTrail(trail(wave, 3), 20, 100, width - 40, 100, -1, 1);   // lo and hi say what the box shows
}
A wave that goes below 0: lo = -1 and hi = 1 put the bottom of the box at -1 and the top at 1, so the whole wave fits.

Related

  • 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.
  • lag() Follows value smoothly instead of jumping: up in up seconds, down in down seconds (TouchDesigner's Lag 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 3181

↑ Back to top · All names