drawRhythm()#
functionoav-p5drawRhythm([x], [y], [w], [h], [style], [o])Also: OAVp5.drums.draw
Draws the rhythm on the canvas as a grid, a circle or a spiral, with a playhead and the hits lighting up, so a recording has it too.
Call it in draw() after background(): drawRhythm(20, 100, 360, 100). Each row is one drum that plays in the pattern, a hit lights up the moment it is heard (the clock drumPhase reads), and a hit between two steps, such as a triplet, sits where it falls. The grid card is part of the page and a recording never shows it, but the canvas is what a recording takes, so this is how the rhythm gets into the video.
With no box it draws a strip along the bottom for 'grid' and a square in the middle for the other two. It draws with p5's own shapes between push() and pop(), so it leaves your styles alone and works in 2D and in WEBGL, in p5 1.x and 2.x. In WEBGL the origin is the middle of the canvas: drawRhythm(-width / 2, -height / 2, width, height, 'circle') fills it.
Before you call it
Start the machine with startDrums() first. Before it plays you get an empty four-lane grid.
Parameters
| Name | Type | Description |
|---|---|---|
x | Number | Left edge of the box to draw in. In WEBGL the middle of the canvas is (0, 0). If any of x, y, w, h is left out, the default box is used. optional |
y | Number | Top edge of the box. optional |
w | Number | Width of the box. optional |
h | Number | Height of the box. optional |
style | String | Object | 'grid' (the 16 steps with a playhead), 'circle' (a ring of 16 per drum with a hand turned by drumPhase) or 'spiral' (the last few bars wound outwards, now at the tip). An object here is taken as o, with style inside it. optional default 'grid' |
o | Object | Options: colors ({ kick: '#f66', … }, any p5 colour per drum), color (one colour for all), off (the empty steps), playhead (a colour, or false for none), bg (a background for the box) and bars (spiral only, 1 to 16, 4 if left out). optional |
Returns
Boolean true if it drew something, false when the box is empty (w or h is not above 0) or there is no p5 global mode to draw with.
Notes
OAVp5.drums.draw(x, y, w, h, style, o)is the same function as a method.
Examples
function setup() {
createCanvas(400, 300);
startDrums('breakbeat', 120);
}
function draw() {
background(14, 16, 26);
drawRhythm(20, 100, 360, 100); // the 16 steps with a playhead
}function setup() {
createCanvas(400, 300);
startDrums('bd(3,8), [~ sd]*2, hh*8, ~ ~ ~ cp', 104);
}
function draw() {
background(14, 16, 26);
drawRhythm(10, 10, 180, 180, 'circle');
drawRhythm(210, 10, 180, 180, 'spiral', { bars: 4, colors: { kick: '#ff7b54' } });
drawRhythm(20, 220, 360, 60, 'grid', { bg: 'rgba(255, 255, 255, 0.05)' });
}function setup() {
createCanvas(400, 300, WEBGL);
startDrums('four on floor', 120);
}
function draw() {
background(14, 16, 26);
drawRhythm(-width / 2, -height / 2, width, height, 'circle'); // (0, 0) is the middle in WEBGL
}Related
startDrums()Starts a drum machine: a 16-step grid on the page that plays a preset, your own grid or one line of rhythm, and counts the bars for your sketch.drumPhaseHow far through the current bar the drum machine is, smoothly from 0 up to just below 1, as it is heard.drumStepWhich of the 16 steps of the bar the drum machine just played, from 0 to 15.startRecording()Records the canvas and the sound your sketch makes into one video file, and returns a promise that says whether the recording began.rhythmChanged()An event you define: it is called with the new line of rhythm every time the rhythm changes.