简体   繁体   English

从画布动态“卸载”Processing JS草图

[英]Dynamically “unload” a Processing JS sketch from canvas

I'm using some javascript to allow users to dynamically load a sketch on click to a canvas element using: 我正在使用一些javascript来允许用户使用以下命令动态加载素描到click元素:

Processing.loadSketchFromSources('canvas_id', ['sketch.pde']); Processing.loadSketchFromSources('canvas_id',['sketch.pde']);

If I call Processing.loadSketchFromSources(...) a second (or third...) time, it loads a second (or third...) .pde file onto the canvas, which is what I would expect. 如果我将Processing.loadSketchFromSources(...)调用第二个(或第三个......)时间,它会将第二个(或第三个......).pde文件加载到画布上,这正是我所期望的。

I'd like for the user to be able to click another link to load a different sketch, effectively unloading the previous one. 我希望用户能够单击另一个链接来加载不同的草图,有效地卸载前一个草图。 Is there a method I can call (or a technique I can use) to check if Processing has another sketch running, and if so, tell it to unload it first? 有没有我可以调用的方法(或者我可以使用的技术)来检查Processing是否有另一个草图运行,如果有,请告诉它先卸载它?

Is there some sort of Processing.unloadSketch() method I'm overlooking? 是否有某种Processing.unloadSketch()方法我忽略了? I could simply drop the canvas DOM object and recreate it, but that (1) seems like using a hammer when I need a needle, and (2) it results in a screen-flicker that I'd like to avoid. 我可以简单地删除画布DOM对象并重新创建它,但是当我需要针时,(1)似乎使用锤子,(2)它会导致我想避免的屏幕闪烁。

I'm no JS expert, but I've done my best to look through the processing.js source to see what other functions may exist, but I'm hitting a wall. 我不是JS专家,但我已经尽力查看processing.js源代码,看看其他功能可能存在,但我还是碰壁了。 I thought perhaps I could look at Processing.Sketches.length to see if something is loaded already, but simply pop'ing it off the array doesn't seem to work (didn't think it would). 我想也许我可以看看Processing.Sketches.length来查看是否已经加载了某些东西,但只是将它从阵列弹出似乎不起作用(不认为它会)。

I'm using ProcessingJS 1.3.6. 我正在使用ProcessingJS 1.3.6。

In case someone else comes looking for the solution, here's what I did that worked. 如果其他人来寻找解决方案,这就是我所做的工作。 Note that this was placed inside a closure (not included here for brevity) -- hence the this.launch = function() , blah blah blah... YMMV. 请注意,这是放在一个闭包内(为简洁起见,这里不包括) - 因此this.launch = function() ,等等等等等等...... YMMV。

/**
 * Launches a specific sketch. Assumes files are stored in
 * the ./sketches subdirectory, and your canvas is named g_sketch_canvas
 * @param {String} item The name of the file (no extension)
 * @param {Array} sketchlist Array of sketches to choose from
 * @returns true
 * @type Boolean
 */
this.launch = function (item, sketchlist) {
    var cvs = document.getElementById('g_sketch_canvas'),
        ctx = cvs.getContext('2d');
    if ($.inArray(item, sketchlist) !== -1) {
        // Unload the Processing script
        if (Processing.instances.length > 0) {
            // There should only be one, so no need to loop
            Processing.instances[0].exit();
            // If you may have more than one, then use this loop:
             for (i=0; i < Processing.instances.length; (i++)) {
            //  Processing.instances[i].exit();
            //}
        }
        // Clear the context
        ctx.setTransform(1, 0, 0, 1, 0, 0);
        ctx.clearRect(0, 0, cvs.width, cvs.height);
        // Now, load the new Processing script
        Processing.loadSketchFromSources(cvs, ['sketches/' + item + '.pde']);
    }
    return true;
};

I'm not familiar with Processing.js, but the example code from the site has this: 我不熟悉Processing.js,但该站点的示例代码包含:

var canvas = document.getElementById("canvas1");
// attaching the sketchProc function to the canvas
var p = new Processing(canvas, sketchProc);
// p.exit(); to detach it

So in your case, you'll want to keep a handle to the first instance when you create it: 因此,在您的情况下,您将需要在创建第一个实例时保留句柄:

var p1 = Processing.loadSketchFromSources('canvas_id', ['sketch.pde']);

When you're ready to "unload" and load a new sketch, I'm guessing (but don't know) that you'll need to clear the canvas yourself: 当你准备“卸载”并加载一个新草图时,我猜测(但不知道)你需要自己清理画布:

p1.exit();
var canvas = document.getElementById('canvas_id'); 
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
// Or context.fillRect(...) with white, or whatever clearing it means to you

Then, from the sound of things, you're free to attach another sketch: 然后,根据事物的声音,你可以自由地附上另一幅素描:

var p2 = Processing.loadSketchFromSources('canvas_id', ['sketch2.pde']);

Again, I'm not actually familiar with that library, but this appears straightforward from the documentation. 同样,我实际上并不熟悉该库,但这从文档中看起来很简单。

As of processing.js 1.4.8, Andrew's accepted answer (and the other answers I've found in here) do not seem to work anymore. 截至处理.js 1.4.8,安德鲁接受的答案(以及我在这里找到的其他答案)似乎不再起作用了。

This is what worked for me: 这对我有用:

    var pjs = Processing.getInstanceById('pjs');
    if (typeof pjs !== "undefined") {
      pjs.exit();
    }

    var canvas = document.getElementById('pjs')
    new Processing(canvas, scriptText);

where pjs is the id of the canvas element where the scrips is being run. 其中pjs是其中scrips正在运行画布元素的id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM