Create an animated sequence of dots connecting together in a dotted path, with each

Currently, I am utilizing the most recent edition of Snap.svg to sketch and animate a path within an SVG:

var s = Snap('#svg');
var getPath = s.path('M15 15L115 115');
var pathLength = getPath.getTotalLength();
getPath.attr({
  stroke: '#000',
  strokeWidth: 5,
  strokeDasharray: pathLength + ' ' + pathLength,
  strokeDashoffset: pathLength,
  strokeLinecap: 'round'
}).animate({
  strokeDashoffset: 0
}, 1500);

Although it is functioning correctly (as evident in this demonstration), my objective is to transform it into a dotted line with each dot animated sequentially.

To illustrate the desired appearance, I created a simple prototype using circles (available for viewing here), but ideally, I would like to implement this effect using a custom path.

In essence, I am seeking a method to animate a complex dotted path; achieving a similar effect as dots along a path or custom attributes on a path.

Answer №1

When working with stroke-dasharray as an array of values, it is possible to keep the stroke-dashoffset at 0 and continuously update the stroke-dasharray until reaching the end of the line. While the following example may not be perfectly smooth or optimized, it demonstrates this concept:

var s = Snap('#svg');
var getPath = s.path('M15 15L115 115');
var pathLength = getPath.getTotalLength();

var perc = 0;
var dotLength = 5;
var gapLength = 4;

getPath.attr({
    stroke: '#000',
    strokeWidth: 1,
    strokeDasharray: 0,
    strokeDashoffset: 0,
    strokeLinecap: 'round'
});

function updateLine(){
 perc +=1;
    if(perc>100){
        perc = 100;
    }
var visibleLength = pathLength*perc/100;
var drawnLength = 0;
    var cssValue = '';
    while(drawnLength < visibleLength){
     drawnLength += dotLength;
        if(drawnLength < visibleLength){
            cssValue += dotLength+ ' ';
            drawnLength += gapLength;
            if(drawnLength < visibleLength){
                cssValue += gapLength+ ' ';
            }
        }else{
            cssValue += (visibleLength + dotLength - drawnLength)+ ' ';
        }
    }
    cssValue += pathLength;
    if (perc<100){
     setTimeout(updateLine, 100);
    }
    getPath.attr({
        strokeDasharray: cssValue
    });
}

updateLine();

http://jsfiddle.net/EEe69/7/

If you prefer to exclude the gaps from the animation, simply subtract their lengths from the total path length.

Answer №2

Have you considered utilizing d3 for this task?

I came across a similar example that involves a dotted line and responds to mouse movements. While I tried implementing a timed function to achieve your desired outcome, I believe with some tinkering, you'll be able to crack it yourself :)

Take a look at this section and try modifying it to create a specific path instead of relying on d3.mouse:

d3.timer(function(step) {
    var svgupdate = d3.select("body").select("svg")
        .on("mousemove", function() { var point = d3.mouse(this); customPath(point); });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The functionality of the bootstrap Dropdown multiple select feature is experiencing issues with the Onchange

Creating a Bootstrap Multiple Select Drop Down with dynamically retrieved options from a Database: <select size="3" name="p" id="p" class="dis_tab" multiple> <?php echo "<option>". $row['abc'] ."</option>"; //Fetching option ...

Tips for fixing the error "TypeError: result.select is not a function" in a Node.JS, Express JS, and MongoDB application

I'm currently working on developing a store API. My goal is to filter the results based on the field from req.query and select. Everything seems to be functioning properly except for the select part. Error: TypeError: result.select is not a function ...

Incrementing a textbox using Javascript/jQuery in ASP.Net

We are currently exploring the use of a Javascript incrementer and decrementer with an ASP.Net textbox to adjust numerical values. The initial setting for the textbox is 0. By adding two divs that trigger Javascript functions, we are able to successfully ...

No matter the way I input the URL in the AJAX call, the response always comes back as successful

I ran into an issue with my ajax request. It was supposed to be a GET request that returned some data, but no matter how I configured the URL, it always gave a success response even when it didn't actually succeed. var id = 5; $.ajax({ type: ...

Grab the URL from React Router

I need assistance with writing a function that updates the current location to match the route of a clicked button. Currently, the code is returning the URL of the page where the button was clicked, not the path related to the button itself. Are there an ...

Error: The method api.createContextKey is not defined while running the Next.js development server

I am currently facing an issue while attempting to start the development server for my Next.js project. The problem arises when I try to incorporate MUI into my project, resulting in these specific errors. The error message points to a TypeError related to ...

Issue with receiving API response in AngularJS

Currently, my goal is to establish a simple API response using AngularJS and the CXF-JAXRS library in an OSGi environment (KARAF). The challenge lies in retrieving the information sent back from the REST service even though the logs indicate a successful c ...

How do I utilize Protractor to target a specific div element with a distinctive class and verify its visibility on the page?

Below is the view.html for a unique class: <div class="appExperience small-tile"> </div> This is the code snippet that I attempted to use: var displayedTile = element(by.css('.appExperience small-tile')); expect(displayedTile.i ...

Issues with login validation in HTML when utilizing JSON and PHP

While creating a login form in HTML using JSON and PHP, I encountered an issue where the if statements in the success function were not working properly. However, the beforeSend and error functions are functioning as expected. Can someone assist me in iden ...

Tips for binding to a single input box within an ngFor loop

Can anyone lend a hand with some code? I'm working on a straightforward table using ngFor, but I'm facing an issue with input binding. The problem is that all the input fields generated by ngFor display the same value when typing. How can I preve ...

Sizing of Info Windows for Google Map Markers

I've been trying to create a Google map displaying multiple locations, but I can't seem to figure out how to adjust the size of the infowindow for each marker. I've spent several hours on this with no luck. Can anyone show me where in this f ...

Creating a chat interface with chat bubbles in React JS can be done by implementing components such as Message

Having some JSON data stored in dummyData, I am seeking guidance on how to position chat bubbles on the left and right based on the direction. My framework of choice is Material UI along with the context API. The attached image serves as a visual reference ...

Issue with data entry: unable to enter the letter 'S' in search field

This bug has been a real challenge for me. It's strange, but I'm unable to type the letter "S" into the search input field. Oddly enough, the keyboard seems to be functioning properly. Please find the sandbox below for reference: https://codes ...

Trouble connecting: MongoDB Node.js client hangs during connection attempt

The node-mongodb-native for node.js seems to be causing a hang when using MongoClient.connect(...), while the mongodb-client (shell command line) works fine in the terminal. Any ideas on what might be causing this issue? var MongoClient = require('mo ...

The declaration file for the module 'react-tilt' was not located

After following the installation and import steps for react-tilt as outlined in this guide, I encountered the following error message and am unable to utilize it. I'm uncertain about what the message implies. Is there anyone who can help me resolve th ...

Data retrieval issue with ng-table displaying information queried from HTTP request

I have been attempting to create an ng-table using ajax data with angularjs and rails. Despite successfully retrieving the data through an http get request, I am encountering difficulties appending it to my table as it always displays no records. Below is ...

Switch between class and slider photos

I am currently having an issue toggling the class. Here is the code I am working with: http://jsfiddle.net/h1x52v5b/2/ The problem arises when trying to remove a class from the first child of Ul. I initially set it up like this: var len=titles.length, ...

Selecting the right <select> option in Vue 2 once the data has been retrieved from the database

In my current Vue 2 project, I am working on a small blog. The structure involves having a separate component for the post form, where one of the form inputs is a select field for the post's category. This select field should be populated with categor ...

Issue with displaying dates correctly on Chart.js and axis labels not being rendered as expected

I'm currently facing an issue with labeling the axes on my chart using chart.js v3.9.1. Despite following the documentation, the labels are not appearing as expected (see image here: https://i.sstatic.net/wCod7.png health_hub_tracker.html: {% extends ...

Overlap of columns within a nested flexbox arrangement

While working with React and Styled Components, I encountered a problem of elements overlapping each other: https://i.stack.imgur.com/RuCYu.png There are a few instances of overlap, including: The padding below the <ul> element is colliding with ...