Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | <!-- |
michael@0 | 2 | Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | --> |
michael@0 | 5 | <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> |
michael@0 | 6 | <path id="p" d="M 100,100 h 50 v 200" fill="none" stroke="red" stroke-width="1"/> |
michael@0 | 7 | <g style="font: 30px monospace"> |
michael@0 | 8 | <text x="100" y="100">a</text> |
michael@0 | 9 | </g> |
michael@0 | 10 | <script><![CDATA[ |
michael@0 | 11 | function put(s, x, y, r) { |
michael@0 | 12 | var e = document.createElementNS("http://www.w3.org/2000/svg", "text"); |
michael@0 | 13 | e.setAttribute("x", x); |
michael@0 | 14 | e.setAttribute("y", y); |
michael@0 | 15 | e.setAttribute("rotate", r); |
michael@0 | 16 | e.textContent = s; |
michael@0 | 17 | g.appendChild(e); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | var s = "a.b.c.d.e."; |
michael@0 | 21 | var text = document.getElementsByTagName("text")[0]; |
michael@0 | 22 | var g = text.parentNode; |
michael@0 | 23 | var adv = text.getSubStringLength(0, 1); |
michael@0 | 24 | for (var i = 1; i < s.length; i++) { |
michael@0 | 25 | var x = i * adv; |
michael@0 | 26 | if (x + adv / 2 < 50) { |
michael@0 | 27 | put(s[i], 100 + x, 100, 0); |
michael@0 | 28 | } else { |
michael@0 | 29 | put(s[i], 150, 50 + x, 90); |
michael@0 | 30 | } |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | var lastText = document.getElementsByTagName("text")[s.length - 1]; |
michael@0 | 34 | var range = document.createRange(); |
michael@0 | 35 | range.setStart(text.firstChild, 1); |
michael@0 | 36 | range.setEnd(lastText.firstChild, 0); |
michael@0 | 37 | window.getSelection().addRange(range); |
michael@0 | 38 | ]]></script> |
michael@0 | 39 | </svg> |