Developer Relations @ Google
Using HTML5 Canvas with Drag and Drop (setDragImage)
I know a lot of people complain about Drag and Drop (DnD), but it is not all that bad. At least we have a platform to work off for the future.
We have recently seen a lot of improvements to Gmail which now includes additional support for Dragging in to Gmail to upload your docs and files, and also dragging attachments out of Gmail to the user file system. I like both of these pieces of functionality, they are subtle but introduce
One thing that I see from the jQuery UI world is that it makes DnD very easy to implement but also very pretty. Whilst HTML5 DnD has support for attaching any arbitrary element to the drag operation, in practice I have never seen a browser implement it, so we are left to our own devices on what we want to show to the user during a drag operation.
I have produced a very (rough) sample – http://html5samples.appspot.com/canvasToDrag.html. There is still a lot of work for me to do with my sample (it needs to be a lot prettier). It is very simple to use, select the negatives that you wish to “develop” and drag them into the development area. You will see that there is a “fan” effect on the drag icon. I must warn that this only works on Webkit browsers (so Chrome and Safari – actually, the drop doesn’t work in Safari yet)
This is all enabled through DataURI’s. I take the selected images and add them to the canvas (adding a little rotation and scaling for effect). Once the canvas is completed I simply call .toDataURL() on the canvas element and add it to a temporary image element. This image element is then used as in the call to the setDragImage method
// Code inside the dragStart event. var uris = []; // A list of img uris, so I know what to display after the drag var img = document.createElement("img"); img.src = canvas.toDataURL(); e.dataTransfer.setData("text/plain", "Text to drag"); e.dataTransfer.setData("text/uri-list", uris.join("\n")); e.dataTransfer.setDragImage(img, 128,128);
It is pretty simple, and can allow you to do some nice effects. Things that I can’t do in HTML5 Drag and Drop is animate the element being dragged. I would love to be able animate the canvas as the user drags the elements around the screen, maybe adding a bit of inertia to the photographs as they are dragged around.
I really wanted to get a reference to Walker Texas Ranger in to the title, but I really couldn’t think of anything that cool. If you can think of a great Chuck Norris reference leave a comment, I am all chins!
It always amazes me that there is so much to HTML that is still not being exploited by developers.
One pattern I see regularly is recursive descent through the DOM to find particular TEXT nodes that contain a given string so that the container element can be manipulated.
It is not that recursion is slow, if your DOM is complex enough you could hit stack overflow errors (although it is pretty unlikely), it is that there are a lot of edge cases when parsing the DOM that you need to code in.
A little known DOM function is available that makes developing applications that need to scan the DOM easy. It is called Tree Walker, created through the createTreeWalker function on the document.
You can create a tree walker very quickly using the following Javascript:
document.createTreeWalker(document.body, NODE_FILTER.SHOW_TEXT, function(node) { return NodeFilter.FILTER_ACCEPT; }, false); while(treeWalker.nextNode()) console.log(treeWalker.currentNode);
The above code is given a root node of document.body, a filter of what to show (only Text Nodes in our case), and a function that returns if the node should be returned (essentially a filter).
An interesting point to note is that the Filter function is only called when iterating over the treeWalker.
This is actually a really cool feature, the currentNode property of the Tree Walker contains DOM objects, so you can start to do some really advanced processing, you could highlight the current node, replace its text or remove it – really anything you want. This is significantly simpler than managing the recursion yourself.
As a more concrete example, lets use this to find all twitter user names on a page and then automatically make these a twitter link. It could be done using recursion pretty simply, but I need something fun to show you.
var re = new RegExp(); // This isn't accurate RE re.compile("@([A-Za-z0-9_]*)"); var walker = document.createTreeWalker( document.body, NodeFilter.SHOW_TEXT, function(node) { var matches = node.textContent.match(re); if(matches) { return NodeFilter.FILTER_ACCEPT; } else { return NodeFilter.FILTER_SKIP; } }, false); var nodes = []; while(walker.nextNode()) { nodes.push(walker.currentNode); } for(var i = 0; node=nodes[i] ; i++) { node.parentNode.innerHTML = node.parentNode.innerHTML.replace(re, "@$1") }
A live example is on my sample site
The theory is, that User-Agents can optimize the access to the DOM better than you can recursively descend through the DOM. So, where would I use this? The first thing that springs to mind is that it is ideal for Chrome extensions. Many Chrome extensions traverse the DOM looking for pieces of text, or particular patterns inside nodes that aren’t available via CSS Selectors.
More information can be found on Mozilla’s Developer site