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

发布于 2024-10-22 08:31:58 字数 1139 浏览 3 评论 0原文

我有一个 redraw() 函数,旨在停止当前的 draw() 循环,从 DOM 检索新的初始化值,然后恢复绘制,就好像这些值是最初的值一样。

但是onchange中的“redraw()”看不到Processing.js的redraw()函数,因此更改输入字段中的值没有任何效果。如果“redraw()”更改为“alert('changed!')”,则会弹出窗口显示“changed!”,因此我们知道问题不是 onchange,而是无法从处理.js 文件。

Wolfram-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

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();
}

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.

But "redraw()" in onchange can't see the Processing.js redraw() function, so changing the value in the input field has no effect. 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.

wolfram-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

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

汐鸠 2024-10-29 08:31:58

这可能是一个范围界定问题。尝试在窗口上显式设置重绘。

而不是:

function redraw() { ...

尝试:

window.redraw = function() { ...

It could be a scoping issue. Try explicitly setting redraw on window.

Instead of:

function redraw() { ...

Try:

window.redraw = function() { ...
梦旅人picnic 2024-10-29 08:31:58

也许您应该首先包含 processing-1.1.0.min.js,因为此时 processing 尚未在 wolfram-automata.pjs 内定义。

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

Maybe you should include processing-1.1.0.min.js first, since processing is not defined inside wolfram-automata.pjs at that point.

<script src="processing-1.1.0.min.js"></script>
<canvas data-processing-sources="wolfram-automata.pjs"></canvas>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文