SVG中的内联文本编辑

Seb*_*ack 22 javascript svg

我在网站中渲染内联SVG,并且必须使用户能够以所见即所得的方式在该SVG中添加和修改文本.基本上我需要一些像svg-edit一样的东西.但是我不需要完全WYSIWYG编辑器,只需要内联文本编辑部分.我查看了svg-edit的源代码,似乎很难只提取它的那一部分.

因此,我正在寻找的是一种简单的方法(可能使用第三方库)来实现内联SVG文本编辑.我已经考虑过在聚焦时用HTML文本输入替换SVG文本,但是文本必须在编辑模式下呈现,就像在生成的SVG中呈现一样.

Jos*_*ost 14

我一直在寻找类似的东西,但没有找到任何东西,所以我和我的硕士论文合作伙伴决定编写我们自己的解决方案.目前它在Chrome中运行得相当好,在Firefox中也相当不错.

演示:http://engelfrost.github.io/svg-input-elements/

代码:https://github.com/engelfrost/svg-input-elements


joh*_*jik 13

无论你在SVG中点击什么,我都会创建一个可编辑的文本.最后一步是获取HTML文本并将其放在SVG元素中.

http://jsfiddle.net/brx3xm59/

代码如下:

var mousedownonelement = false;

window.getlocalmousecoord = function (svg, evt) {
    var pt = svg.createSVGPoint();
    pt.x = evt.clientX;
    pt.y = evt.clientY;
    var localpoint = pt.matrixTransform(svg.getScreenCTM().inverse());
    localpoint.x = Math.round(localpoint.x);
    localpoint.y = Math.round(localpoint.y);
    return localpoint;
};

window.createtext = function (localpoint, svg) {
    var myforeign = document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject')
    var textdiv = document.createElement("div");
    var textnode = document.createTextNode("Click to edit");
    textdiv.appendChild(textnode);
    textdiv.setAttribute("contentEditable", "true");
    textdiv.setAttribute("width", "auto");
    myforeign.setAttribute("width", "100%");
    myforeign.setAttribute("height", "100%");
    myforeign.classList.add("foreign"); //to make div fit text
    textdiv.classList.add("insideforeign"); //to make div fit text
    textdiv.addEventListener("mousedown", elementMousedown, false);
    myforeign.setAttributeNS(null, "transform", "translate(" + localpoint.x + " " + localpoint.y + ")");
    svg.appendChild(myforeign);
    myforeign.appendChild(textdiv);

};

function elementMousedown(evt) {
    mousedownonelement = true;
}


$(('#thesvg')).click(function (evt) {
    var svg = document.getElementById('thesvg');
    var localpoint = getlocalmousecoord(svg, evt);
    if (!mousedownonelement) {
        createtext(localpoint, svg);
    } else {
        mousedownonelement = false;
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 我为此添加了一些功能,例如能够单击文本节点进行编辑,输入/ esc以接受/取消等.http://jsfiddle.net/gordonwoodhull/undr1kx6/44/ (2认同)

pet*_*ter 0

这是一个可以从文本节点获取和更改文本的示例。我建议编写一个 JavaScript 函数,将可编辑的div或类似的东西放在文本节点的位置,并在保存时将文本节点替换innerHTMLdiv.

成功后,请在此处发布最终代码。

<html>
  <head>
  <script>
    function svgMod(){ 
      var circle1 = document.getElementById("circle1"); 
      circle1.style.fill="blue";
    } 
    function svgMod2(){ 
      var circle1 = document.getElementById("circle1"); 
      t1.textContent="New content";
    } 
  </script>
  </head>
  <body>
  <svg id="svg1" xmlns="http://www.w3.org/2000/svg" style="width: 800; height: 1000">
  <circle id="circle1" r="100" cx="134" cy="134" style="fill: red; stroke: blue; stroke-width: 2"/>
  <text id="t1" x="50" y="120" onclick="alert(t1.textContent)">Example SVG text 1</text>
  </svg>
  <button onclick=circle1.style.fill="yellow";>Click to change to yellow</button>
  <button onclick=javascript:svgMod();>Click to change to blue</button>
  <button onclick=javascript:svgMod2();>Click to change text</button>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)