Snap.SVG s.node.offsetLeft [Top] is undefined? -
in snap.svg need position of svg container left upper corner. needed coordinates with
s.mousemove(movefunc)
but relative position of html body. html document responsive. result useing
s.node.offsetleft
or
s.node.offsettop
is undefined. svg container id , use of
document.getelementbyid('usedid').offsetleft
or other offset... give 'undefined'. results independent of browser. far couldn't find usefull answer or example. hope help.
for completion ian's answer. html body ist centered viewport of browser , contents responsible. svg container should centered in body. mousemove event gives me correct coordinates relative viewport. if want example svg grafic element follow mouse or placed @ mousepointer need relative coordinates of svg container's upper left corner , not absolut coordinates x=0 , y=0. how can these coordinates? absolut positioned html element can needed offsets not svg.
sorry, tried jsfiddle unsuccessful. here the code used:
<!doctype html> <html> <head> <script src="snap.svg-min.js"></script> <title>test.svg</title> <style> body {max-width:62em;margin:0 auto;overflow-y:scroll;} main {background: #ddd;} svg {background:#eee;border:1px dotted #3983ab;width:100%;height:auto;} </style> </head> <body> <main> <h1>test responsiv svg</h1> <div style="text-align: center"> <svg id="svgout" style="max-width:500px" viewbox="0 0 500 250" ></svg> </div> <script> var s = snap('#svgout'); var c1 = s.circle(0,0,10).attr({fill: '#00f'}); function mausposi(event, x, y ) { c1.attr({cx: x, cy: y}); mc.attr({'text': x+'/'+y}); ol.attr({'text': s.node.offsetleft}); }; s.mousemove(mausposi); var ol = s.text(10,100, 'offset...: '); var mc = s.text(10,150, 'mousecoord:'); </script> </main> </body> </html>
for circle following mousecurser in correct way have substract coordinates of upper left corner of svg. mouse event return coordinates inside svg relativ body (main). element (circle) coordinates of upper point ist (0/0).
ian, found jsfiddle.net/xj4lj/36/ (yours?) s.node.offset... working if svg container remains left of browsers viewport. returned value in additional text gives undefined.
when there differences screen coordinates svg element, need transformation on points, svg coordinates space. can find out matrix given element.getscreenctm().
it's little bit complex, typically use plugin made, include @ top before create snap paper instance. create svg point, find inverse matrix of transformation screen svg paper, , transformation on points using that.
snap.plugin( function( snap, element, paper, global ) { element.prototype.getcursorpoint = function( x, y ) { var pt = this.paper.node.createsvgpoint(); pt.x = x; pt.y = y; return pt.matrixtransform( this.paper.node.getscreenctm().inverse()); } }); ... function mausposi(event, x, y ) { var cpt = s.getcursorpoint(x,y) c1.attr({cx: cpt.x, cy: cpt.y}); };
Comments
Post a Comment