1

我有一个运行 2 个处理草图的网页,我想按照这个问题中的建议调用 Processing.instances[0].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

谢谢

4

2 回答 2

1

If anyone else finds this question: don't use Processing.instances - we never wrote it to be accessible. There is a Processing.getInstanceById() function that you pass in your canvas id, and get back the sketch that's running on it.

Get your instance with .getInstanceById(), then call .exit() on that.

于 2013-08-30T01:06:44.423 回答
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
      }
    }
  }
};

作为参考,这里可能有更好的方法来清除画布:如何清除画布以进行重绘

于 2013-08-29T22:36:05.250 回答