从浏览器中删除SVG元素的交互式方式
Interactive way to remove SVG elements from browser
我正在通过javascript/inline HTML5生成SVG矩形,我想知道我是否可以在一个矩形的右上角附加一个结束图标(如这样:https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRV4yho7mQoRWj-wEd8LvvKjs4sMAWSFi9cJu5IxGtWn667ofL10g),这样当我拖动矩形时,图标不会保持静态到该位置,而是始终附加到该矩形。
我的目标是让用户有机会从页面中删除该矩形。
如果有更简单的方法可以完成此删除操作,请随时告诉我!
提前感谢!
拖放功能:
var selectedElement = 0;
var currentX = 10;
var currentY = 10;
var currentMatrix = 0;
function selectElement(evt) {
selectedElement = evt.target;
currentX = evt.clientX;
currentY = evt.clientY;
currentMatrix = selectedElement.getAttributeNS(null, "transform").slice(7, -1).split(' ');
for (var i = 0; i < currentMatrix.length; i++) {
currentMatrix[i] = parseFloat(currentMatrix[i]);
}
selectedElement.setAttributeNS(null, "onmousemove", "moveElement(evt)");
selectedElement.setAttributeNS(null, "onmouseout", "deselectElement(event)");
selectedElement.setAttributeNS(null, "onmouseup", "deselectElement(event)");
}
function deselectElement(evt) {
if (selectedElement != 0) {
selectedElement.removeAttributeNS(null, "onmousemove");
selectedElement.removeAttributeNS(null, "onmouseout");
selectedElement.removeAttributeNS(null, "onmouseup");
selectedElement = 0;
}
}
function moveElement(evt) {
dx = evt.clientX - currentX;
dy = evt.clientY - currentY;
currentMatrix[4] += dx;
currentMatrix[5] += dy;
newMatrix = "matrix(" + currentMatrix.join(' ') + ")";
selectedElement.setAttributeNS(null, "transform", newMatrix);
currentX = evt.clientX;
currentY = evt.clientY;
}
创建元素:
var g1 = document.createElementNS(svgNS, 'g');
g1.setAttribute('transform', "translate(200,200)");
rect = document.createElementNS(svgNS, 'rect');
circle = document.createElementNS(svgNS, 'circle');
rect.setAttribute('id', "rec1");
rect.setAttribute('x', 224);
rect.setAttribute('y', 34);
rect.setAttribute('width', 188);
rect.setAttribute('height', 68);
rect.setAttribute('class', "draggableOrgUnit");
rect.setAttribute('transform', "matrix(1 0 0 1 0 0)");
rect.setAttribute('onmousedown', "selectElement(event)");
//rect.setAttribute('onmouseup', "drop(event)");
circle.setAttribute('id', "c0");
circle.setAttribute('cx', "224");
circle.setAttribute('cy', "34");
circle.setAttribute('r', "5");
circle.setAttribute('onclick', "closeMe()");
circle.setAttribute('fill', "blue");
输出:
<svg>
<g>
<g>
<rect>
<circle>
</g>
...
<g>
</svg>
我只测试这个矩形。一旦这起作用,其他人将遵循相同的逻辑。
您可以将 rect 和单击关闭元素放在 <g>
元素内。 然后,要放置矩形,您需要将<g>
转换为所需位置。如果需要,您也可以通过翻译拖放<g>
。
下面的示例显示了一个圆圈作为单击关闭元素,放置在矩形的右上角。除了圆圈,您还可以根据示例使用 <image>
元素:
<g transform="translate(200 200)">
<rect width=50 height=25 fill=red />
<circle onclick=closeMe() r=5 fill=blue cx=43 cy=5 />
</g>
相关文章:
- 为什么这在IE中的工作方式与在Firefox中不同
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 使用javascript存储变量的最安全方式
- RubyonRails——构建交互式接口应该朝哪个方向发展
- 以可优化的方式使用requirejs加载模板
- 在Highcharts中,我们可以通过任何方式在渲染图表之前获得plotWidth和plotHeight
- 任何方式使AJAX调用Gmail API,而无需通过JS库
- 为react组件传递道具的最佳方式
- 让Webpack管理Quirky AMD定义的最佳方式
- 在承诺链中处理早期回报的最佳方式
- 如何“;过滤器”;或者以其他方式重构该数据
- 您有更好的动态方式来缩短复杂的代码jquery吗
- 在ng重复循环中显示条件内容的最佳方式是什么
- 在phonegap中为android调用onload函数的最佳方式
- 如何创建更好的方式来维护基于我的代码访问的最后一个页面
- Javascript以不同的方式声明数字
- 在页面中包含交互式外部内容的最佳方式(JavaScript、AJAX、jQuery)
- 从浏览器中删除SVG元素的交互式方式