Animate the object in SVG with periodic movements

Illustrate once and move items every five seconds in the sequence shown from left to right. I have looked extensively but cannot find a method to draw outer boundaries and position objects close to them.

<animate xlink:href="#blue-rectangle"
         attributeName="x" 
         from="50"
         to="425" 
         dur="5s"
         begin="circ-anim.repeat(2)"
         fill="freeze" 
         id="rect-anim" />

References: https://css-tricks.com/guide-svg-animations-smil/

https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Basic_Shapes

https://developer.mozilla.org/en-US/docs/Web/SVG/Element/animateMotion

https://i.sstatic.net/JyqOu.png https://i.sstatic.net/89Wsb.pnghttps://i.sstatic.net/vXNmd.png

Answer №1

To achieve a continuous motion on the path, utilizing animateMotion is recommended. However, if you require intermittent pauses in the movement, incorporating javascript would be necessary. A comprehensive guide on using javascript to mimic animateMotion can be found in the book titled: Using SVG with CSS3 and HTML5: Vector Graphics for Web Design

In order to halt the rectangle at specific intervals, I leverage the timestamp from the requestAnimationFrame's callback function. In the following example, I stop the rectangle every 2 seconds for a duration of 1 second.

As the timestamp progresses continuously, I establish a way to log the moment when the motion ceases so that the animation can resume from that point. This involves setting the variable lastTime as the most recent time the rectangle was stationary.

Please refer to the comments provided within my code for further insights.

let rid;//the request animation frame id
let track = document.getElementById("track"),
  trackLength = track.getTotalLength(),//the total length of the track
  dur = 15000; //duration of one loop of track, in ms
 
let lastTime = 0;//last time when the rect has stopped
let flag = false; 

let interval = 5000;


function update(time) {
  rid = requestAnimationFrame(update);
    // to stop the rect every 5 seconds
    var deltaT = time%interval;
    //during 1 second
    if ...
svg{border:1px solid;}
path{fill:none; stroke:black;}
<svg viewBox = "0 0 150 200" width="200">
  <path id="track" d="M70,30 Q70,20 80,20L120,20 Q130,20 130,30L130,170.000 Q130,180 120,180L30,180 Q20,180 20,170L20,130 Q20,120 30,120L60,120 Q70,120 70,110Z" />
 <polygon id="rect" points="0,0 -10,0 -10,-10 0,-10 0,0" style="fill: #ff0000;"/> 
</svg>

UPDATE

Here's an alternative approach using animateMotion:

setInterval(()=>{ svg.pauseAnimations(); 
                  setTimeout(()=>{svg.unpauseAnimations()},1000)
                },2000);
svg{border:1px solid;}
path{fill:none; stroke:black;}
<svg id="svg" viewBox = "0 0 150 200" width="300">
  <path id="track" d="M70,30 Q70,20 80,20L120,20 Q130,20 130,30L130,170.000 Q130,180 120,180L30,180 Q20,180 20,170L20,130 Q20,120 30,120L60,120 Q70,120 70,110Z" />
  <polygon id="rect" points=...

UPDATE 2

Presented below is a demonstration of moving 5 blocks along a path. The key distinction lies in the begin attribute within the animateMotion.

All updates are...

setInterval(()=>{ svg.pauseAnimations(); 
                  setTimeout(()=>{svg.unpauseAnimations()},1500)
                },3000);
svg{border:1px solid;}
path{fill:none; stroke:black;}
<svg id="svg" viewBox = "30 0 360 450" width="300">
<defs>
<polygon id="theRect" points="0,0 20,0 20,20 0,20 0,0" style="fill: #ff0000;" />
</defs>

  <path id="track" d="M288.938,33 Q289,12 310,12L350,12 Q371,12 371,33L371,408 Q371,429 350,429L66,429 Q45,429 45,408L45,374 Q45,353 66,353L267,353 Q288,353 288.062,332Z" />
  
  
  <use xlink:href="#theRect">
  <animateMotion id="test" begin= "0s" dur="15s" repeatCount="infinite" rotate="auto" fill="freeze" >
    <mpath xlink:href= "#track"  />
    </animate... 

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

Angular 7 - Customize Mat-select-value color depending on a specific condition

One issue I am facing is changing the color of a selected value in a mat select based on a condition. This change should be visually apparent to the user. Although accessing mat-select-value from the styles is possible, implementing a condition using ng-cl ...

How to create a dynamic height grid using Bootstrap?

Seeking assistance with implementing the layout shown in the image below using bootstrap 4. I am facing an issue with adjusting the height of div 1 and 2 dynamically based on the content in div 3. Can anyone provide some guidance? https://i.sstatic.net/mk ...

Ensure the table row remains on one page when printing or viewing the print preview

I'm currently dealing with an html report that contains a table, which can be seen here http://jsfiddle.net/fhwoo3rg/2/embedded/result/ The issue arises when trying to print the report, as it breaks in the middle of a table row like this: https://i ...

Retrieve the scope object using angular.element and the $ID parameter

In order to transfer data from an angular application to scripts that operate outside of angular, I am seeking a solution since I cannot modify the code of the angular app. By utilizing Angular Batarang and NG-Inspector extensions in Chrome, I have identi ...

Enabling HTML elements in a postback operation

In my user interface, there is a text box where users can input text containing HTML markup. Whenever the page triggers a postback action, it results in an error (500) as the parser interprets the HTML code in the textbox as a hacking attempt. I vaguely ...

PHP: Retrieve and store XML data for one-time use within a session

I have implemented Simplexml within a class to load specific nodes from my xml file into html. <?xml version="1.0" encoding="UTF-8"?> <note> <item> <title>Reminder</title> </item> <item> ...

Beautiful ExpressionChangedAfterItHasBeenCheckedError

I need your help Input field where I can enter a Student email or IAM, which will be added to a string array List displaying all the students I have added, using a for loop as shown below Delete button to remove a student from the array The list has a sp ...

barchart rendered in SVG without the use of any external libraries

Creating a stacked barchart with SVG and HTML without relying on any third-party library has been quite a challenge. Despite searching extensively online, I have not come across a single resource that demonstrates how to build a stacked bar chart using pla ...

Styling certain UL and LI elements with CSS properties

Greetings everyone! I constantly struggle when it comes to making my CSS cooperate. I have some code that involves using ul and li tags. However, I only want to apply that styling to specific sections of my HTML code rather than all instances of the ul and ...

Uploading images using PHP and renaming them

I am in the process of creating a PHP code that will allow me to upload a file name to a database. My goal is to initially upload just the file name so that I can easily retrieve it later on. Below is the code snippet that I have been working on: HTML code ...

Adjusting the position of an iframe with a YouTube video when the window is resized

I'm struggling with resizing and moving a YouTube video embedded using the Youtube API. I'm not a web designer, so I'm facing some difficulties. When trying to resize the window, I can't seem to move or resize the video within an iframe ...

td:before combined with vertical-align:middle

To achieve a table with square cells, I implemented the following CSS: table { width: 100%; table-layout: fixed; } td { text-align: center; vertical-align: middle; } td:before { content: ''; padding-top: 100%; float: ...

Issue in Ionic 3 where ion-list does not scroll vertically on iOS devices when ion-items are dynamically generated using ngFor within the ion-list

When using Ionic v3 to develop a hybrid app, I encountered an issue where ion-items within an ion-list generated using *ngFor are not scrollable on iOS devices. Strangely, this problem does not occur on Android devices. Here is the HTML code snippet: < ...

Display a full-screen image and align it horizontally from the center using CSS styling

When I fill a screen with an image using the following code: width:auto; height:100%; The image fills the screen vertically and almost entirely horizontally, but it gets cropped off on the right side. Is there a way to make the image float centered on th ...

Ways to customize the default home icon

My current theme is Redmond, but I would like to change the color of the home icon in the breadcrumb. To achieve this, I have included the hot-sneaks theme in the pom.xml file: <dependency> <groupId>org.primefaces.themes</groupId&g ...

Creating an HTML form that dynamically populates a second select dropdown from a MySQL database, depending on the value selected

How can I dynamically populate the second select input in my HTML Form based on the value selected in the first select input? <form action="hidden/add_training.php"> <select name="race_type" id="race_ ...

When a user clicks on buttons other than the submit button, the input field redirects to another page

A helpful user named Vittore assisted me in displaying the first 5 list items of the unordered list and implementing page navigation with next and previous buttons flawlessly. The issue arises with an input field, which functions correctly by navigating ...

Troubleshooting the Inline-Block Problem with Jquery Image Bubble Display

Recently, I've been exploring ways to eliminate the padding that is creating space between images before they align correctly. The culprit causing the padding seems to be the display: inline-block property. While it was suggested to have all the li t ...

CSS: Specify child element to have a full width of 100%, but appear only 50% wide

Looking at this page, it seems that the header photo is not displaying full width. #wrap { width: 990px; } #content-wrap { display: flex; } .image-header { display: block; width: 100%; } .image-header img { width: 100%; height: auto; } .cont ...

Using GeoIP2() in Django models to retrieve data saved from an IP address call and present it in my HTML

I have developed a function that extracts the IP address and saves information from the city_data in GeoIP2(). My goal is to retrieve the Latitude and longitude from the city_data and showcase it on my HTML page. The issue I am facing is the inability to ...