Before anything else: I hate websites that play any kind of sound, it's very annoying. I'm writing this answer just as as a curiosity, and as a complement to OP's answer.
It's clear by both OP's question and answer that they are referring to a audio file. However, since D3 means data driven documents, we can use data to play sound in other, creative ways.
In this answer I'll use the AudioContext interface. A good tutorial can be found here: https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Using_Web_Audio_API
In this demo I'm creating the data array for the circles, setting a property called frequency
, which is a frequency (in Hertz) between 500 and 2500 Hz:
const data = d3.range(30).map(d => ({
frequency: 500 + Math.random() * 2000,
//etc...
}));
Then, when you hover over the circles, a brief sound with that frequency is played:
const audioCtx = new(window.AudioContext || window.webkitAudioContext);
circles.on("mouseover", (d, i, n) => {
const osc = audioCtx.createOscillator();
osc.frequency.value = d.frequency;
Here is the (annoying!) demo:
const data = d3.range(30).map(d => ({
frequency: 500 + Math.random() * 2000,
radius: ~~(Math.random() * 20),
x: Math.random() * 300,
y: Math.random() * 150
}));
const svg = d3.select("svg");
const color = d3.scaleOrdinal(d3.schemeCategory10);
const audioCtx = new(window.AudioContext || window.webkitAudioContext);
const circles = svg.selectAll(null)
.data(data)
.enter()
.append("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", d => d.radius)
.style("fill", (_, i) => color(i));
circles.on("mouseover", (d, i, n) => {
d3.select(n[i]).raise().attr("r", d => d.radius + 2);
const osc = audioCtx.createOscillator();
const gain = audioCtx.createGain();
osc.type = "sine";
osc.frequency.value = d.frequency;
osc.connect(gain);
gain.connect(audioCtx.destination)
osc.start(0);
d3.timeout(() => {
osc.stop()
}, 100)
}).on("mouseout", (d, i, n) => {
d3.select(n[i]).attr("r", d => d.radius)
})
<script src="https://d3js.org/d3.v5.min.js"></script>
<svg></svg>