More and more Javascript

November 4th 2009

Seems like I'm still hooked to Javascript. For the last few months, on my spare time I've been toying more and more with it, creating little pieces that will serve as personal benchmarks for browser performance improvements.

For the next few links I recommend using a WebKit based browser otherwise your browser may crash.

The first idea I wanted to try was creating a canvas using checkboxes as pixels. Then display animations with it. Similar to textmode renderers.



I posted the link over twitter and minutes later Aaron was sending me a drawing done with the checkboxes by Valdean Klump. On firefox, the experiment didn't worked correctly and it just created a grid where anyone could pixelate in. Because this, I created drawing tool out of it. I though it would be fun if I could also generate links on-the-fly so people could share their checkbox based drawings easily. This is how it ended up:



(Press Shift for the eraser tool)

Some days after, Joa Ebert ported a Strange Attractor code to Silverlight to compare performance with Flash. I knew that Javascript <canvas> was going to be way slower than these but I was curious how much.



I believe the code can get some performance optimisations for the platform but my interests were to compare exactly the same code. 7 F/S seemed a good start.

At this point I felt it was about time to go back to toying with 3D worlds. Many people were working on engines using <canvas> as the renderer. But I though I could try using <svg> as renderer instead.

Differences between <canvas> and <svg> are pretty much the same as BitmapData and Graphics. With <canvas>, performance decreases exponentially by the size of the viewport. It's easier to fill the pixels of a triangle at 10x10 than 1000x1000. With <svg> the bottle neck is the amount of vectors being drawn. Because they don't get rastered on a bitmap the nodes stay in memory and exponentially gets slower. However, viewport size isn't a much of a problem here, which ticks my fullscreen action requirement.

How do you draw with <svg>?

Good question. A <svg> is basically a XML. Every frame you fill it with nodes for each polygon with their properties. Then, on the next frame, you delete all the nodes and repeat the process. Sounds crazy, but it works. Here is the proof.



As you've probably ovserved, a benefit of <svg> that you can disable antialiasing (which you can't with <canvas>). This thing alone makes <svg> outperform <canvas>.

Now, don't get the wrong impression. At the moment I don't even know if texturing is possible with <svg>, I haven't investigated that yet. Something tells me that, if possible, it'll go horribly slow.

While working on all these I came up with some things I wanted to do that flat colors were good enough. One of these was a QR code in 3D. Now that I own an Android phone I keep seeing more and more QR codes. The shape of a QR code seemed like a little city and extruding it was something that excited my mind (yes, I'm weird).



I've learn quite a bit just for doing this piece. Inkscape for vectorising, Blender for extruding and colorising, Python for exporting from Blender and refactored my engine once again. Performance wise, its like going back to the Actionscript 2 days. But, hopefully, WebGL will arrive soon and I'll be able to play with more polygons. Until then, I can create a bunch of things just with these.

Feel free to study the sources for each piece if you are interested. If you find something wrong or that can be improved, please, let me know.
5 comments written so far...

I likes me some Javascript too. I've been learning a bit of WebGL at http://learningwebgl.com/blog/
It's pretty exciting to have hardware acceleration in the browser, though it is early days yet, and the API is kinda verbose.
November 4th 2009
bigfish
very nice. also played with checkboxes few months ago but interface widgets rendering is very slow even in webkit: http://the389.com/works/drops/
November 4th 2009
andrey
Ha! Nice! It works fast here (Chrome/Linux).

The checkbox things are from back in July. Didn't found time to post it here until now. I should clean them up a bit now. I'll lurk into your code to see how you solved the problems I found :)
November 4th 2009
mr.doob
hey, these are great! so im especially interested in the QR code demo. (the code works with my phones reader! i like urls in QRcode format too). so anyway. i really dig the 3D extrusion effect. i noticed you can see the depth from any direction even if the code is view straight on. is that from the way the 3D object was rendered? or is that achieve via camera pov? thanx for the inspiration!
March 4th 2010
xero
With the mouse you move the camera X, Z... You know, as usual...
March 4th 2010
mr.doob

Have your say!

Name:

Website:

Comment:

Some of the projects that I worked on.



Some of the HTML5 and Actionscript experiments I've done.