Blowing up HTML5 video and mapping it into 3D space

I’ve been doing a bit of experimenting with the Canvas and Video tags in HTML5 lately, and found some cool features hiding in plain sight. One of those is the Canvas.drawImage() api call. Here is the description on the draft site.

3.10 Images

To draw images onto the canvas, the drawImage method can be used.
This method can be invoked with three different sets of arguments:
  • drawImage(image, dx, dy)
  • drawImage(image, dx, dy, dw, dh)
  • drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)
Each of those three can take either an HTMLImageElement, an HTMLCanvasElement, or an HTMLVideoElement for the image argument.

The api lets you take the contents of specific HTML elements and draw them into a canvas, and the 3rd element in that list is just begging to be abused. Copying video into a canvas element means opening up the ability to manipulate or process video frames at runtime. Two concepts instantly came to mind that seemed like fun to try and figure out, here they are below.

Blowing apart fragments of video

Click around the video frame to blow up that part of the video, the exploded pieces will continue to play the video inside them. After a while they retract back to their original place. One feature I didn’t have time to figure out was adding depth to the explosion, so pieces that are closest to ground zero fly up into the air as they sail outward. With full shadow effects this could look really cool.

3D Video

This demo in particular runs really well inside webkit based browsers, but not so much in Firefox. Firefox doesn’t appear to have any hardware acceleration for Ogg decoding so I had to drop the video size in half in order to run at acceptable framerates. Even still, Firefox chokes pretty badly on my Macbook Pro.

*Update* – I’ve changed the ogg video to be 640 x 360, prepare to see firefox weep

Lessons learned

There’s a couple hints I found out along the way that are good to know if you want to play around with drawing video. First, you need a bit of hackish code to get this to work effeciently and it flows like this.

[Video playing] -> [Draw Video onto Canvas 1] -> [Draw fragments of Canvas 1 onto Canvas 2]

Don’t ask me why, but copying pixel data out of a video tag is expensive, so expensive that drawing it into a temporary canvas, and then drawing pieces of that temp canvas onto a final canvas is faster then just referencing the video tag repeatedly within the same loop. That’s why you’ll see 2 Canvases in the source code for the demos. I’m sure there’s a technical reason for this duplication process, but it’s a lazy reason.

Secondly, don’t try copying individual pixels around. You can still see the remnants of my first code attempt inside the explosion demo with getPixel() and setPixel(). This turned out to be horribly slow and completely unnecessary. Canvas.drawImage() + matrix transforms on the canvas space is far more efficient then handcrafted pixel pushing. On the other hand, pixel manipulation allows you to do things like runtime chroma keying, get ready for a new wave of “clippy” style videos with full transparency popping over websites to help you out.

Lastly, I’m learning very quickly that not all browsers are created equal when it comes to performance, it’s a crapshoot when it comes to heavy video+image manipulation. Safari and Chrome work well with h.264, Firefox slogs along with Ogg Theora, and Opera is somewhere in the middle.

305 thoughts on “Blowing up HTML5 video and mapping it into 3D space”

  1. Being able to blow up video files is definitely why we all need to switch over. This is like the cure for cancer. We can all happily blow our video files from now on with our new html5 overlords. Thank goodness me!

  2. Firefox 4b5pre runs even the 3D demo at a flawless speed on an intel pentium dual core running vindows vista.

  3. Also, hardware acceleration is turned off (like is is by default) and antialiasing is used (unlike chrome which does not use antialiasing)

  4. Pingback: Html5 Solution
  5. Pingback: 30?HTML5????
  6. I’m having problems with a much simpler video example.
    I have two versions of a bouncing video. In one, the video element is moved directly.
    http://faculty.purchase.edu/jeanine.meyer/html5workshop/videobounce1.html

    In the other, the video is drawn on the canvas and shapes drawn on top to serve as a mask.
    http://faculty.purchase.edu/jeanine.meyer/html5workshop/videobounce3.html

    The first works in Firefox and Chrome.
    The second works in Firefox. The video does not appear in Chrome.

    Also, neither work with a tag, and they don’t appear to work with your doctype either.

    Any help most appreciated.

  7. Would you happen to know if this technique could be used to create an interactive video for the ipad?
    We want to create a tutorial video with “hot spots” where a user can click on a section of the video an an action would take place…

  8. I’m running an ubuntu laptop I bought in 05 with 128mg of RAM. Demos went flawless in the developers version of chrome. Gecko is light years behind webkit in canvas performance, but if any box would get ugly in chrome, it’s mine. I’d say you did an amazing job of conserving resources. It ain’t your fault Firefox is underfunded compared to chrome. Brilliant proof of concept. Bookmarked your sight you crafty mind.

  9. Pingback: 50???HTML5????
  10. Pingback: ??
  11. PERO SI ESA BASURA LA HAGO IGUALITA EN FLASH 8 Y MAS RAPIDA Y MENOS NPESADA_
    CUAL ES LA VENTAJA DE ESSA RIDICULES DE HTML5_
    ]YA LOE STUVE PROBANDO Y ES UN PROBLEMA PARA TODO
    CREO QUE LOS QUE LO ESTAN APOYANDO SON GAYS TRISTES HAHAAHH

    EL FLASH NUNCA MORIRA

  12. Works with Opera without any problem (full support, no crashes, very smooth even on my slow computer).

  13. The change time for open standards for example html 5 is simply too slow, and never everybody switches into them exactly the same. In the current rate it’ll be another ten years before its up to date with expensive, with that time we’d have managed to move on.

  14. Has anyone got these examples to work on an iphone yet? I have money waiting to pay someone if they have.

    Contact me here or at thomasseidl “at” devitek. com

Comments are closed.