open-audiovisual
Search GitHub

oav-p5 reference · Values over time (CHOP)

slope()#

functionoav-p5
slope(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

NameTypeDescription
valueNumber | BooleanThe number to watch. A boolean counts as 0 or 1. Anything that is not a finite number keeps the last slope.
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

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

Examples

jsOpenProcessing ↗
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);
}
Move the mouse sideways: the bar and the number show how fast you are going, and drop to 0 when you stop.
jsOpenProcessing ↗
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);
}
Hold the mouse: the ring grows and bounces, and its outline is thickest while it is changing fastest.

Related

  • lag() Follows value smoothly instead of jumping: up in up seconds, down in down seconds (TouchDesigner's Lag CHOP).
  • speed() Adds rate to a running total every second, so rotate(speed(TWO_PI)) turns once a second and the rate can change without the total ever jumping.
  • spring() Follows value like a mass on a spring: it overshoots, bounces and settles (TouchDesigner's Spring CHOP).
  • trail() The last few seconds of value as an array, 60 values a second, oldest first and newest last (TouchDesigner's Trail 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 3081

↑ Back to top · All names