
SVG小球
一个小东西,来回弹动的小球,用SVG写的,很简单,原理就是用 line 画一个线,然后用定时器不断更改用 circle 画的蓝色小球的 X 和 Y 坐标,当坐标超出线的端点,再让他反方向跑。
html代码:
01 | < svg width = "500" height = "500" > |
03 | < circle cx = "10" cy = "10" stroke = "red" stroke-width = "2" r = "5" fill-opacity = "0" /> |
05 | < circle cx = "450" cy = "450" stroke = "red" stroke-width = "2" r = "5" fill-opacity = "0" /> |
07 | < line x1 = "10" y1 = "10" x2 = "450" y2 = "450" stroke = "#ccc" stroke-width = "2" /> |
09 | < circle id = "myCircle" cx = "10" cy = "10" stroke = "blue" stroke-width = "2" r = "3" fill = "blue" /> |
js代码:
02 | var oCircle = document.getElementById( "myCircle" ); |
11 | auto = setInterval(move2,5); |
13 | oCircle.setAttribute( "cx" ,i); |
14 | oCircle.setAttribute( "cy" ,i); |
22 | auto = setInterval(move,5); |
24 | oCircle.setAttribute( "cx" ,i); |
25 | oCircle.setAttribute( "cy" ,i); |
27 | auto = setInterval(move,5); |
部分素材资源来源网站,本站提供免费下载,如有侵权请联系站长马上删除!