outline-ref.html 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <body>
  4. <svg xmlns="http://www.w3.org/2000/svg" overflow="visible">
  5. <g>
  6. <rect id="rect" width="100" height="100" style="fill: yellow"/>
  7. <text id="text" x="0" y="140" font-family="Verdana" font-size="20">
  8. Hello world
  9. </text>
  10. <foreignObject id="foreignObject" x="0" y="160" width="200" height="60">
  11. <div xmlns="http://www.w3.org/1999/xhtml">
  12. Here is a paragraph that requires word wrap
  13. </div>
  14. </foreignObject>
  15. <circle id="circle" cx="40" cy="275" r="40" style="fill: green"/>
  16. <ellipse id="ellipse" cx="70" cy="380" rx="70" ry="50" style="fill: yellow"/>
  17. <image id="image" x="0" y="450" height="100" width="100"/>
  18. <line id="line" x1="0" y1="580" x2="100" y2="580" style="stroke: red; stroke-width: 2"/>
  19. <path id="path" d="M50 600 L10 650 L90 650 Z"/>
  20. <polygon id="polygon" points="300,50 350,0 400,50" style="fill: lime"/>
  21. <polyline id="polyline" points="300,80 350,70 400,80" style="fill: none;stroke: black; stroke-width: 3"/>
  22. <g transform="translate(300, 70)">
  23. <circle id="gCircle" cx="50" cy="50" r="20" style="fill: blue"/>
  24. </g>
  25. <g transform="translate(300, 150)">
  26. <circle id="ggCircle" cx="50" cy="50" r="20" style="fill: green"/>
  27. <g>
  28. <rect id="ggRect" x="15" y ="15" width="30" height="10" style="fill: blue"/>
  29. </g>
  30. </g>
  31. <svg x="300" y="250">
  32. <rect id="innerRect" x="30" y="10" height="50" width="50" style="fill: red"/>
  33. </svg>
  34. </g>
  35. </svg>
  36. <script>
  37. function createOutline(boundingRect) {
  38. // Outline starts from a top-left shift pixel of the bounding rect
  39. var left = boundingRect.left - 1;
  40. var top = boundingRect.top - 1;
  41. var right = boundingRect.right;
  42. var bottom = boundingRect.bottom;
  43. var width = boundingRect.width;
  44. var height = boundingRect.height;
  45. var lines = document.createElement("div");
  46. var styles = 'border: 1px solid;'
  47. + 'width: ' + width + 'px;'
  48. + 'height: ' + height + 'px;'
  49. + 'position: absolute;'
  50. + 'top: ' + top + 'px;'
  51. + 'left: ' + left + 'px;';
  52. lines.setAttribute('style', styles);
  53. document.body.appendChild(lines);
  54. }
  55. window.onload = function drawOutline() {
  56. var elements = ['rect', 'foreignObject', 'circle',
  57. 'ellipse', 'image', 'line', 'path',
  58. 'polygon', 'polyline', 'text','gCircle',
  59. 'innerRect'];
  60. elements.forEach(id => {
  61. var element = document.getElementById(id);
  62. createOutline(element.getBoundingClientRect());
  63. });
  64. var ggRect = document.getElementById('ggRect');
  65. var ggRectbbox = ggRect.getBoundingClientRect();
  66. createOutline(ggRectbbox);
  67. var ggCircle = document.getElementById('ggCircle');
  68. var ggCirclebbox = ggCircle.getBoundingClientRect();
  69. var ggbbox = {
  70. left: ggRectbbox.left,
  71. top: ggRectbbox.top,
  72. right: ggCirclebbox.right,
  73. bottom: ggCirclebbox.bottom
  74. };
  75. ggbbox.width = ggbbox.right - ggbbox.left;
  76. ggbbox.height = ggbbox.bottom - ggbbox.top;
  77. createOutline(ggbbox);
  78. }
  79. </script>
  80. </body>
  81. </html>