drawTrail()#
functionoav-p5drawTrail(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
| Name | Type | Description |
|---|---|---|
values | Array<Number> | Number | An array such as the one trail() returns. Or a single number: then drawTrail keeps its own trail of the last 2 seconds. |
x | Number | Left edge of the box. optional default 0 |
y | Number | Top edge of the box. optional default 0 |
w | Number | Width of the box. Left out, it is the canvas width. optional default width |
h | Number | Height of the box. Left out, it is the canvas height. optional default height |
lo | Number | The value drawn at the bottom of the box. optional default 0 |
hi | Number | The 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 |
name | String | Number | Only 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 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.drawTrail(…)is this function under another name.
Examples
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
}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
}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
}Related
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.lag()Followsvaluesmoothly instead of jumping: up inupseconds, down indownseconds (TouchDesigner's Lag CHOP).OAVp5.chopThe CHOP functions under one roof, plus the state of the module behind them:OAVp5.chop.lag(…),.state,.slots(),.reset().