I'm having trouble loading a processing js script via a jquery ajax call. The sketch runs fine in开发者_JAVA技巧 its own file but when i load it into a div it doesn't work. Do I need to init the sketch somehow? I've searched the Processing js docs but can't find it. Thanks
The ugly test script
<a href="#" onclick="$.ajax({url: '/test', context: document.body, success: function(text){ $('#container').html(text); }});">Test</a>
Where '/test' would contain
<script type="application/processing" data-processing-target="pjs">
// code here
</script>
<canvas width="760" height="500" id="pjs"></canvas>
If you're fetching the source from a file, create a new Processing instance for the canvas you want it loaded into, instead:
Processing.loadSketchFromSources($('#pjs'), ['mysketch.pde']);
The second argument is an array because a sketch can consist of multiple source files.
If it's dynamic code, simply build a new Processing instance:
new Processing($('#pjs'), "/* full sketch code goes here. */");
Your post seems to suggest some level of control with respect to what the ajax call to "/test" generates, so I would have it simply generate the sketch code only, without the script and canvas tag, and have the ajax callback function generate the canvas element on the page, followed by a new Processing($('#canvasid'), callbackdata) call so that Processing.js loads the sketch code into the canvas you just made.
精彩评论