问题描述
我希望在按下按钮打开动画后加载 paperjs
但是如果在页面加载后加载纸张,则看起来稿件不起作用。
I wanted paperjs
to load after a button is pressed to turn on animations but it seems the paperscript doesn't work if paper is loaded after page load.
如果您注释掉 setTimeout
并取消注释直接 $。getScript
- paperjs将触发警报('hi')
。我不明白。
If you comment out the setTimeout
and uncomment the direct $.getScript
- paperjs will fire the alert('hi')
. I don't get it.
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function () {
var paperUrl = 'http://cdnjs.cloudflare.com/ajax/libs/paper.js/0.22/paper.js';
$("#jq").text("jQuery is now loaded.")
var lateLoad = function() {
$.getScript(paperUrl, function() {
$("#pp").text("Paperjs is now loaded.");
});
};
//setTimeout(lateLoad, 100);
$.getScript(paperUrl, function() {
$("#pp").text("Paperjs is now loaded.");
});
});
</script>
</head>
<body>
<script type="text/paperscript" canvas="myCanvas">
$('body').css({"background-color": "#999"});
alert('hi!');
</script>
<p id="jq">jQuery NOT loaded yet.</p>
<p id="pp">Paperjs NOT loaded yet.</p>
</body>
</html>
我在Windows 7 x64上使用Chrome 23.0.1271.97 m。有人知道这里发生了什么吗?
I'm using Chrome 23.0.1271.97 m on Windows 7 x64. Anybody have an idea what's going on here?
推荐答案
我想我也想出了一个解决方法 - paper.PaperScript.load()
或详细说明:
I think I figured out a workaround as well - paper.PaperScript.load()
or to elaborate:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function () {
$("#jq").text("jQuery is now loaded.")
var paperUrl = 'http://cdnjs.cloudflare.com/ajax/libs/paper.js/0.22/paper.js';
var lateLoad = function() {
$.getScript(paper_url, function() {
$("#pp").text("Paperjs is now loaded.");
paper.PaperScript.load(); // <-- The fix!
});
};
setTimeout(lateLoad, 1000);
//$.getScript(paperUrl, function() {
// $("#pp").text("Paperjs is now loaded.");
//});
});
</script>
</head>
<body>
<script type="text/paperscript" canvas="myCanvas">
$('body').css({"background-color": "#999"});
alert('hi!');
</script>
<p id="jq">jQuery NOT loaded yet.</p>
<p id="pp">Paperjs NOT loaded yet.</p>
</body>
</html>
这会导致稿件扫描所有的文件。我在在谷歌搜索github回购中的paperscript时。虽然它仍然无法解释为什么纸张在动态加载时不会自行执行load()。
That causes paperscript to scan for all the paperscripts. I found it at https://github.com/paperjs/paper.js/blob/master/src/core/PaperScript.js#L270 while googling for "paperscript" in the github repo. Though it still doesn't explain why paper doesn't do the load() on its own when dynamically loaded.
编辑 - 我理解什么地方出了错。它与因为paperjs没有检查窗口加载的事件是否已经被触发,即 document.readyState ===complete
。我向paperjs
EDIT - I understand what went wrong. It's related to https://github.com/jquery/jquery/blob/master/src/core.js#L768 because paperjs doesn't check if the window loaded event had already fired i.e document.readyState === "complete"
. I submitted a pull request to paperjs https://github.com/paperjs/paper.js/pull/156
这篇关于动态加载脚本会更改其行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!