Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:00:37+00:00 2026-06-04T17:00:37+00:00

what i am trying to do here, is that, I add a small image

  • 0

what i am trying to do here, is that, I add a small image and it will be moving around the body.. But the problem is, i wanted the small image to be moving ard in the svg box.. I tried to take the SVG ID and let it append the image.. It will still move out of the svg box.. I wonder, if i missed out some codings?

$(window).load(function(){
$(document).ready(function(e) {
var num = 0;
var interval;

/*var svg = d3.select("main").append("svg:svg")
.attr("width", w)
.attr("height", h);
*/



$('#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(-11,11),
top: '+=' + randomRange(-11,11)


s /* Extra */
//if (x < 0 || x > w) SVG.vx *= -1;
//if (y < 0 || y > h) SVG.vy *= -1;



}, 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;
}
});

HTML body:

        <div id="box">
    <SVG xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink" id="main" style="border:solid 1px #000000;width:515;height:474;">
        <image xlink:href="Pictures/PondEnvironment/pond.png" x="0" y="0" width="513" height="474" />
</SVG>
      </div>

Many thanks in advance for helping!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:00:38+00:00Added an answer on June 4, 2026 at 5:00 pm

    If you want to keep your predators inside the SVG when animating, you’ll need to check their new positions each time. This fiddle should help:

    http://jsfiddle.net/DJg36/18/

    Also, I don’t think your animations were finishing completely, meaning they were starting to queue, possibly causing some confusions, so just to be sure, you should call stop() before animating again.

    So something like this is needed:

    $('#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)
            });
    
            var containerWidth = $("svg").width() - 25; // Minus the width of the img
            var containerHeight = $("svg").height() - 25; // Minus the width of the img
    
            if (interval) clearInterval(interval);
            interval = setInterval(function() {
    
                for (var i = 0; i < num; i++) {
                    var randomLeft = randomRange(-11, 11);
                    var randomTop = randomRange(-11, 11);
    
                    var predator = $('#predator' + i);
                    var predatorLeft = parseInt(predator.css("left"));
                    var predatorTop = parseInt(predator.css("top"));
    
    
                    if (predatorLeft + randomLeft <= 0) randomLeft = 11;
                    if (predatorLeft + randomLeft >= containerWidth) randomLeft = -11;
    
                    if (predatorTop + randomTop <= 0) randomTop = 11;
                    if (predatorTop + randomTop >= containerHeight) randomTop = -11;
    
                    predator.stop();
                    predator.animate({
                        left: '+=' + randomLeft,
                        top: '+=' + randomTop
                    }, 100);
                }
            }, 100);
            num++;
        });
    

    Hope this helps!

    Andy

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the problem that I am trying to solve. I have two folders
So here's the problem : I'm trying to make a service that starts another
I am trying to add a small image in a row of my ExpandableList.
So I'm developing a small project with Cocos2Dx but I'm trying to add Bluetooth
Here's the problem: I'm trying to make a small reporting tool. So... I have
Here is my problem. I am trying to write a small simple game engine
I have an example here that replicates what I am trying to accomplish. As
I have a page going here that uses jQuery: http://treethink.com/services What I am trying
Trying to use code from here Im trying to incorporate both classes so that
Here is the program that I am trying to compile #include<stdio.h> int main() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.