trail()#
functionoav-p5trail(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
| Name | Type | Description |
|---|---|---|
value | Number | Boolean | The number to record. A boolean counts as 0 or 1. Anything that is not a finite number is skipped. |
seconds | Number | How many seconds of history to keep, from 0.05 to 600. optional default 2 |
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
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 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.trail(…)is this function under another name. To see the line on the canvas usedrawTrail(), to see it in the corner of the page usescope().
Examples
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
}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);
}
}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);
}Related
drawTrail()Draws a line throughvalueson the canvas, so it is in the work and in a recording: the oldest atx,loat the bottom of the box,hiat the top.scope()Showsvaluein a small oscilloscope in the corner of the page and gives it back, so you can wrap any expression to watch it.lag()Followsvaluesmoothly instead of jumping: up inupseconds, down indownseconds (TouchDesigner's Lag CHOP).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().