如何使用raphaël分隔两个svgs?

时间:2016-08-12 17:07:02

标签: javascript forms svg raphael

我是初学者。我可以得到我想要发生的行为,但不是发生在两个单独的SVG中,所有的行为都发生在'svg2'中,尽管我为'svg1'和'svg2'选择了相应的id(或者至少我认为我做了)

<!DOCTYPE html> 
<html lang="en">
<head> 
<meta charset="UTF-8" />
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.2.0/raphael-min.js"></script>
<script src="logic.js"></script>
<title>Forms and Concentric Circles</title>
</head> 
<body>
<h1>Forms and Concentric Circles</h1>

<div id="svg1"></div>

<div class="form">Add how many?
<input type="text" id="howmany" />
<button id="more">Add More</button></div>

<div id="svg2"></div>

<div class="form">
<button id="another">Add Another</button>
</div>

</body>
</html>


var paper;

disappear = function() {
this.remove();
}

spin = function(r) {
  angle = Math.random()*1440 - 720;
  initial = { 'transform': 'r0' }
  final = { 'transform': 'r' + angle}
  r.attr(initial);
  r.animate(final, 2000);
}

addMore = function() {
   rectNum = $('#howmany').val();
   for (step = 0; step < rectNum; step += 1) {
   x = Math.random() * 180;
   y = Math.random() * 180;
   r = paper.rect(x, y, 20, 20);
   filled = {
   'fill': '#ddf'
    }
    r.attr(filled);
    r.click(disappear);
    spin(r);
   }
}

radius = 10
morecircles = function() {
paper.circle(100, 100, radius);
radius = radius + 10;
}

setup = function() {
   paper = Raphael('svg1', 200, 200);
   $('#more').click(addMore);

   paper = Raphael('svg2', 200, 200);
   $('#another').click(morecircles);
}

$(document).ready(setup);

1 个答案:

答案 0 :(得分:0)

更改第二个svg的名称,例如

paper2.circle

并且在您的设置功能中它应该是

paper2.Raphael('svg2',200,200)