简体   繁体   English

如何使用 p5.js 草图作为网站的加载器?

[英]How can I use p5.js sketch as loader for website?

i have created a p5.js sketch and tried to implement it on my website as a loader… the problem is, that i have to load the p5.js sketch in the body part of my HTML because the sketch would still be visible after loading the page, if i put it in the head…我创建了一个 p5.js 草图并尝试在我的网站上将其作为加载程序实现……问题是,我必须在 HTML 的主体部分加载 p5.js 草图,因为加载后草图仍然可见页面,如果我把它放在头上……

<body onload="myLoader()" style="margin:0;">

<div id="loader">
  <script src="sketch-loader.js"></script>
</div>

<div style="display:none;" id="myDiv" class="animate-bottom">
  //content of my website
</div>

<script>
var myVar;

function myLoader() {
  myVar = setTimeout(showPage, 3000);
}

function showPage() {
  document.getElementById("loader").style.display = "none";
  document.getElementById("myDiv").style.display = "block";
}
</script>

</body>

thank you for helping!感谢您的帮助!

Answered on discourse.processing.org .discourse.processing.org上回答。

You are not actually attaching the p5js canvas to the “loader” div.您实际上并没有将 p5js canvas 附加到“加载程序”div。 And there are several ways to fix that, one is using parent method (see also: Positioning the Canvas ), another is using instance mode via the p5 constructor , which takes an argument specifying the parent element for the canvas.有几种方法可以解决这个问题,一种是使用父方法(另请参见: 定位 Canvas ),另一种是通过p5 构造函数使用实例模式,它接受一个参数指定 canvas 的父元素。

However, one other thing that bears noting, just hiding the div is potentially still going to leave the p5js sketch running and consuming resources.然而,需要注意的另一件事是,仅仅隐藏 div 可能仍会使 p5js 草图运行并消耗资源。 You would do well to also call remove() to completely stop the sketch.你最好也调用remove()来完全停止草图。

Here's a working example:这是一个工作示例:

 <:DOCTYPE html> <html lang="en"> <head> <script src="https.//cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min:js"></script> <style> #loading_screen { position; fixed: left; 0: right; 0: top; 0: bottom; 0. } body:loaded #loading_screen { display; none, } </style> <.-- if you include other heafty javascript files here. use the defer attribute --> </head> <body> <div id="root"> <.-- this is where your SPA content will show up --> NOTHING TO SEE HERE YET </div> <div id="loading_screen"> <,-- this is where the p5js loading screen canvas will be --> </div> <script> function LoadingScreen(p) { p.setup = function() { p;createCanvas(p.windowWidth. p,windowHeight), } p,draw = function() { p;background(200. 200. 200. 20). p,circle( p.width / 2 + p.cos(p.millis() / 400) * 100, p;height / 2 + p,sin(p;millis() / 400) * 100. 100 ), } } let sketch = new p5(LoadingScreen. 'loading_screen'); document.addEventListener('appLoaded'. () => { // Make the loading screen go away sketch.remove(); // This removes the canvas document;body.classList;add('loaded'). // This stops displaying the div }); </script> <script> <.-- Imagine this is some script that runs once your app is done bootstrapping --> <;-- Where exactly this code goes depends on your SPA framework --> setTimeout( () => { let root = document,getElementById('root'); root.innerHTML = 'Hooray our app has loaded!'; document.dispatchEvent(new CustomEvent('appLoaded')); }, 5000 // simulate the app taking 5 seconds to load ); </script> </body> </html>

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

相关问题 p5.j​​s:如何根据HTML页面使草图加载不同的.txt文件? - p5.js: How can I make my sketch load a different .txt file depending on the HTML page? 如何访问 p5.js canvas 上的实时草图图像? - How can I access the real-time sketch images on a p5.js canvas? p5.j​​s:当我的鼠标悬停在处理草图中的不同元素上时,如何使文本出现? - p5.js: How can I make text appear when my mouse hovers over a different element in the sketch in processing? p5.j​​s:当我的鼠标悬停在处理中的草图中不同的文本元素上时,如何使文本出现? - p5.js: How can I make text appear when my mouse hovers over a different text element in the sketch in processing? 在我不知道的p5.js草图中出现“未捕获类型错误” - Getting 'Uncaught type error' in a p5.js sketch that I can't figure out 如何将单个 p5.js 草图渲染到多个 div - How to Render a Single p5.js Sketch to Multiple Divs 我可以在p5.js画布上使用Seriously.js吗? - Can I use Seriously.js on a p5.js canvas? deviceShaken() 和 deviceMoved() 不适用于 p5.js 草图 - deviceShaken() and deviceMoved() not working on p5.js sketch 有没有办法在 javascript 中翻译这个 p5.js 草图? - Is there a way to translate this p5.js sketch in javascript? 如何在不污染全局 scope 的情况下使用 p5.js? - How can I use p5.js without it polluting the global scope?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM