2013-08-29 41 views
1

我已經運行2個處理草圖,我想打電話給Processing.instances [0] .exit()的建議在這個問題上的網頁:Dynamically "unload" a Processing JS sketch from canvasProcessing.instances返回null

但是,當我打電話Processing.instances它返回null,並且我沒有在JavaScript控制檯上發生錯誤 - 也Processing.instances.length返回(0)。

下面的JavaScript代碼:

document.onkeydown = function(e) { // or document.onkeypress 
e = e || window.event; 
if (e.keyCode == 115 || e.keyCode == 83) { //press "s" or "S" 
    alert(Processing.instances.length); 
} 
}; 

這裏爲網站的網址:http://culturadigital.cc/nuevaweb

感謝

+0

也在這裏:var varvases = getElementsByClassName(document,'processingsketch'); \t如果(畫布){ \t \t對於(j = 0;Ĵ fartagaintuxedo

回答

1

正如你從PJS論壇想通了,Processing.instances是前途未卜。這個怎麼樣:

document.onkeydown = function(e) { // or document.onkeypress 
    e = e || window.event; 
    if (e.keyCode == 115 || e.keyCode == 83) { //press "s" or "S" 
    var canvases = document.getElementsByClassName("processingsketch"); 
      window.alert(canvases.length); 
    if (canvases) { 
     var ps = Array(); 
     for (j=0;j<canvases.length;j++) { 
     ps[j]=Processing.getInstanceById(canvases[j].getAttribute('id')); 
     } 
     for (j=0;j<canvases.length;j++) { 
     window.alert("ps " + ps[j]); 
     window.alert(canvases[j].getAttribute('id')); 
     if(ps[j]){ps[j].exit();} //per fartagaintuxedo's comment below: to avoid second error because once it exits then there is no longer an instance in that canvas 
     canvases[j].width = canvases[j].width; //to obliterate the canvas instead of just freezing it 
     } 
    } 
    } 
}; 

僅供參考,可能有更好的方法來清除這裏的畫布:How to clear the canvas for redrawing

+0

顯然'退出'應該使草圖不可見..這裏發生的是它凍結..奇怪..然後第二次我按「s」我再次得到同樣的錯誤 – fartagaintuxedo

+0

但我想那就是它必須做的..只是凍結,爲什麼不。 – fartagaintuxedo

+0

只是一個註釋,爲了避免第二個錯誤*,因爲一旦退出,那麼在該畫布中不再有一個實例*只需添加'if(ps [j]){ps [j] .exit();}' – fartagaintuxedo

1

如果任何人發現這樣一個問題:不使用Processing.instances - 我們從來就寫信給可以訪問。有一個Processing.getInstanceById()函數可以在你的畫布ID中傳遞,並找回正在其上運行的草圖。

.getInstanceById()爲例,請撥打.exit()