Advertisement
Guest User

Untitled

a guest
Sep 16th, 2017
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const rawdata = window.api.state
  2. const namespace = 'http://www.w3.org/2000/svg';
  3. const svg = document.createElement('svg');
  4.  
  5. function createNode(type, attributes) {
  6.   const node = document.createElementNS(namespace, type);
  7.   Object.keys(attributes).forEach((attributeKey) => {
  8.     node.setAttributeNS(null, attributeKey, attributes[attributeKey]);
  9.   });
  10.   return node;
  11. }
  12.  
  13. const areas = rawdata.areas;
  14.  
  15. let minX = Infinity;
  16. let minY = Infinity;
  17. let maxX = -Infinity;
  18. let maxY = -Infinity;
  19. areas.forEach((area) => {
  20.   const points = area.poly.map((point) => `${point.x} ${point.y}`).join(',');
  21.   area.poly.forEach((point) => {
  22.     if (point.x < minX) {
  23.       minX = point.x;
  24.     }
  25.     if (point.x > maxX) {
  26.       maxX = point.x;
  27.     }
  28.     if (point.y < minY) {
  29.       minY = point.y;
  30.     }
  31.     if (point.y > maxY) {
  32.       maxY = point.y;
  33.     }
  34.   });
  35.   const polygon = createNode('polygon', { points });
  36.   polygon.classList.add('room');
  37.   svg.appendChild(polygon);
  38. });
  39.  
  40. const walls = rawdata.walls;
  41. walls.forEach((wall) => {
  42.   const line = createNode('line', { x1: wall.a.x, y1: wall.a.y, x2: wall.b.x, y2: wall.b.y })
  43.   line.classList.add('wall');
  44.   svg.appendChild(line);
  45.  
  46.   const walldx = wall.b.x - wall.a.x;
  47.   const walldy = wall.b.y - wall.a.y;
  48.   const wallLength = Math.sqrt(walldx * walldx + walldy * walldy);
  49.  
  50.   wall.openings.forEach((opening) => {
  51.     const openingLength = opening.width;
  52.     const openingRatio = openingLength / wallLength;
  53.     const x1 = wall.a.x + opening.t * walldx - openingRatio / 2 * walldx;
  54.     const y1 = wall.a.y + opening.t * walldy - openingRatio / 2 * walldy;
  55.     const x2 = x1 + walldx * openingRatio;
  56.     const y2 = y1 + walldy * openingRatio;
  57.     const openingLine = createNode('line', { x1, y1, x2, y2 });
  58.     openingLine.classList.add('opening');
  59.     openingLine.classList.add(`opening--${opening.type}`);
  60.     svg.appendChild(openingLine);
  61.   });
  62. });
  63.  
  64. const width = maxX - minX;
  65. const height = maxY - minY;
  66. const padding = 0.1;
  67.  
  68. svg.setAttributeNS(null, 'viewBox', `${minX - padding * width} ${minY - padding * height} ${width + 2 * padding * width} ${height + 2 * padding * height}`);
  69.  
  70. const svgData = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>${svg.outerHTML}`;
  71. const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
  72. const svgUrl = URL.createObjectURL(svgBlob);
  73. const downloadLink = document.createElement('a');
  74. downloadLink.href = svgUrl;
  75. downloadLink.download = 'floorplan.svg';
  76. document.body.appendChild(downloadLink);
  77. downloadLink.click();
  78. document.body.removeChild(downloadLink);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement