Today’s Exercise:

P5 Sliders

A slider is a user interface element that lets us move the position of a slider to get a number between a range. The following code will add a slider to a sketch that allows us to select a number from 0 to 100 (with a default at the start of 50). Each time the value is changed we will display the value of the slider in the console window. Note we write to the console window just so we can see it is working, in the future, we will use the slider or similar elements to control properties of our sounds (for instance, volume, playback speed, etc). Check it out on the P5 Editor.

let slider;

function setup() {
  createCanvas(400, 400);
  slider = createSlider(0, 100,50);
  slider.position(100,260);
  slider.input(sliderChanged);
}

function sliderChanged() {
  console.log(slider.value());
}

function draw() {
  background(220);
}

Making a Circle act like a button

We covered an additional user interface topic that was brought to our attention, what if we want to make a circle behave like a button? The following is an example of how to make a circle become a user interface element with the ability to accept a click. Check it out on the P5 Editor:

let mouseWithinCircle=false;

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  mouseWithinCircle=dist(100,100,mouseX,mouseY)<75/2;
  if (mouseWithinCircle) {
    fill(100,40,70);
  } else {
    fill(0,200,50);
  }
  circle(100,100,75);
}

function mousePressed() {
  if (mouseWithinCircle) {
    alert("Clicked");
  } 
}

Tone.JS Player Object - Additional Parameters and Methods

Changing the Volume:

player.volume.value=-10;

Changing the speed that the Sample is played: