2013-02-08 51 views
0

我在跟蹤處理草圖中鼠標移動到Javascript時的移動問題。據我所知,程序運行良好,除了它不會激活mouseMoved,無論是作爲事件還是作爲邏輯操作。我也嘗試過pMousex!= mouseX,那也沒用。任何幫助?Processing.js:不會跟蹤mouseMoved()?

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); 
} 

回答

0

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

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

通常畫在說明鼠標處理程序不能正常工作,您通常需要根據鼠標位置設置您的狀態,然後調用redraw(),將代碼實際繪製到從draw繪製的屏幕上,而不是從您的事件中調用處理程序。