可我知道我可以衡量我的SVG尺寸的宽度和长度?从那里,我想限制和限制所有图像元素只能在SVG内移动。设置SVG边界
我的SVG的名字是“主”,如何设置弹跳区域,这样的图像时,到达侧它不会从该地区移出?
目前,如果我设置更多的图像出现在SVG,不知它会搬出SVG的。有没有办法检查它?
<!-- ******************** Animation ******************** -->
$(window).load(function(){
$(document).ready(function(e) {
var num = 0;
var interval;
$('#add').click(function(e) {
$('#box').append('<div class="predator" id="predator'+ num + '"><img src="Pictures/PondSpecies/anchovies.png"></div>');
$('#predator'+num).css({
left: randomRange(500,150),
top: randomRange(400,150)
});
if (interval)
clearInterval(interval);
interval = setInterval (function() {
for (var i=0; i<num; i++) {
$('#predator'+i).animate ({
left: '+=' + randomRange(-6,7),
top: '+=' + randomRange(-6,7)
}, 100);
}
}, 100);
num++;
});
$('#remove').click(function(e) {
num--;
$('#predator' + num).remove();
if (num == 0 && interval)
clearInterval(interval);
});
});
/* FUNCTIONS */
function randomRange(min, max) {
return Math.random() * (max-min) + min;
}
});
<!-- ******************** Animation ******************** -->
EDITED ::
我试图把在这3次检查,但不知何故,它不工作,检查......
function outOfBounds(point, boundary) {
return point.y > boundary.top
|| point.y < boundary.bottom + 200
|| point.x < boundary.getLeftBoundAt(point.y)+500
|| point.x > boundary.getRightBoundAt(point.y)+500;
}
function getLeftBoundAt(y) {
return this.slope * y + this.base + 300;
}
function getTopBoundAt(x) {
var segment = this.topSegmentAt(x);
return segment.origin.y + segment.slope * (x - segment.origin.x);
}
你确定你正在使用SVG动画吗?你提到的代码只是CSS动画使用jQuery方法'.animate()'... – balexandre