delayed start / ending of processing.js sketches?

edited November 2014 in JavaScript Mode

I'm making an advent calendar, and realized it would be a better UX to have each sketch open up on the current page. I could do this via iframes, but I was wondering if I could also do it programmatically in the page?

Skimming the processing.js code, it seems like maybe I could call init() later? Is there a safe way of tearing down a sketch?

Or is it better to just iframe it all?

Answers

  • edited November 2014 Answer ✓

    You can have multiple canvases on one page and address each one individually, e.g.

    <canvas id="Canvas1" data-processing-target="" width="1024" height="768">
      <p>Your browser does not support the canvas tag.</p>
    </canvas>
    
    <script type="text/javascript">
        var myCanvas1 = document.getElementById('Canvas1');
        new Processing(myCanvas1, mySketch1);
     </script>
    

    mySketch1 is a variable your PJS-translated sketch is assigned to. For this, you can use the ProcessingJS helper, which translates your pde to a js code:

    http://processingjs.org/tools/processing-helper.html

    In the script block above, you can then use the JavaScript Date() function to enable the respective sketch.

  • And regarding your third question: No, there is no safe way as long as the code can be inspected client-side. Then it can also be manipulated in the browser DOM. You would need to have some server-side script (e.g. PHP) or even a database that checks the date (the server-side date) and delivers the sketch code only if the date matches.

  • Thanks for the feedback!

    By safe, I didn' mean "keep secret"; my sketches are reasonably processor intensive, and I would NOT want to run more than one at once!

  • I was thinking of a "real" advent calendar having windows one has to open day for day. To save CPU, course you could stop the sketches that were already opened. The perfect scenario would be calendars which save the open/closed state for each window for every user individually (so user1 could have opened 1,2,3,4,5 and user2 1,3,4). That, however, would involve session data.

  • You can have only one sketch, running a routine on demand.

Sign In or Register to comment.