I’m overlaying invisible text on top of an image. Is there a jQuery plugin (or similar) that will allow users to select an area on the image (which also selects the overlaid text) and be able to copy the content.
Right now, I have placed each character in its own <span /> tag. Problem is when user selects, it sometimes select all the overlaid text (unless user is extremely precise with his/her mouse), sometimes the image itself becomes selected, etc.
A solution similar to GMail’s PDF viewer would be nice. Suggestions?
Google seems to know from a pdf where the various
x,ytext offsets are in the file. When you select a bunch of lines, it places a set of absolutely positioned “selection” divs over the image where the “text” is (they have classhighlight-pane). When you select text, it fills in a#selection-contenttextarea with the contents of what you have selected, and selects the text in it (try usingwindow.getSelection().anchorNodein Chrome, e.g.). Besides those selection overlays, there is just an image.page-image. I bet they actually use window to capture all the mouse gestures they care about (I assumemousedownandmouseup). (Here’s an example pdf document)If you’re absolute-positioning the elements, you could detect
mousedown,mousemoveandmouseup, figure out the span elements mouse is over (or nearest to), and fill in a textarea with the contents of all content between those two elements. If you want to just use word-granularity, I doubt anyone would complain (surround each word with a span, rather than each letter).Edit: I got kinda curious last night and coded up a really naive version. It only does
mousedownandmouseup, and it doesn’t work in IE (I don’t feel like debugging it 🙂Check it out on jsfiddle.
Features you might want to add:
mousemove