简体   繁体   English

如何将Processing.js函数公开到HTML页面?

[英]How do I expose Processing.js functions to the HTML page?

I've got a redraw() function that is designed to halt the current draw() loop, retrieve new initialization values from the DOM, and resume drawing as if the values were those in the first place. 我有一个redraw()函数,该函数旨在停止当前的draw()循环,从DOM中检索新的初始化值,并像开始时那样重新开始绘制。

But "redraw()" in onchange can't see the Processing.js redraw() function, so changing the value in the input field has no effect. 但是onchange中的“ redraw()”看不到Processing.js redraw()函数,因此更改输入字段中的值无效。 If "redraw()" is changed to "alert('changed!')", a popup displays "changed!", so we know the problem isn't the onchange, but an inability to import the redraw() function from the Processing.js file. 如果将“ redraw()”更改为“ alert('changed!')”,则弹出窗口将显示“ changed!”,因此我们知道问题不在于onchange,而是无法从窗口中导入redraw()函数。 Processing.js文件。

wolfram-automata.html 钨,automata.html

<p>Rule: <input onchange="redraw()" id="rule" value="110" size="4" /></p>
<canvas id="sketch" data-processing-sources="wolfram-automata.pjs"></canvas>
<script src="processing-1.1.0.min.js"></script>

wolfram-automata.pjs 钨,automata.pjs

processing.setup = function () {
    size(bounds, steps);
    background(white);

    rule = parseInt(document.getElementById("rule").value, 10);

    currentState = initState(bounds);
    rule = bits(rule);
}

processing.draw = function () {
    drawState(currentState, currentStep);
    currentState = step(currentState, rule);
    currentStep++;

    if (currentStep >= steps) { noLoop(); }
}

function redraw() {
    println("Redrawing");

    noLoop();

    currentState = [];
    currentStep;

    processing.setup();

    loop();
}

It could be a scoping issue. 这可能是一个范围界定的问题。 Try explicitly setting redraw on window. 尝试在窗口上显式设置重绘。

Instead of: 代替:

function redraw() { ...

Try: 尝试:

window.redraw = function() { ...

Maybe you should include processing-1.1.0.min.js first, since processing is not defined inside wolfram-automata.pjs at that point. 也许您应该首先包含processing-1.1.0.min.js ,因为此时尚未在wolfram-automata.pjs中定义processing

<script src="processing-1.1.0.min.js"></script>
<canvas data-processing-sources="wolfram-automata.pjs"></canvas>

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

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