slope()#
functionoav-p5slope(value, [name])Also: OAVp5.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).
Ask how hard something is moving rather than where it is: abs(slope(mouseX)) is the mouse speed in pixels per second, and slope(micLevel) jumps up at the start of a word. It is as noisy as its input, so smooth it when it flickers: lag(abs(slope(x)), 0.05, 0.3).
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 watch. A boolean counts as 0 or 1. Anything that is not a finite number keeps the last slope. |
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
Number The change per second, in the units of value (pixels per second for a pixel position). 0 on the first call, and 0 until @openav/chop has loaded or 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.slope(…)is this function under another name.
Examples
function setup() {
createCanvas(400, 300);
textSize(16);
}
function draw() {
background(20);
const v = abs(slope(mouseX)); // pixels per second
fill(255);
text(nf(v, 1, 0) + ' px/s', 20, 120);
rect(20, 140, min(v / 10, width - 40), 24);
}function setup() {
createCanvas(400, 300);
}
function draw() {
background(20);
const r = spring(mouseIsPressed ? 140 : 60, 100, 6); // a radius that bounces
const moving = lag(abs(slope(r)), 0.05, 0.3); // how fast it is changing, smoothed
noFill();
stroke(255, 211, 107);
strokeWeight(2 + min(14, moving / 40));
circle(width / 2, height / 2, r * 2);
}Related
lag()Followsvaluesmoothly instead of jumping: up inupseconds, down indownseconds (TouchDesigner's Lag CHOP).speed()Addsrateto a running total every second, sorotate(speed(TWO_PI))turns once a second and the rate can change without the total ever jumping.spring()Followsvaluelike a mass on a spring: it overshoots, bounces and settles (TouchDesigner's Spring CHOP).trail()The last few seconds ofvalueas an array, 60 values a second, oldest first and newest last (TouchDesigner's Trail CHOP).OAVp5.chopThe CHOP functions under one roof, plus the state of the module behind them:OAVp5.chop.lag(…),.state,.slots(),.reset().