open-audiovisual
Search GitHub

oav-p5 reference · Values over time (CHOP)

speed()#

functionoav-p5
speed(rate, [name])

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

Use the total to turn or move something: rotate(speed(TWO_PI)) is one turn a second, x = speed(60) % width slides 60 pixels a second and wraps around. You pass the rate, not the position, so the rate can follow a knob or the microphone (speed(micLevel * TWO_PI) spins faster the louder it gets) and the angle never jumps, which frameCount * rate would do.

Time is in seconds, the same at any frame rate. One frame counts for at most a quarter of a second, so coming back to a hidden tab does not make the shape leap.

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
rateNumber | BooleanHow much to add per second, in the unit you use the result in (radians for rotate(), pixels for a position). It may change every frame. A boolean counts as 0 or 1.
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 running total, 0 at the start. It is 0 until @openav/chop has loaded (a few frames), and 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.
  • Plenty of sketches have a variable called speed. If yours does, it keeps the name, and OAVp5.chop.speed(…) is this function under another name (third example).

Examples

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

function draw() {
  background(20);
  translate(width / 2, height / 2);
  rotate(speed(TWO_PI));   // one turn a second
  rect(0, 0, 120, 120);
}
A square that turns once a second, steadily.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  rectMode(CENTER);
}

function draw() {
  background(20);
  const rate = map(mouseX, 0, width, -TWO_PI, TWO_PI);   // turns per second, left to right
  translate(width / 2, height / 2);
  rotate(speed(rate));
  rect(0, 0, 120, 120);
}
Move the mouse: the left side spins the square one way, the middle stops it, the right side spins it the other way, and it never jumps.
jsOpenProcessing ↗
let speed = 2;   // the sketch's own variable called speed

function setup() {
  createCanvas(400, 300);
  rectMode(CENTER);
}

function draw() {
  background(20);
  translate(width / 2, height / 2);
  rotate(OAVp5.chop.speed(speed));   // the same function, under another name
  rect(0, 0, 120, 120);
}
The sketch keeps its own speed (2 radians a second) and reaches the function as OAVp5.chop.speed.

Related

  • lag() Follows value smoothly instead of jumping: up in up seconds, down in down seconds (TouchDesigner's Lag CHOP).
  • spring() Follows value like a mass on a spring: it overshoots, bounces and settles (TouchDesigner's Spring 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).
  • 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 3063

↑ Back to top · All names