Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make dragging SVG elements in Chrome with jQuery work without getting offsets? (Firefox is OK)
    primarykey
    data
    text
    <p>I am trying to write some code that allows SVG elements to be dragged around. This is pretty easy with jQuery, jQuery UI and jQuery SVG, and works fine in Firefox, but in Chrome when I drag the SVG element, an offset is apparently added to its coordinates. I can't see anything I'm doing wrong, or discover any known bug in this area.</p> <p>I've constructed a small example that illustrates the problem:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Foo&lt;/title&gt; &lt;style type="text/css"&gt; &lt;/style&gt; &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://keith-wood.name/js/jquery.svg.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(function() { var svgOnLoad = function () { var svg = $('#canvas').svg('get'); $('#piece') .draggable() .bind('drag', function (event, ui) { // Update transform manually, since top/left style props don't work on SVG var t = event.target.transform.baseVal; if (t.numberOfItems == 0) { t.appendItem(svg.root().createSVGTransform()); } t.getItem(0).setTranslate(ui.position.left, ui.position.top); }); } $('#canvas').svg({loadURL: "foo.svg", onLoad: svgOnLoad}); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="canvas" style="width: 100%; height: 100%;"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>where foo.svg is just:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="450" height="450"&gt; &lt;rect id="piece" width="50" height="50" x="100" y="100" /&gt; &lt;/svg&gt; </code></pre> <p>An online version can be found at:</p> <p><a href="http://jsfiddle.net/rrthomas/v477X/2/" rel="nofollow">http://jsfiddle.net/rrthomas/v477X/2/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload