views:

1084

answers:

3

In my quest to learn javascript (which seems to be my latest source of questions for SO these days) i've found this API for drawing http://www.c-point.com/javascript_vector_draw.htm

Meanwhile I've been making some experiments with graphics, which tend to be heavy on the amount of stuff to draw. This guy is drawing with divs, every square part of the image is a div. You can easily check it by inspecting his example at the page.

He goes to making divs 1px/1px to draw a pixel

Now what i have to ask is the following:

Is this the best method? It seems quite heavy when a drawing is more elaborate. Which other methods would you sugest?

This http://stackoverflow.com/questions/96486/javascript-drawing-library already has a couple of links to libs so no need to put any here.

I've seen here in SO other questions regarding drawing. I'm just wondering to which point drawing with divs isn't awful!

+2  A: 

for vector drawing, libraries like Raphael provide a consistent API over SVG and VML.

for raster drawing, you can use <canvas> for browsers that support canvas.

for IE, you would default to DIVS or have your drawing API use silverlight if it's available. Note that for efficiency, divs should not be 1px by 1px but rather be as long as necessary for the shape you are drawing. 20 1-pixel divs of the same color should be 1 div that is 20 pixels wide. Generally you won't get very interactive with the div approach, because the browser you're targetting (IE) has slow DOM access

EDIT: The linked library does do the div-optimizations.

for HTML-only solutions (no SVG/VML/canvas) you use css and custom border-widths:

Jimmy
A: 

Not, it is the worst method, elaborated before there was SVG or canvas elements... It would be very heavy on memory and very slow. An amusing, interesting hack, but not really usable in the real world.

Beside the libraries mentioned in the other thread, relying on canvas/SVG/VML, I saw games made with sprites, ie. some images positioned absolutely. There was also an awesome hack coding a first-person 3D shooting game in 5k of JavaScript, using generation of XBM images on the fly. But support for this image format have been removed from recent versions of Windows... :-(

Come to think of it, you can also make images using "data:image/png;base64" protocol, but the encoding would be quite slow, and IE browsers would be out.

PhiLho
mahemoff
Thanks for the info, but currently IE8 is still beta and not widely used. I meant current IE versions (6, 7). I would write similar stuff for some CSS tricks, hoping info will become obsolete one day. :-)
PhiLho
A: 
Eugene Lazutkin