【发布时间】:2015-07-15 01:54:39
【问题描述】:
我正在使用 SnapSVG 库。我正在尝试将动态创建的 SVG 圆圈添加到 SVG 组中。每次单击链接时都会添加一个新圆圈(添加复合)。然后我尝试将新创建的圆圈推送到一个数组并将该数组传递给组(拖动)函数。我基本上想将所有的圆圈和矩形拖到一个组中。但它不起作用。这是我的代码...
<!DOCTYPE html>
<html>
<head>
<script data-require="jquery@*" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="snap.svg-min.js"></script>
</head>
<body>
<svg id="svg"></svg>
<div id="addComp">Add Composite</div>
<script>
// JavaScript Document
(function() {
var s = Snap("#svg");
// Construct Composite
function composite(y, i) {
var CirArray = [];
$("svg g").remove();
$("svg rect").remove();
$("svg circle").remove();
var square = s.rect(30, 40, y, 40);
console.log("Square:" + y);
square.attr({
fill: 'lightblue',
stroke: 'lightblue',
//strokeOpacity: .3,
//strokeWidth: 10
});
CirArray.push(square);
var k = 0;
for (z = 1; z <= i; z++) {
k = k + 45;
console.log("Circle:" + k);
var mycircle = s.circle(k, 120, 20);
mycircle.attr({
fill: 'coral',
stroke: 'coral',
strokeOpacity: .3,
strokeWidth: 10
});
CirArray.push(mycircle);
} // For loop end
drag.apply(null, CirArray);
} // Construct Composite End
// Group
function drag(CirArray) {
var tableset = s.group(CirArray);
for (p = 0; p < CirArray.length; p++) {
console.log(CirArray[p])
}
console.log(CirArray.length)
// Drag
var move = function(dx, dy) {
this.attr({
transform: this.data('origTransform') + (this.data('origTransform') ? "T" : "t") + [dx, dy]
});
}
var start = function() {
this.data('origTransform', this.transform().local);
}
var stop = function() {
console.log('finished dragging');
}
tableset.drag(move, start, stop);
} //Drag End
// Add Composite
var x = 0;
var clct = 0;
$("#addComp").click(function() {
x = x + 45;
clct = clct + 1;
composite(x, clct);
}); // Add Composite End
}()); // Iffe End
</script>
</body>
</html>
Visit this site and click on 'Add composite' link a couple of times to see it in action
非常感谢任何帮助....
谢谢
【问题讨论】:
标签: javascript jquery svg snap.svg