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 var textElements = document.querySelectorAll(".testTextRun"); | |
fs
2016/03/31 09:01:41
This shadows the parameter (which is always this a
| |
8 for (var elemNum = 0; elemNum < textElements.length; ++elemNum) { | |
9 var text = textElements[elemNum]; | |
10 var charCount = text.getNumberOfChars(); | |
11 for (var index = 0; index < charCount; ++index) { | |
12 var color = colors[index % colors.length]; | |
13 highlightGlyph(text, index, color, highlightContainer); | |
14 } | |
15 } | |
16 } | |
17 | |
18 function highlightGlyph(textElement, index, color, highlightContainer) { | |
19 var extent = textElement.getExtentOfChar(index); | |
20 | |
21 // Highlight rect that we've selected using the extent information | |
22 var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect"); | |
23 rect.setAttribute("x", extent.x); | |
24 rect.setAttribute("y", extent.y); | |
25 rect.setAttribute("width", extent.width); | |
26 rect.setAttribute("height", extent.height); | |
27 rect.setAttribute("fill-opacity", "0.5"); | |
28 rect.setAttribute("fill", color); | |
29 highlightContainer.appendChild(rect); | |
30 | |
31 // Output the start offset | |
32 var text = document.createElementNS("http://www.w3.org/2000/svg", "text"); | |
33 text.setAttribute("x", extent.x + extent.width / 2); | |
34 text.setAttribute("y", extent.y + extent.height + 5); | |
35 text.setAttribute("text-anchor", "middle"); | |
36 text.setAttribute("font-size", 8); | |
37 text.setAttribute("style", "-webkit-user-select: none; select: none;"); | |
38 text.appendChild(document.createTextNode(index)); | |
39 highlightContainer.appendChild(text); | |
40 } | |
OLD | NEW |