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.
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)
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.
fantastic,but what is its pupose
Firefox 4b5pre runs even the 3D demo at a flawless speed on an intel pentium dual core running vindows vista.
Also, hardware acceleration is turned off (like is is by default) and antialiasing is used (unlike chrome which does not use antialiasing)
thanks for sharing. great news
can this be done with the videos playing through embed(embed tag) source?
wow awesome! html5 is really rocks the 3d effects :>
Flawless on IE 9 Beta.
Very cool demo!
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.
If this would work on iOS, we could watch 3-D videos in true colors.
??? ??? ???
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.
please give me!
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…
Thats one of the awesome ones HTML5 work presentations.
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.
?
Veri nice, we are want develov our web with flatform HTML 5
very useful tips about HTML5, love it ^_^
I Like this~~~~~~
i Like it …………….!!!!!!!!!!!!
that is great.
I remember double-buffering technics of 90’s. . .
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
HTML5 IS FUCK GARBAGE HAHAHAH
Pretty cool. Might try using this somewhere in my website later when HTML5 is supported by more browsers
Works with Opera without any problem (full support, no crashes, very smooth even on my slow computer).
??????????
wo bu hui ying wen ,neng fen yi cheng zhong wen bu
It is a wonderful work!!
Looks great!!
Actually it works pretty fast with the newest version of Opera (v 12.00)!
WOW Very Impressive effect.Very nice.
amazing style , nice post
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.
AED
AED
http://touch.dailymotion.com/?ref=bookmark&refid=0
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
perfecto…awesome \m/
Wow such a great tutorials dude, I was searching for such a awesome tuts. Thanks for sharing
i m going in
This is brilliant.
cool video, this works in my chrome..
awesome Sean. Hard code. is there live demo from your project?
Agreed with you.Quite a good post! I do love it!
http://9pillsonline.com/
Gosh, this is not what is the invention, you say, this is definitely.
I love your stories really much because they are published in an understandable comprehensible. So I can study them although I come from Germany and have some problems to translate English stories.
I like a good video
Your Blog Very Nice, Thanks For Share It.Thank You.
fantastic man
Is it possible to have same effect on simple image?
Hi! Is it possible to use a webcam or IP camera as the video source for what you are doing inside of video and canvas?????? joshmarinacci points to your exploding video in his very nice piece on (canvasdeepdive) but while he mentions webcams at the beginning of his piece, he never goes back to it. Please — can u help? just tell me it’s not possible, OR, if it can be done — how do you set up the “source” for those inputs???
Thank you very much for any help here.
Oops — previous post meant to write “…on his very nice piece on (canvasdeepdive), but while…”
Oh, sorry! I see I am putting “canvas” into brackets, so the input control is filtering it out! OK, that previous post was supposed to be “…on his very nice piece on the canvas tag (canvasdeepdive), but while…”
really nice share
Very cool demo! like it~
Html5 just killed flash! i like it!
Its Awesome
mapping it into 3D space means you need more resources, is’t it?
Wow, this is really smooth ! But not very useful
Firefox just keeps on chugging – whether it’s 3d, or even some of the facebook games – it’s struggle city even on a brand new computer. Chrome runs the best, easily.
Blowing up HTML5 video and mapping it into 3D space. A little too geeky for me though. Nice tutorial
thank you
You were asking why you need two buffers : that’s called “double duffering” and is a good pattern in all image processing. When browser sends information of the current buffer to the screen (read operation on buffer), you can already compute the next image (write operation on buffer) but on a new buffer, else you would corrupt the previous image before it has been sent to screen. Using a second buffer, like you did, is the correct way of dealing with this. (look for double-buffering or multi-buffering for more details).
Your exploding video is quite impressive, really nice job
this is really awesome effect very good your post my friend!
Nicely done, html is still popular of all the time!
That’s way cool!~!I love it , hihi, very nice
Great article, I like It! Nice, very nice, thanks for information.
Great… it is awesome to see what HTML5 is can do !!!
i was wondering if i could do the same with an image instead of a video…?
i am very new to this, any pointers would be just fine… thanks…
This is very cool, under Chrome browser is working just fine.
Blowing apart fragments of video, im interesting things.. maybe i can try.
like a champ on linux with an old video card
wow wow 3d demo! like it
I tried this on both Firefox 3.6 and the latest install of chrome. I found that Chrome handled the spinning and shattering of the video better but Firefox did the actually playing of the video much more smoothly. Also the video looping failed on Chrome, so it would play through once, then instead of restarting it would only stop on the first frame. I was surprised, because even without reading that the author expected poor performance on Firefox, I thought Chrome would be better for html5, it seems that this is not universally true. I’m running winxp SP3 with a 3.2 Ghz P4, and 2GB of ram.
sprz?t si?owy
Usefull advices, I used to have a problem with graphic, but now i dont!
Cool effect. The 3D video looks great. Thanks for sharing.
So cool!!!
I’m not sure why iPad wouldn’t work for you guys, Safari plays it perfectly.
Incredibly cool! Very well done indeed!
You just showed us HOW advanced HTML5 is!
thank a lot.
I couldn’t get it to work on firefox but chrome worked
Worked perfectly in Firefox 15.0 for Win 7
Thanks for your demo, I like it!!!
Interesting tutorial, great share. Thanks for sharing.
I heard HTML 5 is the coolest one
i love your page
Firefox on Windows plays both flawlessly, too.
I seriously love the blowing parts of the video, looks amazing, great work!
Wow! You were right! Now html5 is a standard.
a like this is code html 3D.. i w’nt to try to my site..
sipp… wonderfulll…
Really nice.
Really cool post, I looked here – good luck
how to create image or video small size?
For example how many individuals are still having problems composing css that all internet explorer can study without having to crack it up a bit. IE, 6 -7 band alert bells! Terrible discuss toss things back a few years. I know these internet explorer will never stay for ever but there are still individuals still using these repetitive old internet explorer, half the reason why display was presented in the first place – operates on all browses and systems globally.
Hi,
That’s sad that is it not working in safari! safari doesn not support the HTML5 fully yet
Beautiful. Keep up the good work. Html5 is the future
I was searching for such a awesome tuts. Thanks for sharing
Hi,
How do you publish this shit?
Thanks,
Edgar Zotov
How do ya make it run on JSP site I mean?
problem to see videos in some browser need suggesion!!!!!!!!!
like a champ on linux with an old video card