我正在学习编码并使用PaperJS
.
我有一个更新总分的功能,并将其显示在页面顶部。
function updateScore(){
text.content = 'Circles Cleared: ' + total;
text.bringToFront();
}
Total 是我保存当前分数的变量。我正在努力在我的代码中的其他任何地方使用它/将其发布到,MongoDB
因为该变量似乎不是全局的?我已经paperscript
在 HTML 文件中内联了。
我尝试将总数附加到表单中,并通过POST
拨打电话提交表单,但由于未定义总数,它不断返回。
甚至在console.logging
完成POST
通话后总不会出现。
任何帮助表示赞赏,提前致谢。
您的主要问题是PaperScript
代码在与您的JavaScript
代码不同的范围内执行。
为了在两者之间共享变量,最简单的方法是Paper.js
直接使用in JavaScript
(参见文档)。
这是您描述的案例的简化工作示例(总计显示Paper.js
在画布中)。
// setup paperjs
paper.setup(document.getElementById('canvas'));
// init total variable
var total = 0;
// init total text item
var totalText = new paper.PointText({
point: paper.view.center,
justification: 'center'
});
displayTotal();
// listen to button click
$('button').click(function() {
// increment total variable
total += 100;
// display total
displayTotal();
});
function displayTotal() {
totalText.content = 'total = ' + total;
}
html,
body {
margin: 0;
overflow: hidden;
height: 100%;
}
canvas[resize] {
width: 100%;
height: 100%;
}
button {
position: fixed;
top: 15px;
left: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.11.5/paper-core.min.js"></script>
<canvas id="canvas" resize></canvas>
<button>increment total</button>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句