Most efficient way to draw particles in HTML5 on iPad 2
Asked Answered
L

3

9

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!

Lyontine answered 14/3, 2012 at 15:42 Comment(4)
rather than doubting the performance, why not actually try it? You might be surprised...Personalty
Tested on iPad2 for ya. It IS kind of jerky and slow. Also, you haven't set the viewport scaling properly because it's got a weird zoom thing happening.Marozas
Thank you very much! That confirms my worries, I guess. @Personalty I only got the iPad 1 here right now for testing which is of course suboptimal...Lyontine
I really think canvas would be the better solution. The bad framerate would be because it had to recalculate the same radial gradient every time, but if you cache it, (i.e. use sprites), and apply a darkening filter to the canvas at every step, that has the potential to be way faster than 3x115 imgs (I'm actually surprised that works at all).Prochora
L
1

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.

Leventis answered 14/8, 2012 at 19:50 Comment(0)
I
0

This should be built in the canvas. Check out EaselJS and this demo.

http://easeljs.com/ http://easeljs.com/demos/MusicVisualizer/index.html

Imbecility answered 28/3, 2012 at 14:13 Comment(0)
B
0

You could optimize performances a LOT by using WebGL(, which is supported on the iPad2.)... which is not supported for basic html pages on ios safari as stated Nison Maël...

For the time being you only have canvas as a solution. Which will still give you better performances...

(You can check this blog for more info: http://learningwebgl.com/blog/

With a little faith and time you'll be amazed!)

Bourassa answered 9/8, 2012 at 10:25 Comment(1)
I have read that iOS doesn't have WebGL except for ads. However, if you can use WebGL, it's definitely the way to go. I have wrote a particle library : arcanis.github.com/js.spark/testAbase

© 2022 - 2024 — McMap. All rights reserved.