0

当移植到 Javascript 时,我在处理草图中跟踪鼠标的移动时遇到问题。据我所知,该程序运行良好,只是它不会激活 mouseMoved,无论是作为事件还是作为逻辑操作。我也试过 pMousex != mouseX 也没有用。有什么帮助吗?

color[] palette = new color[5];
color c1;
color c2;
color c3;
color c4;
color c5;
color bgColor;

int swarmSize;
int xVariance;
int yVariance;
int maxSize;
int maxOpacity;
int maxStroke;

//Non-definables for swarm gen
int sideMod;
int sSize;
int opacity;
int stroke;

void setup() {
size(600, 400);

c1= #BF2633;
c2= #A6242F;
c3= #D9CEAD;
c4= #C0B18F;
c5= #011C26; 

bgColor = color(23, 76, 79);

palette[0] = c1;
palette[1] = c2;
palette[2] = c3;
palette[3] = c4;
palette[4] = c5;

swarmSize = 1;
xVariance = 60;
yVariance = 60;
maxSize = 30;
maxOpacity = 255;
maxStroke = 4;

}

void draw() { //tried tracking pMouse != mouse here, no dice
}

void drawSwarm() {
  for (int i = 0; i < swarmSize; i++)
{
 if (random(1, 10) < 5) {
  sideMod = -1;
} 
else {
  sideMod = 1;
}
stroke = int(random(1, maxStroke));
sSize = int(random(1, maxSize));
opacity = int(random(0, maxOpacity));

strokeWeight(stroke);
stroke(palette[int(random(1, 5))], opacity);
fill(palette[int(random(1, 5))], opacity);

// strokeWeight(int(random(1, 7)));
ellipse(mouseX + int(random(1, xVariance)) * sideMod, mouseY+ int(random(1, yVariance)), sSize, sSize);

} }

void mouseMoved() { //Doesn't work in processing.js
drawSwarm();
}

void keyPressed() {
background(bgColor);
} 
4

1 回答 1

0

下面的代码工作得很好(见http://jsfiddle.net/qPpRQ/

int x,y;
void draw() {
  point(x,y);
}
void mouseMoved() {
  x = mouseX;
  y = mouseY;
  redraw();
}

通常,鼠标处理程序中的绘制指令不能很好地工作,您通常希望根据鼠标位置设置您的状态,然后调用 redraw(),并调用实际将内容绘制到屏幕的代码绘制,而不是从您的事件处理程序。

于 2013-02-08T13:45:21.303 回答