xxxxxxxxxx
36
/*
* @name Load and Play Sound
* @description Load sound during preload(). Play a sound when canvas is clicked.
* <br><br><em><span class="small"> To run this example locally, you will need the
* <a href="http://p5js.org/reference/#/libraries/p5.sound">p5.sound library</a>
* a sound file, and a running <a href="https://github.com/processing/p5.js/wiki/Local-server">local server</a>.</span></em>
*/
let ball = {};
let soundFile;
function preload() {
soundFormats('mp3', 'ogg');
soundFile = loadSound('assets/jackyboy.mp3');
}
function setup() {
createCanvas(710, 100);
}
function draw() {
background(0);
ball.x = constrain(mouseX, 0, width);
ellipse(ball.x, height / 2, 100, 100);
}
function mousePressed() {
// map the ball's x location to a panning degree
// between -1.0 (left) and 1.0 (right)
let panning = map(ball.x, 0, width, -1.0, 1.0);
soundFile.pan(panning);
soundFile.play();
}