Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const rawdata = window.api.state
- const namespace = 'http://www.w3.org/2000/svg';
- const svg = document.createElement('svg');
- function createNode(type, attributes) {
- const node = document.createElementNS(namespace, type);
- Object.keys(attributes).forEach((attributeKey) => {
- node.setAttributeNS(null, attributeKey, attributes[attributeKey]);
- });
- return node;
- }
- const areas = rawdata.areas;
- let minX = Infinity;
- let minY = Infinity;
- let maxX = -Infinity;
- let maxY = -Infinity;
- areas.forEach((area) => {
- const points = area.poly.map((point) => `${point.x} ${point.y}`).join(',');
- area.poly.forEach((point) => {
- if (point.x < minX) {
- minX = point.x;
- }
- if (point.x > maxX) {
- maxX = point.x;
- }
- if (point.y < minY) {
- minY = point.y;
- }
- if (point.y > maxY) {
- maxY = point.y;
- }
- });
- const polygon = createNode('polygon', { points });
- polygon.classList.add('room');
- svg.appendChild(polygon);
- });
- const walls = rawdata.walls;
- walls.forEach((wall) => {
- const line = createNode('line', { x1: wall.a.x, y1: wall.a.y, x2: wall.b.x, y2: wall.b.y })
- line.classList.add('wall');
- svg.appendChild(line);
- const walldx = wall.b.x - wall.a.x;
- const walldy = wall.b.y - wall.a.y;
- const wallLength = Math.sqrt(walldx * walldx + walldy * walldy);
- wall.openings.forEach((opening) => {
- const openingLength = opening.width;
- const openingRatio = openingLength / wallLength;
- const x1 = wall.a.x + opening.t * walldx - openingRatio / 2 * walldx;
- const y1 = wall.a.y + opening.t * walldy - openingRatio / 2 * walldy;
- const x2 = x1 + walldx * openingRatio;
- const y2 = y1 + walldy * openingRatio;
- const openingLine = createNode('line', { x1, y1, x2, y2 });
- openingLine.classList.add('opening');
- openingLine.classList.add(`opening--${opening.type}`);
- svg.appendChild(openingLine);
- });
- });
- const width = maxX - minX;
- const height = maxY - minY;
- const padding = 0.1;
- svg.setAttributeNS(null, 'viewBox', `${minX - padding * width} ${minY - padding * height} ${width + 2 * padding * width} ${height + 2 * padding * height}`);
- const svgData = `<?xml version="1.0" encoding="UTF-8" standalone="no"?>${svg.outerHTML}`;
- const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
- const svgUrl = URL.createObjectURL(svgBlob);
- const downloadLink = document.createElement('a');
- downloadLink.href = svgUrl;
- downloadLink.download = 'floorplan.svg';
- document.body.appendChild(downloadLink);
- downloadLink.click();
- document.body.removeChild(downloadLink);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement