简体   繁体   English

动态加载 p5.js 脚本

[英]Dynamically Load p5.js Script

I am trying to dynamically load a p5.js script (Processing for the browser), but, no matter what I seem to do, I can't get the script to display its visuals in a webpage.我正在尝试动态加载 p5.js 脚本(浏览器的处理),但是,无论我似乎做什么,我都无法让脚本在网页中显示其视觉效果。 Under normal circumstances, I'd load the p5 script in the index.html, using the standard <script src="sketch.js" type="text/javascript"></script> .在正常情况下,我会使用标准的<script src="sketch.js" type="text/javascript"></script>在 index.html 中加载 p5 脚本。 When I attempt using a jquery script to dynamically append the script, however, Chrome throws "Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience".然而,当我尝试使用 jquery 脚本动态附加脚本时,Chrome 抛出“主线程上的同步 XMLHttpRequest 已被弃用,因为它对最终用户的体验有不利影响”。 I've also tried using我也试过使用

$('body').load("aSeparateFileWithTheScriptInIt.html", function() {
    console.log("load html was performed");
    $.getScript("sketch.js", function(data, textStatus, jqxhr) {
      console.log("load sketch was performed");
    });
  });

which displays/prints absolutely nothing.它绝对不显示/打印任何内容。


Strangely, this code returns that the script ran successfully.奇怪的是,这段代码返回脚本运行成功。

$.getScript( "sketch.js" )
  .done(function( script, textStatus ) {
    console.log( textStatus );
  })
  .fail(function( jqxhr, settings, exception ) {
    $( "div.log" ).text( "Triggered ajaxError handler." );
});

I could use help figuring out how I should properly use jquery to dynamically load the sketch.js file.我可以使用帮助来确定我应该如何正确使用 jquery 来动态加载sketch.js 文件。

not exactly what you wanted but i guess reason is the same.不完全是你想要的,但我想原因是一样的。 I was load p5js in runtime by adding new script with async="true" to the header.我通过向标头添加带有async="true"新脚本在运行时加载 p5js。

and in my case i didn't see any global functions which supposed to be there with p5.js (in particular loadImage , createCanvas ).就我而言,我没有看到任何应该与 p5.js 一起存在的全局函数(特别是loadImagecreateCanvas )。

though i was able to see p5 object.虽然我能够看到 p5 对象。

so in my case i had to manually call new p5();所以就我而言,我不得不手动调用new p5(); to solve problem.来解决问题。

and that is it, everything is there!就是这样,一切都在那里!

hope it's going to save someone couple hours :)希望它能为某人节省几个小时:)

EDIT 14/05/2019编辑 14/05/2019

I end up getting rid of p5 library completely, and rewriting methods i need by myself.我最终完全摆脱了p5库,并自己重写了我需要的方法。

Mostly because p5 allows only one canvas on the page, and because it is very slow comparing to working directly with canvas and canvas context .主要是因为p5只允许页面上有一个画布,并且与直接使用canvascanvas context相比它非常慢

If you have to load your sketch asynchronously (to be started long after P5.js has loaded and looked for an existing sketch itself), you will have to add the following lines in order to start the sketch in "global mode"如果您必须异步加载您的草图(在 P5.js 加载并查找现有草图后很长一段时间内启动),您必须添加以下几行以便在“全局模式”下启动草图

window.setup = setup;
window.draw  = draw;
new p5();

window is the global variable which P5 fills with all its functions in order to provide the "global mode" environment. window是全局变量,P5 用它的所有函数填充它以提供“全局模式”环境。

setup and draw are those user-defined functions, P5 will invoke in order to run your sketch. setupdraw是那些用户定义的函数,P5 将调用以运行您的草图。 They must be globally available before new p5() is run - either because they have been defined as such or because you made them global explicitly (as shown in my example).运行new p5()之前,它们必须是全局可用的 - 要么是因为它们已被定义为这样,要么是因为您明确地将它们设为全局(如我的示例所示)。

The latter allows you to define these two functions in a local environment (eg an IIFE) together with any other methods you need (and which you do not want to pollute the global namespace with) and still get rid of the nasty target object prefix (eg p5.xxx ) in front of all P5 methods and properties.后者允许您在本地环境(例如 IIFE)中定义这两个函数以及您需要的任何其他方法(并且您不想污染全局命名空间),并且仍然摆脱讨厌的目标对象前缀(例如p5.xxx ) 在所有 P5 方法和属性之前。

This "solution" is more of a "hack" rather than a real "answer" (as it is undocumented and exploits behavior which might change in the future) but it helped me loading external scripts into an already loaded web page running P5 without having to tell those sketches that they will be executed in "local mode" (as a sketch should never need to know the mode it will be run in)这个“解决方案”更像是一个“黑客”而不是一个真正的“答案”(因为它是未记录的并且利用了将来可能会改变的行为)但它帮助我将外部脚本加载到运行 P5 的已经加载的网页中,无需告诉那些草图它们将在“本地模式”下执行(因为草图永远不需要知道它将运行的模式)

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

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