با سلام خدمت کاربران گرامی سایت پی وی لرن و کاربرانی که علاقمند به دوره آموزش SVG هستند. در جلسه گذشته با Gradient در svg آشنا شدیم. در این جلسه قرار است به ایجاد واکنش گرایی در svg بپردازیم.
تصاویر svg را می توان نسبت به اقدامات کاربر، واکنش گرا و یا اصطلاحاً responsive نمود. به این فرآیند ایجاد واکنش گرایی در svg می نامیم.
svg از pointer events و keyboard events و document events پشتیبانی می کند.
به مثال زیر توجه کنید.
testSVG.htm
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <html> <title>SVG Interactivity</title> <body> <h1>Sample Interactivity</h1> <svg width="600" height="600"> <script type="text/JavaScript"> <![CDATA[ function showColor() { alert("Color of the Rectangle is: "+ document.getElementById("rect1").getAttributeNS(null,"fill")); } function showArea(event){ var width = parseFloat(event.target.getAttributeNS(null,"width")); var height = parseFloat(event.target.getAttributeNS(null,"height")); alert("Area of the rectangle is: " +width +"x"+ height); } function showRootChildrenCount() { alert("Total Children: "+document.documentElement.childNodes.length); } ]]> </script> <g> <text x="30" y="50" onClick="showColor()">Click me to show rectangle color.</text> <rect id="rect1" x="100" y="100" width="200" height="200" stroke="green" stroke-width="3" fill="red" onClick="showArea(event)"/> <text x="30" y="400" onClick="showRootChildrenCount()"> Click me to print child node count.</text> </g> </svg> </body> </html> |
textSVG.htm را در مرورگر وب Chrome باز کنید. شما می توانید از Chrome / Firefox / Opera برای مشاهده تصویر SVG به طور مستقیم و بدون هیچ افزونه ای استفاده نمایید.
اینترنت اکسپلورر ۹ و نسخه های بالا تر آن نیز از رندر تصویر SVG پشتیبانی می کند.
در این جلسه با آموزش واکنش گرایی در svg آشنا شویم.
در جلسه بعدی به پیوند سازی در svg خواهیم پرداخت.
با پی وی لرن همراه باشید.