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

  • SEARCH
  • Home
  • 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 7923459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:20:02+00:00 2026-06-03T17:20:02+00:00

I’ve done some poking about but the documentation seems a little sparse on the

  • 0

I’ve done some poking about but the documentation seems a little sparse on the subject, and one or two things aren’t adding up,

I’m trying to get a line to track an animated element, my design for this is to use the same animation variables as on the box element for the X1 co-ordinate of the line,

Javascript (latest jQuery, SVG Plugin + SVG Animation Plugin)

$(function(){
var balloons = [
$("#box1"),
$("#box2"),
$("#box3")
];

var lines = [
$("#line1"),
$("#line2"),
$("#line3")
];

function act(jqObj, lineX, speed, change) {
jqObj
.animate({'left': change}, speed)
.animate({'left': -change}, speed);

lineX
.animate({svgX1: change}, speed)
.animate({sgvX1: -change}, speed, function() {
    act(jqObj, lineX, speed, change);
});
};
for (i = 0; i < balloons.length; i++) {
var speed = 2000 + Math.random() * 501;
var change = 10 + Math.random() * 26;
act(balloons[i], lines[i], speed, change);
}
});

HTML/CSS

<html>
<head>

<title>Proof of Concept Page</title>
<style type="text/css">
 .html .body {position: absolute;}

.box {
 position: absolute;
        width:100px;
        height:100px;
        position: absolute;
        background-color:red;
}
#box1 {
margin-left:300px; margin-top:60px 
}
#box2 {
margin-left:500px; margin-top:20px 
}
#box3 {
margin-left:700px; margin-top:50px 
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.svg.js"></script>
<script type="text/javascript" src="jquery.svganim.js"></script>
<script type="text/javascript" src="main.js"></script>


</head>
<body>
<div  class="box" id="box1">Proj 1</div>
<div  class="box" id="box2">Proj 2</div>
<div  class="box" id="box3">Proj 3</div>

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    <line id="line1" x1="350" y1="160" x2="550" y2="500" style="stroke:rgb(255,0,0);stroke-width:2"/>
    <line id="line2" x1="550" y1="120" x2="550" y2="500" style="stroke:rgb(255,0,0);stroke-width:2"/>
    <line id="line3" x1="750" y1="150" x2="550" y2="500" style="stroke:rgb(255,0,0);stroke-width:2"/>
</svg>

</body></html>

There are a few problems here, firstly, the obvious one, which is that the current code sets the position of the X1 to the far left because I’m not telling to add to the original value.

The reason I’m not using ‘+=change’ is that ‘+=anything’ breaks the code for some reason, even a generic numerical value.

Finally, even if I just set the value of the first X1 animation to say 500, and the second to 0, only the first will fire, the second animation is being skipped. This effect is seen here too where the lines fly to the far left and then don’t move.

I’m sure this is partly my extremely amateur understanding of jQuery and Javascript, but I would appreciate any constructive help.

  • 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-03T17:20:03+00:00Added an answer on June 3, 2026 at 5:20 pm

    This was rather challenging in that I had not used svg before.

    After playing with it on and off for a couple of days, I ended up with this:

    var $boxes = $(".box"),//coded in HTML
        strings = [];//populated below with sgv lines
    
    $('#canvas').svg({
        onLoad: function(svg) {
            var g = svg.group({
                stroke: 'red',
                strokeWidth: 2
            });
            $boxes.each(function(i, el) { // make one string per box.
                strings.push(svg.line(g, parseInt($(el).css('left')), 18+parseInt($(el).css('top')), 50, 200));
            });
        }
    });
    
    //animate the boxes
    $boxes.each(function(i, el) {
        var speed = 2000 + Math.random() * 501,
            change = 10 + Math.random() * 26,
            jqObj = $(el),
            initial_left = parseInt(jqObj.css('left')) || 0;
        (function act() {
            jqObj.animate({
                'left': initial_left + change
            }, {
                duration: speed,
                step: function() { //move the string's top end to keep up with the box
                    $(strings[i]).animate({
                        svgX1: jqObj.css('left')
                    }, 0);
                }
            }).animate({
                'left': initial_left - change
            }, {
                duration: speed,
                step: function() { //move the string's top end to keep up with the box
                    $(strings[i]).animate({
                        svgX1: jqObj.css('left')
                    }, 0);
                },
                complete: act
            });
        })();
    });
    

    See DEMO.

    This is actually a hybrid solution with svg strings and non-svg text boxes. These elements are animated in such a way that each string appears to be attached to a text box.

    The code is not easy to follow and could no doubt be simplified. In the end, I was just pleased to get something working. Good education for me and I hope it’s useful to you .

    EDIT – explanatory notes:

    Your interpretation: Just about correct. I’m actually using an alternative form of animate, which works as follows, .animate(properties, options), where properties and options are both maps (javascript objects). This form of the method allows a step option to be specified – ie. an operation that will occur at each step of the animation. In this case, the operation of interest is changing the position of the top end of the balloon string.

    i and ‘el’: jQuery’s .each() methods offer a convenient way to iterate over the elements in an array, the DOM nodes in a jQuery container, or properties of a plain js object. Both forms of .each() accept a callback function which specifies the operation to perform at each iteration. In turn, the callback function accepts two parameters representing (for array and jQuery container) index and value, or (for plain js object) key and value. The .each() method calls the callback function internally and inserts the parameters once per iteration. When writing a callback, you can call the arguments what you like, and I called them i (short for index) and el (short for element).

    Line animation: The line actually DOES just move a tiny bit and stop. BUT, this operation is performed in the step callback, it is called once per step of the animation – ie. as many times as necessary until the animation is complete. Thus, the line keeps up with the object to which it is “glued”.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.