Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to draw particles in HTML5 on iPad 2

I'm trying to create moving lights with trails for an HTML5 website/app targeted at iPad 2. I wonder what the best way to do this is and whether using HTML5 is viable at all. I chose HTML5 because it's easier and cheaper to develop and deploy than native iOS apps with Objective C. Of course if it turns out that HTML5 simply doesn't offer enough performance I might have to swallow the bitter pill.

Anyway to give you an impression what I'm talking about, this is what I got so far:

screenshot http://devdali.no-ip.org/mathias/test-lights/screenshots/1.jpg

Or you can see it in action here (only works in webkit based browsers).

At first I tried using HTML5 canvas and drawing radial gradients as particles in similar manner you see above. It worked but the framerate was horrible even on my desktop computer!

So after a bit of reading I found out that CSS3 transforms may be hardware accelerated, so I build the version you see above. Every "particle" is a 64x64 png image. For each light there is the "head" light (one img) followed by a trail consisting of 115 img elements. Each img element is transformed using "translate3d" (as well as scale and rotation). Also the opacity of each element is adjusted dynamically.

Doing it this way provided much better framerates on my computer, but I doubt the iPad 2 will handle it.

I'd be grateful if anyone could give me some hints on how to improve the performance of this in general and considering the target platform.

Thanks for any help in advance!

like image 695
Bunkerbewohner Avatar asked Mar 14 '12 15:03

Bunkerbewohner


1 Answers

If you accept small changes to the effect, some other procedure may work fast:

  • Instead of drawing the light's trails by the means of many particles, just draw the lights in their current positions in a Canvas element.

  • You can then darken the whole image at the beginning of a frame by filling a black rectangle with a very low opacity on top. This way the trails fade into dark, but would not alter their color like they do now.

  • The amount of drawing operations however will reduce vastly. The most costly operation would be filling the fading rectangle for every frame.

like image 175
dronus Avatar answered Sep 21 '22 14:09

dronus