OLD | NEW |
(Empty) | |
| 1 // Highlight glyphs from multiple text elements using SVG text queries. |
| 2 |
| 3 var colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]; |
| 4 function highlightGlyphs(textElements, highlightContainer) { |
| 5 // Highlight each glyph with a semi-transparent rectangle and |
| 6 // a number corresponding to the queried character index. |
| 7 for (var elemNum = 0; elemNum < textElements.length; ++elemNum) { |
| 8 var text = textElements[elemNum]; |
| 9 var charCount = text.getNumberOfChars(); |
| 10 for (var index = 0; index < charCount; ++index) { |
| 11 var color = colors[index % colors.length]; |
| 12 highlightGlyph(text, index, color, highlightContainer); |
| 13 } |
| 14 } |
| 15 } |
| 16 |
| 17 function highlightGlyph(textElement, index, color, highlightContainer) { |
| 18 var extent = textElement.getExtentOfChar(index); |
| 19 |
| 20 // Highlight rect that we've selected using the extent information |
| 21 var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect"); |
| 22 rect.setAttribute("x", extent.x); |
| 23 rect.setAttribute("y", extent.y); |
| 24 rect.setAttribute("width", extent.width); |
| 25 rect.setAttribute("height", extent.height); |
| 26 rect.setAttribute("fill-opacity", "0.5"); |
| 27 rect.setAttribute("fill", color); |
| 28 highlightContainer.appendChild(rect); |
| 29 |
| 30 // Output the start offset |
| 31 var text = document.createElementNS("http://www.w3.org/2000/svg", "text"); |
| 32 text.setAttribute("x", extent.x + extent.width / 2); |
| 33 text.setAttribute("y", extent.y + extent.height + 5); |
| 34 text.setAttribute("text-anchor", "middle"); |
| 35 text.setAttribute("font-size", 8); |
| 36 text.setAttribute("style", "-webkit-user-select: none; select: none;"); |
| 37 text.appendChild(document.createTextNode(index)); |
| 38 highlightContainer.appendChild(text); |
| 39 } |
OLD | NEW |