【发布时间】:2019-08-26 08:29:31
【问题描述】:
我正在使用 SVG 路径在两个 Angular Material 树组件之间绘制连接线。我有一个问题,因为当我展开树节点时,路径会向上或向下移动,但我希望它保持在同一位置。当我折叠树节点时,我想将路径从子节点移动到父节点。
我尝试过转换:翻译或矩阵,但我不知道如何计算值。
我应该使用变换还是编辑路径的“d”属性??
这就是我创建 svg 的方式:
createSVG() {
let svgContainer = document.getElementById('svg-main-container');
this.svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
this.svg.setAttribute('id', 'svg-canvas');
this.svg.setAttribute('style', `position:absolute;left:0;top:0;display:inline-block;height:100%;width:100%`);
this.svg.setAttribute('viewBox', '0 0 100 100');
this.svg.setAttribute("preserveAspectRatio", "none");
this.svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
svgContainer.appendChild(this.svg);
this.svgService.svg = this.svg;
return this.svg;
}
这是我绘制连接路径的方式:
drawConnector(a,b){
let path = document.createElementNS("http://www.w3.org/2000/svg", "path");
let d = `M${a.x_left},${a.y_left} C50,${a.y_left} 50 ${b.y_right} ${b.x_right} ${b.y_right}`;
path.setAttributeNS(null, "d", d);
path.setAttributeNS(null, "fill", "none");
path.setAttributeNS(null, "stroke", "#555");
path.setAttributeNS(null, "stroke-width", "1.5px");
path.setAttributeNS(null, "vector-effect", "non-scaling-stroke");
path.setAttribute("id", this.svgService.draggedElementId);
path.setAttribute("class", this.svgService.draggedElementId);
this.svgService.svg.appendChild(path);
}
我如何连接元素:
connectDivs(leftId, rightId, color, tension) {
leftId = this.svgService.draggedElementId
rightId = this.svgService.droppedElementId
this.svgService.connections.push({leftId,rightId});
let svgContainer = document.getElementById('svg-component');
let mainBox = svgContainer.getBoundingClientRect();
let points = [];
//left element
let left = document.getElementById(leftId);
let leftPosition = left.getBoundingClientRect()
let x_left = this.mapCoordinates(leftPosition.left - mainBox.left + leftPosition.width/2, mainBox.left, mainBox.left + mainBox.width, 0, 100);
let y_left = this.mapCoordinates(leftPosition.top - mainBox.top + leftPosition.height/2, mainBox.top, mainBox.top + mainBox.height, 0, 100);
points.push({x_left, y_left});
//right element
let right = document.getElementById(rightId);
let rightPosition = right.getBoundingClientRect()
let x_right = this.mapCoordinates(rightPosition.left - mainBox.left + rightPosition.width/2, mainBox.left, mainBox.left + mainBox.width, 0, 100);
let y_right = this.mapCoordinates(rightPosition.top - mainBox.top + rightPosition.height/2, mainBox.top, mainBox.top + mainBox.height, 0, 100);
points.push({x_right, y_right});
this.drawConnector(points[0], points[1]);
}
以及我如何在树展开时尝试转换路径:
expandLines(node){
let nodeElement = document.getElementById(node.id);
let svgContainer = document.getElementById('svg-main-container');
let svgBox = svgContainer.getBoundingClientRect();
let nodePosition = nodeElement.getBoundingClientRect() //position of collapsed/expanded tree element
let x_node = this.mapCoordinates(nodePosition.left - svgBox.left + nodePosition.width/2, svgBox.left, svgBox.left + svgBox.width, 0, 100);
let y_node = this.mapCoordinates(nodePosition.top - svgBox.top + nodePosition.height/2, svgBox.top, svgBox.top + svgBox.height, 0, 100);
let svgCanvas = document.getElementById('svg-canvas');
let svgCanvasBox = svgCanvas.getBoundingClientRect();
let path = svgCanvas.getElementsByClassName(node.id)[0];
let pathPosition = path.getBoundingClientRect();
let x_path = this.mapCoordinates(pathPosition.left - svgCanvasBox.left + pathPosition.width/2, svgCanvasBox.left, svgCanvasBox.left + svgCanvasBox.width, 0, 100);
let y_path = this.mapCoordinates(pathPosition.top - svgCanvasBox.top + pathPosition.height/2, svgCanvasBox.top, svgCanvasBox.top + svgCanvasBox.height, 0, 100);
let trans_x = x_path + x_node;
let trans_y = y_path - y_node;
//path.setAttribute('transform',`translate(${trans_x}, ${trans_y})`);
path.setAttribute('transform', `matrix(1,0,0,1,${trans_x},${trans_y})`);
}
mapCoordinates(n, a, b, _a, _b){
let d = b - a;
let _d = _b - _a;
let u = _d / d;
return _a + n * u;
}
我所拥有的图片:
【问题讨论】:
标签: javascript typescript svg