2012-02-08 96 views
0

我需要找到一種方法,可以在HTML文檔內動態更改處理腳本的來源。processing.js更改腳本源

這是嵌入腳本的正常工作:

<script type='application/processing' src='sketch.txt' id="applet"> 
    </script> 

現在我試圖改變源:

$('#applet').attr('src', 'sketch2.txt'); 
    alert($('#applet').attr('src')); 

警報顯示,來源變更爲「sketch2.txt」,但該小程序仍然保持不變。我想我需要以某種方式刷新腳本。

非常感謝您的幫助!

回答

0

我相信您必須手動將每個proc附加到畫布上,而不是僅僅更改源文件。這工作在這裏:

function first_call(processing) { 
    processing.size(300,300); 
    processing.background(100); 
    var called = false; 
    processing.draw = function() { 
    if (!called) { processing.println("called #1"); called = true; } 
    } 
} 

function second_call(processing) { 
    processing.size(400,400); 
    processing.background(200); 
    var called = false; 
    processing.draw = function() { 
    if (!called) { processing.println("called #2"); called = true; } 
    } 
} 

var canvas = document.getElementById('canvas1'); 
var processingInstance = new Processing(canvas, first_call); 
var processingInstance = new Processing(canvas, second_call);