Blowing up HTML5 video and mapping it into 3D space

By Sean Christmann | Posted April 20th, 2010 | Html, Javascript

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.

Reader Comments (187) Leave a Comment

  1. Jae Xavier | June 16, 2010 at 4:03 pm | permalink

    This is so awesome. 3D space will be apart of the Web3.0 age.

  2. wespai | June 17, 2010 at 12:37 am | permalink

    great plugin I love it

  3. Jake | July 14, 2010 at 10:24 am | permalink

    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!

  4. david peppiatt | July 15, 2010 at 5:23 am | permalink

    fantastic,but what is its pupose

  5. Mark | August 27, 2010 at 4:32 pm | permalink

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

  6. Mark | August 27, 2010 at 4:36 pm | permalink

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

  7. oyun haberleri | September 13, 2010 at 10:11 am | permalink

    thanks for sharing. great news

  8. Lava Kumar | September 20, 2010 at 1:10 am | permalink

    can this be done with the videos playing through embed(embed tag) source?

  9. sai | October 3, 2010 at 9:49 pm | permalink

    wow awesome! html5 is really rocks the 3d effects :>

  10. Gagege | November 3, 2010 at 8:07 pm | permalink

    Flawless on IE 9 Beta.
    Very cool demo!

  11. ???? » Blog Archive » ???????30?HTML5???? | November 24, 2010 at 3:17 am | permalink

    [...] ????30???????????HTML5? Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D?????? ???HTML 5??Canvas ?Video [...]

  12. HTML5 Video Destruction « Itony.tk | November 30, 2010 at 5:17 am | permalink

    [...] //A } // ]]> Block based destruction of HTML5 video, best viewed in webkit based browsers. Return to Craftymind Article Due to popular request, the ogg video is now 640 x 360, don’t hold me responsible for [...]

  13. [...] Blowing up HTML5 video and mapping it into 3D space [...]

  14. [...] Blowing up HTML5 video and mapping it into 3D space [...]

  15. HTML5 Video — ?? ? ?? ? | December 8, 2010 at 10:30 am | permalink

    [...] ??????????. ? ????????? ???????? ??? ????? ???????? (1, 2, 3, 4, 5, 6) ?????? ??, ??? ?? ??????????? ????? ?? [...]

  16. [...] This is a site that allows you to click on certain videos and see how it blows up in front of your very eyes. Plus, the site will teach users on how to create such blowing up videos. Learn how to make a blowing up effect in videos with HTML5. [...]

  17. [...] 3. Blowing up HTML5 video and mapping it into 3D space [...]

  18. Html5 Solution | December 26, 2010 at 12:29 pm | permalink

    [...] 3. Blowing up HTML5 video [...]

  19. Thomas Kumlehn | December 26, 2010 at 2:58 pm | permalink

    I tried it with my webkit based Safari on iPad iOS 4.2. I also tried Skyfire.
    None of this works on the iPad, sadly, but true.

  20. Thomas Kumlehn | December 26, 2010 at 2:59 pm | permalink

    If this would work on iOS, we could watch 3-D videos in true colors.

  21. 30?HTML5???? | December 30, 2010 at 9:16 pm | permalink

    [...] Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D??? [...]

  22. ????? | January 12, 2011 at 6:34 am | permalink

    ??? ??? ???

  23. Jeanine | January 12, 2011 at 11:16 am | permalink

    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.

  24. mike | January 14, 2011 at 11:19 am | permalink

    please give me!

  25. [...] Demo |  Website:http://www.craftymind.com/2010/04/20/blowing-up-html5-video-and-mapping-it-into-3d-space/ [...]

  26. [...] Link: Blowing up HTML5 video and mapping it into 3D space [...]

  27. [...] 25. Blowing up HTML5 video [...]

  28. Matt | January 27, 2011 at 5:32 pm | permalink

    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…

  29. [...] 6 Jogos em HTML 5 HTML 5 e o fim do Flash Efeitos com HTML 5 [...]

  30. 5 awesome HTML5 demos | February 7, 2011 at 8:58 pm | permalink

    [...] not even mentionning how gorgeous the player is or how cool the full-window functionality is.3. Blowing up HTML5 videoThis will probably not only blow a video in your browser, it will also blow your mind. It’s [...]

  31. [...] 8. Blowing Up HTML5 Video [...]

  32. 30?HTML5???? - Dream step | February 16, 2011 at 12:15 am | permalink

    [...] Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D??? [...]

  33. [...] Blowing Up HTML5 Video [...]

  34. abkeya | February 20, 2011 at 4:05 pm | permalink

    Thats one of the awesome ones HTML5 work presentations.

  35. 50?html5????? - ?? - ????/????/???? | February 20, 2011 at 6:23 pm | permalink

    [...] Blowing Up HTML5 Video [...]

  36. [...] Blowing Up HTML5 Video This tutorial is about a blowing up HTML5 video and mapping it into 3D space. Check it out: [...]

  37. [...] 10. Coding A HTML 5 Layout From Scratch [...]

  38. [...] Blowing Up HTML5 Video This tutorial is about a blowing up HTML5 video and mapping it into 3D space. Check it out: [...]

  39. Patrick | March 4, 2011 at 3:35 pm | permalink

    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.

  40. yanshuhao | March 16, 2011 at 8:25 am | permalink

    ?

  41. ????????30?HTML5???? « ?? | March 21, 2011 at 2:48 am | permalink

    [...] ????? brainy ????? html5&css3 Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D?????????HTML 5??Canvas ?Video [...]

  42. akhmad fathur | April 1, 2011 at 12:44 am | permalink

    Veri nice, we are want develov our web with flatform HTML 5

  43. Barbara | April 7, 2011 at 12:06 pm | permalink

    very useful tips about HTML5, love it ^_^

  44. ??????30?HTML5???? | ?? | April 7, 2011 at 5:53 pm | permalink

    [...] P6 C# |# G! o. ^# ]0 ?# b + y4 A& X( A+ s Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D?????????HTML 5??Canvas ?Video [...]

  45. [...] Blowing up HTML5 video and mapping it into 3D space [...]

  46. [??]???????30?HTML5???? | Xiaoshao | April 8, 2011 at 7:18 pm | permalink

    [...] ????30???????????HTML5? Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D?????????HTML 5??Canvas ?Video [...]

  47. 50???HTML5???? | April 9, 2011 at 8:23 pm | permalink

    [...] Blowing Up HTML5 Video [...]

  48. [...] Blowing Up HTML5 Video [...]

  49. [...] Blowing up HTML5 video and mapping it into 3D space [...]

  50. [...] Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D?????????html 5??Canvas ?Video [...]

  51. [...] ??http://www.craftymind.com/2010/04/20/blowing-up-html5-video-and-mapping-it-into-3d-space/ [...]

  52. [...] Blowing up HTML5 video and mapping it into 3D space [...]

  53. [...] Blowing up HTML5 video and mapping it into 3D space [...]

  54. [...] Blowing up HTML5 video and mapping it into 3D space [...]

  55. [...] Blowing Up HTML5 Video This tutorial is about a blowing up HTML5 video and mapping it into 3D space. Check it out: [...]

  56. [...] Block based destruction of HTML5 video, best viewed in webkit based browsers. Return to Craftymind Article Due to popular request, the ogg video is now 640 x 360, don’t hold me responsible for [...]

  57. ???????30?HTML5???? | ??? | May 12, 2011 at 8:11 pm | permalink

    [...] Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D?????????HTML 5??Canvas ?Video ?????????????????????Canvas.drawImage() api???????? [...]

  58. ?? | May 17, 2011 at 12:13 am | permalink

    [...] ????????30???????????HTML5? Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D?????????HTML 5??Canvas ?Video [...]

  59. [...] 23. Blowing up HTML5 video and mapping it into 3D space [...]

  60. [...] View Tutorial [...]

  61. Html5?????? - iDotNet | June 2, 2011 at 5:36 am | permalink

    [...] 1.Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D??? [...]

  62. [...] This is a site that allows you to click on certain videos and see how it blows up in front of your very eyes. Plus, the site will teach users on how to create such blowing up videos. Learn how to make a blowing up effect in videos with HTML5. [...]

  63. 30?HTML5???? | ????? | June 29, 2011 at 7:08 pm | permalink

    [...] Blowing up HTML5 video and mapping it into 3D space??HTML5???????3D??? [...]

  64. [...] View Tutorial [...]

  65. [...] Blowing Up HTML5 Video [...]

  66. SoberVc | July 5, 2011 at 4:07 am | permalink

    I Like this~~~~~~

  67. asad | July 30, 2011 at 1:56 am | permalink

    i Like it …………….!!!!!!!!!!!!

  68. [...] Block based destruction of HTML5 video, best viewed in webkit based browsers. Return to Craftymind Article Due to popular request, the ogg video is now 640 x 360, don’t hold me responsible for [...]

  69. 21?????HTML5???? - Joy chao|???? | August 17, 2011 at 6:01 pm | permalink

    [...] ???? [...]

  70. tiger | August 20, 2011 at 12:46 am | permalink

    that is great.

  71. Gonzalo Viñas | October 10, 2011 at 11:26 am | permalink

    I remember double-buffering technics of 90’s. . .

  72. sanctus | October 14, 2011 at 11:19 am | permalink

    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

  73. SANCTUS | October 14, 2011 at 11:21 am | permalink

    HTML5 IS FUCK GARBAGE HAHAHAH

  74. Bob john | October 19, 2011 at 3:31 am | permalink

    Pretty cool. Might try using this somewhere in my website later when HTML5 is supported by more browsers

  75. witek | October 21, 2011 at 9:39 pm | permalink

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

  76. xuying | October 27, 2011 at 7:56 pm | permalink

    ??????????

  77. xuying | October 27, 2011 at 7:57 pm | permalink

    wo bu hui ying wen ,neng fen yi cheng zhong wen bu

  78. HOTSTEAL | November 12, 2011 at 10:09 pm | permalink

    It is a wonderful work!!

  79. YoonjuLee | November 16, 2011 at 1:26 am | permalink

    Looks great!!

  80. Pedro99 | November 28, 2011 at 3:59 am | permalink

    Actually it works pretty fast with the newest version of Opera (v 12.00)!

    :-)

  81. Yordan | January 10, 2012 at 3:21 pm | permalink

    WOW Very Impressive effect.Very nice.

  82. seouk | January 12, 2012 at 4:24 am | permalink

    amazing style , nice post

  83. 20???Html5??????? : PS???-?????? | January 24, 2012 at 9:16 pm | permalink

    [...] Blowing up HTML5 Video and Mapping it into 3D space [...]

  84. [...] Blowing Up HTML5 Video and Mapping it into 3D Space [...]

  85. ????35??????HTML5?? | ?? | February 2, 2012 at 11:37 pm | permalink

    [...] Blowing up HTML5 video and mapping it into 3D space [...]

Leave a Comment

Submit