javascript - I'm trying to get this code I found for draggable SVGs to work -
i found interesting code on petercollingridge.co.uk dragging svgs.
after while of trying work in project, decided try peter's code run in fiddle.
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="200"> <style> .draggable { cursor: move; } </style> <script type="text/ecmascript"> < ![cdata[ var selectedelement = 0; var currentx = 0; var currenty = 0; var currentmatrix = 0; function selectelement(evt) { selectedelement = evt.target; currentx = evt.clientx; currenty = evt.clienty; currentmatrix = selectedelement.getattributens(null, "transform").slice(7, -1).split(' '); (var = 0; < currentmatrix.length; i++) { currentmatrix[i] = parsefloat(currentmatrix[i]); } selectedelement.setattributens(null, "onmousemove", "moveelement(evt)"); selectedelement.setattributens(null, "onmouseout", "deselectelement(evt)"); selectedelement.setattributens(null, "onmouseup", "deselectelement(evt)"); } function moveelement(evt) { var dx = evt.clientx - currentx; var dy = evt.clienty - currenty; currentmatrix[4] += dx; currentmatrix[5] += dy; selectedelement.setattributens(null, "transform", "matrix(" + currentmatrix.join(' ') + ")"); currentx = evt.clientx; currenty = evt.clienty; } function deselectelement(evt) { if (selectedelement != 0) { selectedelement.removeattributens(null, "onmousemove"); selectedelement.removeattributens(null, "onmouseout"); selectedelement.removeattributens(null, "onmouseup"); selectedelement = 0; } } ]] > </script> <rect x="0.5" y="0.5" width="399" height="199" fill="none" stroke="black" /> <rect class="draggable" x="30" y="30" width="80" height="80" fill="blue" transform="matrix(1 0 0 1 25 20)" onmousedown="selectelement(evt)" /> <rect class="draggable" x="160" y="50" width="50" height="50" fill="green" transform="matrix(1 0 0 1 103 -25)" onmousedown="selectelement(evt)" /> </svg>
i'm still getting errors in project, being:
" uncaught syntaxerror: unexpected token <" , "uncaught referenceerror: selectelement not defined "
i read invisible characters causing first problem if copy/paste code, haven't found any.
thanks can offer.
like others said, remove cdata jumbo. here updated fiddle: https://jsfiddle.net/88pocqsr/1/
we've removed < ![cdata[
, ]]>
Comments
Post a Comment