如何改变滑块的鼠标上下触发振荡器的音调,并在使用Javascript和Web音频API的鼠标up事件后保留状态
How to change pitch of mousedown/up triggered oscillator with slider and have it retain state after mouseup event with Javascript and Web Audio API
代码中的注释应该是自解释的。
<div id="pad2"></div>
<input id="oscPitch" type="range" min="5" max="500" step="1" value="50"/>
<p>Pitch</p>
<script>
context = new webkitAudioContext();
var pad2 = document.getElementById("pad2");
pad2.onmousedown= function () {
oscillator = context.createOscillator(), // Creates the oscillator
oscillator.type = 2; // 4 oscillator types // Pitch
oscillator.connect(context.destination); // Connects it to output
oscillator.noteOn(0);
document.getElementById('oscPitch').addEventListener('change', function() {
oscillator.frequency.value = this.value;
});
};
// If you remove the mouse up event below and change the pitch slider it works fine. But I want to have the pitch slider retain it's state before & after the mouse up event. Itried storing the getElementId of the slider element to a variable and playing with it but I couldn't chisel it out.
pad2.onmouseup = function () {
oscillator.disconnect();
};
</script>
编辑。我更接近于这个:
<input id="oscPitch" type="range" min="5" max="500" step="1" value="90"/>
<p>Pitch</p>
<script>
context = new webkitAudioContext();
var pad2 = document.getElementById("pad2");
var pitchState = document.getElementById('oscPitch').value;
pad2.onmousedown= function () {
oscillator = context.createOscillator(), // Creates the oscillator
oscillator.type = 2;
oscillator.frequency.value = pitchState;
oscillator.connect(context.destination); // Connects it to output
oscillator.noteOn(0);
};
pad2.onmouseup = function () {
oscillator.disconnect();
};
</script>
明白了。
<input id="oscPitch" type="range" min="5" max="500" step="1" value="90"/>
<p>Pitch</p>
<script>
context = new webkitAudioContext();
var pad2 = document.getElementById("pad2");
pad2.onmousedown= function () {
var pitchState = document.getElementById('oscPitch').value;
oscillator = context.createOscillator(), // Creates the oscillator
oscillator.type = 2;
oscillator.frequency.value = pitchState;
oscillator.connect(context.destination); // Connects it to output
oscillator.noteOn(0);
};
pad2.onmouseup = function () {
oscillator.disconnect();
};
</script>
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- EaseJS拖放;放下(动画CC)电影剪辑的鼠标坐标
- 当鼠标悬停在文本中的单词上时显示警报
- JsFiddle上的鼠标事件不起作用
- 在Web应用程序中使用Highcharts javascript
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- WebDriverException:tinyMCE未在selenium Web driver java中定义
- Selenium Web驱动程序和javascript
- Windows形成web浏览器控件和Javascript更改的DOM
- 如何在鼠标悬停时在另一个图像上滑动图像.
- 鼠标悬停事件影响列表中所有行中的按钮,而不仅仅是特定按钮
- 如何将javascript事件从web浏览器wpf控件发送到wpf的c#代码
- Node js 捕获键盘按下和鼠标移动(不在 Web 浏览器上)
- 如何在按住鼠标左键的情况下获取鼠标位置以在web中选择和拖动文本
- Web:动画云-溢出和鼠标中点击
- IE的vlc web插件鼠标事件
- 悬停的Web按钮,鼠标悬停时显示下拉阴影
- SVG路径元素未触发JavaFX8 Web视图中的鼠标事件
- 优化触摸屏的 Web 界面(处理不需要的鼠标拖动等)
- 如何改变滑块的鼠标上下触发振荡器的音调,并在使用Javascript和Web音频API的鼠标up事件后保留状态