Javascript 我如何在鼠标按下时仅更改一次颜色(阻止其循环)?
我正在做另一个练习教程,我的程序应该在你按住鼠标的时候改变一下眼睛的颜色。但当我试着按住它时,它会不断地在随机颜色中循环,而不是仅仅改变一次。本质上,我只想在用户每次按下或按住鼠标时更改一次眼睛颜色Javascript 我如何在鼠标按下时仅更改一次颜色(阻止其循环)?,javascript,p5.js,Javascript,P5.js,我正在做另一个练习教程,我的程序应该在你按住鼠标的时候改变一下眼睛的颜色。但当我试着按住它时,它会不断地在随机颜色中循环,而不是仅仅改变一次。本质上,我只想在用户每次按下或按住鼠标时更改一次眼睛颜色 var u; var l; var a; var mods = []; var x; var y; var count; var r, g, b; function setup() { createCanvas(windowWidth, windowHeight); u = 100;
var u;
var l;
var a;
var mods = [];
var x;
var y;
var count;
var r, g, b;
function setup() {
createCanvas(windowWidth, windowHeight);
u = 100;
l = 40;
var highCount = height/80;
var wideCount = width/80;
count = int(highCount * wideCount);
var index = 0;
for (var xc = 0; xc < wideCount; xc++) {
for (var yc = 0; yc < highCount; yc++) {
mods[index++] = new Module(int(xc)*u,int(yc)*u);
}
}
}
function draw() {
//background black if mouse is pressed or held down
if (mouseIsPressed) {
background(0);
r = random(255);
g = random(255);
b = random(255);
//background white if mouse is not pressed or held down
} else {
background(255);
}
//drawing the eyeballs
noStroke();
translate(15, 15);
for (var i = 0; i <= count; i++) {
mods[i].update();
mods[i].eyeball();
mods[i].pupil();
}
}
function Module(_x, _y) {
this.x = _x;
this.y = _y;
this.a = 0;
}
Module.prototype.update = function() {
this.a = atan2(mouseY-this.y, mouseX-this.x);
}
Module.prototype.eyeball = function() {
push();
translate(this.x, this.y);
fill(255);
ellipse(0, 0, l, l);
pop();
}
//need to keep pupil and iris together for movement
Module.prototype.pupil = function() {
push();
translate(this.x, this.y);
rotate(this.a);
fill(r, g, b);
ellipse(8, 0, l/2, l/2);
fill(0);
ellipse(8, 0, l/4, l/4);
pop();
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
var-u;
变种l;
var a;
var mods=[];
var x;
变量y;
var计数;
var r,g,b;
函数设置(){
createCanvas(窗口宽度、窗口高度);
u=100;
l=40;
var highCount=高度/80;
var wideCount=宽度/80;
计数=整数(高计数*宽计数);
var指数=0;
对于(var xc=0;xc
在您的代码中,它不断更改r
、g
和b
您更希望使用event
另外,我在setup()
中声明了初始随机颜色,这样它就不会抱怨了:p5.js说:color()应该是数字
var-u;
变种l;
var a;
var mods=[];
var x;
变量y;
var计数;
var r,g,b;
函数设置(){
createCanvas(窗口宽度、窗口高度);
//原色
r=随机(255);
g=随机(255);
b=随机(255);
u=100;
l=40;
var highCount=高度/80;
var wideCount=宽度/80;
计数=整数(高计数*宽计数);
var指数=0;
对于(var xc=0;xc 对于(var i=0;非常感谢您的解释,我不知道draw函数会连续执行其中的代码,再次感谢!现在这更有意义了:)