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,y
text 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 class highlight-pane
). When you select text, it fills in a #selection-content
textarea with the contents of what you have selected, and selects the text in it (try using window.getSelection().anchorNode
in 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 assume mousedown
and mouseup
). (Here's an example pdf document)
If you're absolute-positioning the elements, you could detect mousedown
, mousemove
and mouseup
, 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 mousedown
and mouseup
, 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With