我有一个运行2个处理草图的网页,我想按这个问题中的建议调用Processing.instances.exit():Dynamically "unload" a Processing JS sketch from canvas
但是,当我调用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
谢谢
发布于 2013-08-29 22:36:05
正如您从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
发布于 2013-08-30 01:06:44
如果其他人发现了这个问题:不要使用Processing.instances -我们从来没有写过它是可以访问的。在画布id中传递一个Processing.getInstanceById()函数,并返回运行在它上的草图。
使用.getInstanceById()获取实例,然后调用.exit()。
https://stackoverflow.com/questions/18518324
复制相似问题