2012-02-16 9 views
16

WebサイトでインラインSVGをレンダリングし、そのSVGのテキストをWYSIWYG方式で追加および変更できるようにする必要があります。基本的にはsvg-editのようなものが必要です。しかし、私は完全にWYSIWYGエディタを必要としませんが、インラインテキスト編集部分だけです。私はsvg-editのソースコードを見てきましたが、その部分だけを抽出するのは非常に難しいようです。SVGでのインラインテキスト編集

私が探しているのは、インラインSVGテキスト編集を実装するための簡単な方法です(多分サードパーティのライブラリを使って)。私はすでに、フォーカスしているときにSVGテキストをHTMLテキスト入力に置き換えることを考えましたが、編集モードではSVGにレンダリングされるときと同じようにテキストをレンダリングする必要があります。

答えて

1

ここでは、テキストノードからテキストを取得および変更できる例を示します。テキストノードの代わりに編集可能なdivなどを入れて、divinnerHTMLというテキストノードを置き換えるJavaScript関数を記述することをお勧めします。

成功するとここに最終コードを投稿してください。

<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> 
10

私はあなたが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; 
    } 
}); 
+1

私は、httpなど、キャンセル/受け入れるように入力します/ ESC、それらを編集するテキストノードをクリックすることができるというように、これにいくつかのより多くの機能を追加しました: //jsfiddle.net/gordonwoodhull/undr1kx6/44/ – Gordon