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 ******************** -->
編集::
この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);
}