W3cubDocs

/DOM

StereoPannerNode.pan

The pan property of the StereoPannerNode interface is an a-rate AudioParam representing the amount of panning to apply. The value can range between -1 (full left pan) and 1 (full right pan).

Syntax

var audioCtx = new AudioContext();
var panNode = audioCtx.createStereoPanner();
panNode.pan.value = -0.5;

Returned value

An a-rate AudioParam containing the panning to apply.

Note: Though the AudioParam returned is read-only, the value it represents is not.

Example

In our StereoPannerNode example (see source code) HTML we have a simple <audio> element along with a slider <input> to increase and decrease pan value. In the JavaScript we create a MediaElementAudioSourceNode and a StereoPannerNode, and connect the two together using the connect() method. We then use an oninput event handler to change the value of the StereoPannerNode.pan parameter and update the pan value display when the slider is moved.

Moving the slider left and right while the music is playing pans the music across to the left and right speakers of the output, respectively.

var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var myAudio = document.querySelector('audio');

var panControl = document.querySelector('.panning-control');
var panValue = document.querySelector('.panning-value');

pre.innerHTML = myScript.innerHTML;

// Create a MediaElementAudioSourceNode
// Feed the HTMLMediaElement into it
var source = audioCtx.createMediaElementSource(myAudio);

// Create a stereo panner
var panNode = audioCtx.createStereoPanner();

// Event handler function to increase panning to the right and left
// when the slider is moved

panControl.oninput = function() {
  panNode.pan.value = panControl.value;
  panValue.innerHTML = panControl.value;
}

// connect the AudioBufferSourceNode to the gainNode
// and the gainNode to the destination, so we can play the
// music and adjust the panning using the controls
source.connect(panNode);
panNode.connect(audioCtx.destination);

Specifications

Specification Status Comment
Web Audio API
The definition of 'pan' in that specification.
Working Draft

Browser compatibility

Feature Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 41 Yes 37 No 28 No
Feature Android webview Chrome for Android Edge mobile Firefox for Android IE mobile Opera Android iOS Safari
Basic support 41 41 Yes 37 No 28 No

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/StereoPannerNode/pan