Clicca sull’area bianca e genera un cerchio con colore e raggio casuale.
JavaScript può acquisire la posizione del mouse con evt.clientX, evt.clientY
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0"?> <svg xmlns="http://www.w3.org/2000/svg" width="640" height="480"> <script type="text/ecmascript"> <![CDATA[ function aggiungi(evt) { var contenitore=evt.target.parentNode; var x=evt.clientX; var y=evt.clientY; var cerchio=document.createElementNS("http://www.w3.org/2000/svg","circle") cerchio.setAttribute("cx", x); cerchio.setAttribute("cy", y); cerchio.setAttribute("r", Math.random()*25+5); var r=Math.round(Math.random()*256); var g=Math.round(Math.random()*256); var b=Math.round(Math.random()*256); cerchio.setAttribute("fill", "rgb("+r+","+g+","+b+")"); contenitore.appendChild(cerchio); } ]]> </script> <rect width="640" height="480" stroke="blue" stroke-width="3" fill="white" onclick="aggiungi(evt)" /> </svg> |