如何向 HTML 页面公开Processing.js 函数?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是一个范围界定问题。尝试在窗口上显式设置重绘。
而不是:
尝试:
It could be a scoping issue. Try explicitly setting redraw on window.
Instead of:
Try:
也许您应该首先包含
processing-1.1.0.min.js
,因为此时processing
尚未在wolfram-automata.pjs
内定义。Maybe you should include
processing-1.1.0.min.js
first, sinceprocessing
is not defined insidewolfram-automata.pjs
at that point.