Create and drag circles in SVG
Tested on Firefox 3 beta 4.
file: makeDragDrop.svg
1 2 <svg xmlns="http://www.w3.org/2000/svg" width="100%" 3 xmlns:xlink="http://www.w3.org/1999/xlink" > 4 <script><![CDATA[ 5 var xmlns="http://www.w3.org/2000/svg" 6 xlink="http://www.w3.org/1999/xlink" 7 Root=document.documentElement 8 standardize(Root) 9 10 function standardize(R){ 11 var Attr={ 12 "onmouseup":"add(evt)", 13 "onmousedown":"grab(evt)", 14 "onmousemove":null, 15 "onmouseover":"hilight(evt)", 16 "onmouseout":"hilight(evt)" 17 } 18 assignAttr(R,Attr) 19 } 20 function hilight(evt){ 21 var T=evt.target 22 if (T.nodeName=="rect") return 23 if (evt.type=="mouseover") T.setAttributeNS(null,"stroke-opacity",1) 24 else T.setAttributeNS(null,"stroke-opacity",.5) 25 } 26 function add(evt){ 27 if (evt.target.nodeName!="rect") return 28 var C=document.createElementNS(xmlns,"circle") 29 var stroke=Color() 30 var rad=10+Math.random()*50 31 var Attr={ 32 r:rad, 33 cx:evt.clientX, 34 cy:evt.clientY, 35 "fill": Color(), 36 "fill-opacity":.75, 37 "stroke": stroke, 38 "stroke-opacity":.5, 39 "id":stroke, 40 "stroke-width":10+Math.random()*(55-rad) 41 } 42 assignAttr(C,Attr) 43 Root.appendChild(C) 44 } 45 function grab(evt){ 46 var O=evt.target 47 if (evt.target.nodeName=="rect") return 48 var Attr={ 49 "onmousemove":"slide(evt,'"+O.id+"')", 50 "onmouseup":"standardize(Root)" 51 } 52 assignAttr(Root,Attr) 53 } 54 function slide(evt,id){ 55 var o=document.getElementById(id) 56 var c=""; if (o.nodeName=="circle") c="c" 57 o.setAttributeNS(null, c+"x", evt.clientX) 58 o.setAttributeNS(null, c+"y", evt.clientY) 59 } 60 function assignAttr(O,A){ 61 for (i in A) O.setAttributeNS(null,i, A[i]) 62 } 63 function Color(){ 64 return "rgb("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+")" 65 } 66 ]]> 67 </script> 68 <rect width="100%" height="100%" fill="white"/> 69 <text font-size="12pt" x="50" y="20" id="t1">Click something to move it</text> 70 <text font-size="12pt" x="80" y="40" id="t2">Click nothing to add something</text> 71 </svg>
Source file copied from http://srufaculty.sru.edu/david.dailey/svg/makeDragDrop.svg