简体   繁体   English

HTML5 Canvas-用掉落的金片制作动画背景

[英]HTML5 Canvas - Making an animated background with falling gold flakes

I'm working on a website which calls for an animated background. 我正在一个需要动画背景的网站上工作。 I am trying to imitate falling gold flakes. 我试图模仿掉落的金片。 Initially we tried using an animated gif, but it's way too heavy and sluggish. 最初,我们尝试使用动画gif,但是它太笨重了。

This is my first time working with Canvas, I would definitely like someone with more experience to give me some input on this. 这是我第一次与Canvas合作,我当然希望有更多经验的人能给我一些建议。

I found some code as a starting point and repurposed it for my needs. 我找到了一些代码作为起点,并根据自己的需要对其进行了重新设计。 Here's where I am currently at: http://codepen.io/slickdev/pen/LqKai 这是我当前所在的位置: http : //codepen.io/slickdev/pen/LqKai

To-do : I'd like to slow down the speed of the falling particles, and alter their shape from being a perfect circle to look more like falling gold flakes. 待办事项 :我想减慢下落的粒子的速度,并将其形状从完美的圆形更改为看起来更像下落的金片。 If I can get it to appear more realistic it would be ideal. 如果我可以使它看起来更逼真,那将是理想的。 I also need to focus on performance. 我还需要关注性能。 Are there any ways to optimize this to run more efficiently? 有什么方法可以优化它以使其更有效地运行?

Are there any drawbacks to this approach, in terms of accessibility / cross-browser compatibility? 就可访问性/跨浏览器兼容性而言,此方法是否有任何缺点? I've done some research and it seems that it's widely supported. 我已经进行了一些研究,并且似乎得到了广泛的支持。

Your help would be greatly appreciated! 您的帮助将不胜感激!

I don't know if you've already had a chance to finish this before now, but here goes.... 我不知道您之前是否已经有机会完成此操作,但是这里...

To change the speed of the falling particles you'll want to edit the @vx and @vy on lines 57 and 58 of that forked Pen of yours, something similar to: 要更改下落粒子的速度,您需要在您的那支分支笔的第57和58行上编辑@vx@vy ,类似于:

@vx = range(0,1)+8*xpos-5
@vy = 0.2*@r

I found that altering the DOP (and Y value to compensate) gave something a little more 'believable' too: 我发现更改DOP(和Y值进行补偿)也可以提供一些“可信”的东西:

@dop = 0.001*range(1,5)
@y = range(-100,h-@r2)

With regards to drawing more 'realistic' snowflakes, I personally prefer seeing shapes closer to "circles", since the actual flakes themselves are harder to see in real life. 关于绘制更多“逼真的”雪花,我个人更喜欢看到更接近“圆形”的形状,因为实际的薄片本身在现实生活中很难看到。 But if you're keen to work on your original idea I can point you to HTML5 canvas fractals . 但是,如果您热衷于最初的想法,我可以为您指出HTML5画布的分形

A good example of what to look into if you're looking for something simple are Pentaflake fractals . Pentaflake分形就是一个很好的例子,它可以用来寻找简单的东西。 If you really want to blow people's minds then I would suggest looking into the source code of this HTML canvas fractal application, here . 如果您真的想引起人们的注意,那么我建议您在此处研究此HTML canvas fractal应用程序的源代码。

I hope this helps :) 我希望这有帮助 :)

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

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