Get mp3 Michael Buble Pitbull MP3 search Gipsy Kings Nat King Cole All mp3 genres Coil Neil Young Suzy Solar Shakira Eva Cassidy Stevie Ray Vaughan Cobra Starship Useless Pickles » Blog Archive » Javascript Triangles (and real-time 3d)

Javascript Triangles (and real-time 3d)

Update: Now works in IE6 thanks to a hack. Just look for the <!–[if lt IE 7]> conditional comment in the source code for details

For some reason, I decided that I want to draw triangles in a browser… without Flash, the canvas tag, or Java applets. I also wanted it to be as fast as possible, so that rules out the obvious method of creating a thin DIV for every horizontal scanline of the triangle (too many DIVs). After 2 evenings of work, I present this proof of concept.

If you like it, please digg it!

23 Responses to “Javascript Triangles (and real-time 3d)”

  1. sajacy Says:

    I registered just to leave a congratulatory message, so…

    Awesome job, keep it up.

  2. jef Says:

    Excellent. I’m not so interested in the 3D part but the much-cheaper rendering of arbitrary triangles will be very useful.

    How about another routine to draw circles/ellipses by approximating the edges with triangles and filling in the interior with rectangles?

  3. UselessPickles Says:

    Thanks.

    Yeah, the 3D is pretty useless because it can’t randle much more than that simple object without dropping to unbearable framerates, but it was a good way to demonstrate how fast the triangle rendering is.

    Next time I have some free time, maybe I’ll work on a generalized ploygon routine then try ellipses.

  4. Fading Roses Says:

    True 3D rendering in JavaScript…

  5. spicyj Says:

    Any fancy way to do lines like this :P ?

  6. UselessPickles Says:

    I can’t think of any way to use this border trick for drawing lines. You’d have to make tons of DIVs to make a line, like this library does:
    http://www.walterzorn.com/jsgraphics/jsgraphics_e.htm

  7. dragonmage Says:

    Hi, this is pretty amazing, not plugin required for 3D!

    I can’t follow ur code too well (don’t know javascript), but have questions :)
    I’ll just ask one tho: how to the points like:
    { points: [0, 12, 1], color: new Vector(255, 64, 0) },
    { points: [1, 12, 2], color: new Vector(255, 64, 0) },
    { points: [2, 12, 3], color: new Vector(255, 64, 0) },

    get converted to triangles. I.e. to the first three make one triangle, the next three another etc?
    Or are there implicit shared vertices? I notice the example does not have a multiple of 3…

  8. UselessPickles Says:

    The three lines of code you are quoting define 3 triangles. Vertices are shared (notice how all three of those triangles use point number 12). The points property of each triangle is an array of indicies into the points array for the object.

  9. dragonmage Says:

    oh ok, I’ll see if I can understand teh code better - I was wondering if it could be used for, say a general triangle strip.

  10. dragonmage Says:

    Ah that’s cool, it fits with what I do for terrain models.

  11. Il blog di TixXio Says:

    3d rendering in Javascript…

    Si tratta solamente di un proof of concept che oltre a sembrare scarsamente utile in un’applicazione reale non offre grandi possibilitГ  di personalizzazione. Rimane comunque un lavoro originale interessante da guardare e che lascia pensare&#8230…

  12. BigZaphod Says:

    I noticed that the code seems to use innerHTML. In my experience, using DOM is actually faster - although YMMV. :-)

  13. UselessPickles Says:

    “In my experience, using DOM is actually faster”

    That’s been the opposite of my experience when adding a large number of items to the DOM tree (especially in IE). Here’s some speed comparisons between innerHTML and DOM methods:
    http://www.quirksmode.org/dom/innerhtml.html

    According to the findings of that experiment, I am using the fastest method. My initial implementation did use DOM methods, and I noticed quite a difference when I switched to using innerHTML (I didn’t record any measurments, but the speed-up was noticable).

  14. BigZaphod Says:

    Thanks for the innerHTML link. :-) I may have run into some other bottleneck when I was using innerHTML in my application. IE in particular seemed to be re-rendering images whenever I inserted into a particular DIV using innerHTML. Switching to DOM made that go away and so everything seemed faster. Perhaps it was just a side effect due to the circumstances of how the page was being rendered and the structures being inserted and surrounding it and such.

    I love your app. It’s inspiring!

  15. anty.at » 3D Objekte mit JavaScript rendern Says:

    […] Gestern habe ich mir noch überlegt, ob es eigentlich in einer angemessenen Geschwindigkeit möglich wäre eine 3D Engine mit JavaScript zu realisieren. Heute sehe ich auf uselesspickles.com, dass Jemand die gleiche Idee hatte wie ich und sich an die Umsetzung gemacht hat! […]

  16. Stefan Van Reeth Says:

    Great idea!!! Sadly it’s only usable for a very limited set of shapes, but hey, we can’t have all. When is the shading and texture mapping coming ;) ?

  17. UselessPickles Says:

    Technically, it’s usable for any shape made of triangles… it’s just slower with more triangles. Flat shading is already there (look again if you didn’t notice it), but any other type of shading or texture mapping would be impossible with this technique. Those require per-pixel rendering, which would be extremely slow with javascript/DOM/CSS.

  18. Javascript 3D engine at iPhone Tweak Zone-Tweaks-Cracks-Software-Wallpaper-Info Says:

    […] References: Javascript Triangles (and real-time 3d) - Link Rotating 3D Polyhedron Demo - Link Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. […]

  19. 3pointD.com Says:

    Render 3D Objects in JavaScript…

    Check out this demo of a 3D rendering engine concept that the Useless Pickles blog hacked up in JavaScript. It works by manipulating the borders of page elements so that they appear as triangles — which means it could conceivably be used to rend…

  20. iicbiit Says:

    its genious :) just the way i like the things done
    i don’t really need this concept. i just admire it :)
    bravo

  21. Dalek Bell » Blog Archive » blender2js - blender to javascript 3d model conversion Says:

    […] Via BlenderNation, a script for exporting blender 3d models to javascript.В  This reminds me of Samuel Johnson’s quip: “Sir, a woman’s preaching is like a dog’s walking on his hind legs. It is not done well; but you are surprised to find it done at all.”В  The conversion is approximate and the animation slow for non-trivial models, but it is impressive to see plain javascript doing this. Kudos to useless pickles for the core triangle-drawing trickery. […]

  22. close Says:

    Great work. Your concept is explained by MS developer Erik Meijer in a video on Channel 9 (video 1, about 0:11:00). He forgot your name, but remembers the url!

  23. cycnus Says:

    Your cool project was mentioned by Erik Meijer (of Microsoft Research) in a video about Volta, his new project.
    The video is available on channel 9:
    http://channel9.msdn.com/ShowPost.aspx?PostID=324106
    Erick Meijer’s home page is at:
    http://research.microsoft.com/~emeijer/

    Renaud

Leave a Reply

You must be logged in to post a comment.