Guest User

Untitled

a guest
Apr 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. style x
  2. client 300x100
  3. offset 300x100
  4.  
  5. stylex
  6. client300x100
  7. offsetundefinedxundefined
  8.  
  9. "style" "x"
  10. "client" "0x0"
  11. "offset" "undefinedxundefined"
  12.  
  13. <svg id="svg1" xmlns="http://www.w3.org/2000/svg" version="1.1">
  14. <circle cx="50" cy="50" r="40" stroke="black" stroke-width="1" fill="red" />
  15. <circle cx="150" cy="50" r="40" stroke="black" stroke-width="1" fill="green" />
  16. <circle cx="250" cy="50" r="40" stroke="black" stroke-width="1" fill="blue" />
  17. </svg>
  18.  
  19. var svg1 = document.getElementById('svg1');
  20.  
  21. console.log(svg1);
  22. console.log('style', svg1.style.width + 'x' + svg1.style.height);
  23. console.log('client', svg1.clientWidth + 'x' + svg1.clientHeight);
  24. console.log('offset', svg1.offsetWidth + 'x' + svg1.offsetHeight);
  25.  
  26. #svg1 {
  27. width: 300px;
  28. height: 100px;
  29. }
  30.  
  31. var bBox = svg1.getBBox();
  32. console.log('XxY', bBox.x + 'x' + bBox.y);
  33. console.log('size', bBox.width + 'x' + bBox.height);
  34.  
  35. var el = document.getElementById("yourElement"); // or other selector like querySelector()
  36. var rect = el.getBoundingClientRect(); // get the bounding rectangle
  37.  
  38. console.log( rect.width );
  39. console.log( rect.height);
  40.  
  41. var chart = document.getElementsByClassName("chart")[0];
  42. var width = chart.getClientRects()[0].width;
  43. var height = chart.getClientRects()[0].height;
  44.  
  45. var heightComponents = ['height', 'paddingTop', 'paddingBottom', 'borderTopWidth', 'borderBottomWidth'],
  46. widthComponents = ['width', 'paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth'];
  47.  
  48. var svgCalculateSize = function (el) {
  49.  
  50. var gCS = window.getComputedStyle(el), // using gCS because IE8- has no support for svg anyway
  51. bounds = {
  52. width: 0,
  53. height: 0
  54. };
  55.  
  56. heightComponents.forEach(function (css) {
  57. bounds.height += parseFloat(gCS[css]);
  58. });
  59. widthComponents.forEach(function (css) {
  60. bounds.width += parseFloat(gCS[css]);
  61. });
  62. return bounds;
  63. };
  64.  
  65. let div = document.querySelector("div");
  66. let style = getComputedStyle(div);
  67.  
  68. let width = parseFloat(style.width.replace("px", ""));
  69. let height = parseFloat(style.height.replace("px", ""));
  70.  
  71. let svg = document.querySelector("svg");
  72. let box = svg.getBBox(); // box.height, box.height
Add Comment
Please, Sign In to add comment