简体   繁体   English

JavaScript 的“更新”function,类似于 p5.js?

[英]An “update” function for JavaScript, similar to p5.js?

I used p5.js in order to prototype some ideas, but since p5.js doesn't really draw stuff using actual HTML (as far as I understand, it uses a canvas), it doesn't include stuff like selectable text.我使用 p5.js 来制作一些想法的原型,但由于 p5.js 并没有真正使用实际的 HTML 绘制东西(据我所知,它使用画布),它不包括可选文本之类的东西。 The effect I'm trying to achieve is going to be constantly changing (The CSS of a DOM element will change constantly), is there a way that I am able to change CSS function every "frame", similar to the update function of p5.js? The effect I'm trying to achieve is going to be constantly changing (The CSS of a DOM element will change constantly), is there a way that I am able to change CSS function every "frame", similar to the update function of p5 .js?

Sticking with p5坚持p5

If you are comfortable using p5 you can stick with that.如果您对使用 p5 感到满意,则可以坚持使用。 Here is a demo from the docs on what the style() function does:这是文档中关于style() function 所做的演示:

https://p5js.org/reference/#/p5.Element/style https://p5js.org/reference/#/p5.Element/style

You'll notice it uses another p5 function, createDiv() to make an html div (that you can change the contents of) which is I think itself a wrapper for JavaScript's createElement .您会注意到它使用另一个 p5 function, createDiv()来制作 html div(您可以更改其内容),我认为它本身就是 JavaScript 的createElement的包装器。

https://p5js.org/reference/#/p5/createDiv https://p5js.org/reference/#/p5/createDiv

Its always worth searching the docs of the library you already use before you start down learning something new in my opinion.在我看来,在你开始学习新东西之前,搜索你已经使用的库的文档总是值得的。

If p5 is just absolutely not the way you want to go (maybe you aren't using 80% of its functions) there are two other routes I mentioned in my comment.如果 p5 绝对不是您想要 go 的方式(也许您没有使用其 80% 的功能),那么我在评论中还提到了另外两条路线。

requestAnimationFrame

requestAnimationFrame will sync with the screen's refresh rate and computer's processor to create the smoothest possible animations. requestAnimationFrame 将与屏幕的刷新率和计算机的处理器同步,以创建尽可能流畅的动画。 This is useful for lots of animation if you want it to appear super smooth.如果您希望它看起来超级平滑,这对于许多 animation 很有用。

function animationFunction(){

  // Put your logic here:

  // this makes the function repeat again
  requestAnimationFrame(animationFunction);
}

// this "kicks it off"
requestAnimationFrame(animationFunction);

setInterval

Set interval accepts a function as the first argument and a number of milliseconds as the second, so this is better for ones where you want to explicitly set how long between "frames".设置间隔接受 function 作为第一个参数和毫秒数作为第二个参数,因此这对于您想要明确设置“帧”之间的时间间隔的人来说更好。

let numberOfMillisecondsBetweenEachFrame = 300;

setInterval(function(){
  // Put your logic here
}, numberOfMillisecondsBetweenEachFrame);

Here is an example of both methods:以下是这两种方法的示例:

https://codepen.io/EightArmsHQ/pen/KKdNJRx https://codepen.io/EightArmsHQ/pen/KKdNJRx

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

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