speed()#
functionoav-p5speed(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
| Name | Type | Description |
|---|---|---|
rate | Number | Boolean | How 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. |
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 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 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. - Plenty of sketches have a variable called
speed. If yours does, it keeps the name, andOAVp5.chop.speed(…)is this function under another name (third example).
Examples
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);
}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);
}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);
}speed (2 radians a second) and reaches the function as OAVp5.chop.speed.Related
lag()Followsvaluesmoothly instead of jumping: up inupseconds, down indownseconds (TouchDesigner's Lag CHOP).spring()Followsvaluelike a mass on a spring: it overshoots, bounces and settles (TouchDesigner's Spring 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().