SVG点击实现动态放大的圆效果

分享一段代码实例,它利用svg实现了点击生成动态放大的圆效果。

代码实例如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落</title>
</head>
<body>
<svg id="svg" width="500px" height="500px" style="background-color: #f2f2f2"></svg>
<script>
var svg = document.getElementById("svg");
svg.onclick = function(event) {
  var circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
  circle.setAttribute("cx", event.offsetX);
  circle.setAttribute("cy", event.offsetY);
  circle.setAttribute("r", 10);
  circle.setAttribute("fill", randomColor());
  circle.setAttribute("opacity", 1);
  svg.appendChild(circle);
  var t = setInterval(function() {
    var r = parseInt(circle.getAttribute("r"));
    r++;
    circle.setAttribute("r", r);
    var opacity = parseFloat(circle.getAttribute("opacity"));
    opacity -= 0.005;
    circle.setAttribute("opacity", opacity);
    if (opacity <= 0) {
      circle.parentNode.removeChild(circle);
      clearInterval(t);
    }
  }, 10);
}
function randomColor() {
  var r = Math.floor(Math.random() * 256);
  var g = Math.floor(Math.random() * 256);
  var b = Math.floor(Math.random() * 256);
  return "rgb(" + r + "," + g + "," + b + ")";
}
</script>
</body>
</html>

原文地址:SVG点击实现动态放大的圆效果

主页地址:http://www.softwhy.com/

猜你喜欢

转载自www.cnblogs.com/softwhy/p/9120609.html
今日推荐