javascript函数包含两个元素和web音频api

javascript function with two elements and web audio api

本文关键字:web 音频 api 元素 函数 包含两 javascript      更新时间:2023-09-26

我想制作一个函数来控制web音频api中的两个参数,这些参数会根据html5上的输入元素而变化。我不知道如何使它按需要工作。

这是输入滑块的代码:

<div>
  <input type="range" min="0" max="100" value="0" oninput="BrownVol(this);" />BrownVol</input>
</div>

以及功能:

BrownVol = function(element) {
  var fraction = parseInt(element.value) / parseInt(element.max);
  this.gain_3.gain.value = fraction * fraction;
};

我想在输入中添加另一个参数,比如:

<div>
  <input type="range" min="0" max="100" value="0" oninput="BrownVol(this, gain_3);" />BrownVol</input>
</div>

并替换函数中的第二个参数

BrownVol = function(element, type) {
  var fraction = parseInt(element.value) / parseInt(element.max);
  this.type.gain.value = fraction * fraction;
};

如果我能定义像"gain_3.gain"这样的参数,那就更好了…

我试过了,但没用。

我不知道怎么做,有人能帮我吗?谢谢

在第二个函数中,您传入type

BrownVol = function(element, type) {

但你试着这样称呼它

this.type.gain.value = fraction * fraction;

type不被识别为其属性。如果它是this的属性,并且名称本身不是可变的,则不应将其作为参数传入。如果有时它是gain_3,有时是gain_4,则应该将其作为一个值,而不是键,例如gain['value'] = 3,那么传入gain将获得所需的值。