Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - select invisible text overlaid on image, ala GMail PDF viewer

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?

like image 486
StackOverflowNewbie Avatar asked Nov 09 '10 04:11

StackOverflowNewbie


1 Answers

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:

  1. Some better way of checking for position-based matches; I just do whether it's included in the box.
  2. Dynamic updating on mousemove
  3. Line-based rather than span-based
  4. You could still do selecting by background color, but depending on how your elements are arranged it might not look very good. Also would need to support transparency.
like image 86
theazureshadow Avatar answered Nov 14 '22 16:11

theazureshadow