gpt4 book ai didi

javascript - (切换)函数未定义

转载 作者:行者123 更新时间:2023-11-28 03:15:10 28 4
gpt4 key购买 nike

我正在尝试使用按钮来打开和关闭 P5 振荡器的音频(因为据我了解,WebAudio API 内容现在需要在浏览器中进行交互才能播放音频)。

在设置之前我有:

var button;

在功能设置中我有:

button = createButton("audio on/off");
button.mousePressed(toggle);
button.position(32, 180);

在功能切换中我有:

if (!playing()) {
osc.start();
//playing = true; } else {
osc.stop();
//playing = false; }

它一直给我错误消息

Uncaught ReferenceError: toggle is not defined (sketch: line 45)"

为什么我总是收到这个信息?任何帮助将不胜感激。

完整的 sketch.js 代码是:

var button;
var stepX;
var stepY;
let osc;

function setup() {
createCanvas(displayWidth, displayHeight);
noCursor();
noStroke();
colorMode(HSB, width, height, 100);
reverb = new p5.Reverb();
delay = new p5.Delay();
osc = new p5.Oscillator();
osc.setType('sine');
reverb.process(osc, 5, 5);
osc.start();
// delay.process() accepts 4 parameters:
// source, delayTime, feedback, filter frequency
// play with these numbers!!
delay.process(osc, 0.6, 0.3, 1000);

// play the noise with an envelope,
// a series of fades ( time / value pairs )

var t = 10
let text = createP("s-p-e-c-t-r-u-m");

text.position(30, 30);
text.style("font-family", "monospace");
text.style("background-color", "#FFFFFF");
text.style("color", "#F20FD7");
text.style("font-size", "12pt");
text.style("padding", "10px");

let texty = createP("move mouse to control the <br>sine-wave theramin.<br><br>x axis = pitch 30-3000hz<br>y axis = volume quiet-loud)");

texty.position(32, 80);
//texty.style("background-color", "#FFFFFF");
texty.style("font-family", "monospace");
texty.style("color", "#FFFFFF");
texty.style("font-size", "10pt");


button = createButton("audio on/off");
button.mousePressed(toggle);
button.position(32, 180);

}

function draw() {
let pitch = map(mouseX, 0, width, 30, 3000);
let volume = map(mouseY, 0, height, 1, 0);
background(200);
osc.freq(pitch);
osc.amp(volume);

stepX = mouseX + 2;
stepY = mouseY + 2;

for (var gridY = 0; gridY < height; gridY += stepY) {
for(var gridX = 0; gridX < width; gridX += stepX) {
fill(gridX, height - gridY, 100);
rect(gridX, gridY, stepX, stepY);
}
}


function toggle() {
if (!playing) {
osc.start();
playing = true;
} else {
osc.stop();
playing = false;
}
}

}

最佳答案

toggledraw 范围内声明:

function draw() {

// [...]

function toggle() {

// [...]
}
}

将其移出draw,即可解决问题:

function draw() {

// [...]
}

function toggle() {

// [...]
}

参见示例:

var button;
var stepX;
var stepY;
let osc;

function setup() {
createCanvas(displayWidth, displayHeight);
noCursor();
noStroke();
colorMode(HSB, width, height, 100);
reverb = new p5.Reverb();
delay = new p5.Delay();
osc = new p5.Oscillator();
osc.setType('sine');
reverb.process(osc, 5, 5);
osc.start();
// delay.process() accepts 4 parameters:
// source, delayTime, feedback, filter frequency
// play with these numbers!!
delay.process(osc, 0.6, 0.3, 1000);

// play the noise with an envelope,
// a series of fades ( time / value pairs )

var t = 10
let text = createP("s-p-e-c-t-r-u-m");

text.position(30, 30);
text.style("font-family", "monospace");
text.style("background-color", "#FFFFFF");
text.style("color", "#F20FD7");
text.style("font-size", "12pt");
text.style("padding", "10px");

let texty = createP("move mouse to control the <br>sine-wave theramin.<br><br>x axis = pitch 30-3000hz<br>y axis = volume quiet-loud)");

texty.position(32, 80);
//texty.style("background-color", "#FFFFFF");
texty.style("font-family", "monospace");
texty.style("color", "#FFFFFF");
texty.style("font-size", "10pt");

button = createButton("audio on/off");
button.mousePressed(toggle);
button.position(32, 180);
}

function draw() {
let pitch = map(mouseX, 0, width, 30, 3000);
let volume = map(mouseY, 0, height, 1, 0);
background(200);
osc.freq(pitch);
osc.amp(volume);

stepX = mouseX + 2;
stepY = mouseY + 2;

for (var gridY = 0; gridY < height; gridY += stepY) {
for(var gridX = 0; gridX < width; gridX += stepX) {
fill(gridX, height - gridY, 100);
rect(gridX, gridY, stepX, stepY);
}
}
}

function toggle() {
if (!playing) {
osc.start();
playing = true;
} else {
osc.stop();
playing = false;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.sound.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.js"></script>

关于javascript - (切换)函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59709176/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com