Tips on managing the speed of animations for numerous SVG components with jQuery

Is there a way to customize the speed of multiple SVG elements individually? Currently, the script sets speed = .5; for all paths and lines under the parent group

<g id="World-On-Your-Plate">
.

To simplify and control the animations better, I've categorized the child elements of the SVG into separate groups: <g id="World">, <g id="On-Your">, and <g id="Plate">.

I'd like to set a different speed for the child group <g id="On-Your"> as speed = .3;, while maintaining the speed at .5 for the <g id="World"> and <g id="Plate"> groups.

HTML Code

<!-- SNIPPET CODE HERE -->

CSS Code

#World * {
    animation: letter-animation .7s linear forwards;
    animation-timing-function: ease-in-out;
}

#On-Your * {
    animation: letter-animation .5s linear forwards;
    animation-timing-function: ease-in-out;
}

#Plate * {
    animation: letter-animation .7s linear forwards;
    animation-timing-function: ease-in-out;
}

@keyframes letter-animation {
    100% {
        stroke-dashoffset: 0;
    }
}

JQuery Code

// JQuery code snippet here

CodePen: View on CodePen

Original Question: Link to Original Question

Linked Question 1: Second Linked Question

Linked Question 2: Third Linked Question

Answer №1

Instead of declaring the speed as a global variable, I have implemented a calculation for each shape to determine its speed.

To handle the delay, I have initiated a global variable called let delay = 0; and then increased the delay value within the forEach loop using: delay += Number(speed);

The speed attribute in the SVG is specified using data-speed, and for individual shapes, the speed is obtained with:

let speed = s.parentNode.dataset.speed;

// JavaScript Code
let svg = document.querySelector("svg")
let woyp = document.querySelector("#World-On-Your-Plate")
let shapes = woyp.querySelectorAll("g line, g path");

let delay = 0;

shapes.forEach((s, i) => {
    let totalLength = s.getTotalLength();
    let speed = s.parentNode.dataset.speed;
    delay += Number(speed);
    s.setAttribute("stroke-dasharray", totalLength);
    s.setAttribute("stroke-dashoffset", totalLength);
    s.setAttribute("style", `animation-duration:${speed}s;animation-delay:${delay}s`)
})
#World-On-Your-Plate * {
    animation-name: letter-animation;
    animation-fill-mode: forwards;
    animation-timing-function: ease-in-out;
}

@keyframes letter-animation {
    100% {
        stroke-dashoffset: 0;
    }
}

svg{width:90vh}
<svg viewBox="8 8 15 15">
<g id="World-On-Your-Plate" >
    <g id="World" stroke="#003668" stroke-width="1" fill="none" data-speed=".7">
        <!--W-->
        <line x1="8.427" y1="9.704" x2="9.47" y2="12.345" clip-path="url(#clip-path-47)" />
        <line x1="9.192" y1="12.345" x2="10.212" y2="9.704" clip-path="url(#clip-path-46)" />
        <line x1="9.331" y1="9.704" x2="10.374" y2="12.345" clip-path="url(#clip-path-45)" />
        <line x1="10.096" y1="12.345" x2="11.115" y2="9.704" clip-path="url(#clip-path-44)" />
       <!-- Additional Shapes -->
    </g>
    <g id="On-Your" stroke="#BDA484" stroke-width=".5" fill="none" data-speed=".1">
        <!--Shapes For 'On Your' section -->

    </g>
    <g id="Plate" stroke="#003668" stroke-width="1" fill="none" data-speed=".7">
        <!--Shapes For 'Plate' section -->

    </g>
</g>
  
</svg>

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

Replace all anchor tags containing href="#" with the Javascript function Void(0) using jQuery

Currently, I am attempting to update all anchor tags using href="#" and changing the value to JavaScript:Void(0) instead of just # running throughout the entire application. However, it seems that this change is being applied to all anchor tags, even tho ...

SwiperJS continuously applies additional right margin to every slide

My Swiper carousel seems to be adding an unnecessary 5px margin-right to each slide, resulting in the cards not fitting into one frame. https://i.sstatic.net/fwn5G.png I attempted to fix this by setting the margin-right to 0px for the .swiper-slide class ...

Using accent marks in AJAX to retrieve information from a database

I'm currently working on compiling a list that can be found at the following link: Within the php file, I have implemented meta tags to handle accents. However, when using ajax to display search results, entering accents such as ö,ü,ő etc in the s ...

There seems to be a issue with quotes in the comments for jQuery; everything else is functioning perfectly

When I try to post a comment with special characters like ' or ", it gets stored in the database successfully but the jQuery code fails to display it in the list. Any suggestions would be greatly appreciated. function feedbacksubmit () { // Display ...

How can we send data from several input fields using jQuery ajax?

I have several input fields, such as <p>Filter by age</p> <select class="filter-users"> <option value="under20">Under 20</option> <option value="20to40">20 to 40</option> </select> <p& ...

Is there a way to retrieve the parent window's domain in a child window (opened with window.open) when both windows are located on different

Can anyone offer assistance with cross-domain communication between windows? Looking to use window.open(); function. ...

Issue with A-frame Raycaster not functioning properly; specifically need to determine intersection point with a-sky

I am currently attempting to determine the coordinates where the ray caster intersects with the a-sky element. However, I am facing two main issues: 1) The ray caster is not visible even after adding showline:true 2) The intersection listener is never ...

Shape with an unusual rounded base edge

Recently, I've been exploring how to create a rectangle with a rounded bottom side. However, my research only revealed how to achieve a rounded side, not rounded corners. .figure { height: 400px; width: 200px; background-color: black; borde ...

Ways to repair the mouse hover transform scale effect (animation included)

I am currently facing an issue with my GridView that contains images. When I hover over the top of the image, it displays correctly, but when I move to the bottom, it does not show up. After some investigation, I suspect that there may be an overlay being ...

Tips for implementing a unique ID attribute in a dropdown menu using Laravel's Blade template system

When working with Laravel, I utilize {{ Form::select('state', array(<values>)); }} to generate a drop-down in the view. This results in a dropdown containing an array of values specified in the . The name attribute value is "state" due to i ...

Looking to change the date format from 24/05/2021 to 24/May/2021 using jQuery or JavaScript?

My client prefers not to use a date picker, but wants to be able to type dates directly into a textbox and have them automatically converted to the format 24/May/2021 as they leave the field. I am looking for a solution using either Javascript or jQuery. ...

leveraging struts tag prefixes alongside ajax technology

I'm completely lost when it comes to using AJAX and jQuery, but I'm determined to create a basic login web app with the Struts 2 framework. Here's the login page template I found online: <html> <head> <link href="css/style ...

Steps to make a multi-image rollover effect similar to Facebook albums

Is there a way to change multiple images within a Facebook album when hovering over just one image, rather than only being able to change one image at a time using the rollover effect? ...

Tips for retrieving the class name using jQuery

How can I access the class name of an element using jQuery when it is not known beforehand? <input type="checkbox" id="if you tick me" class="tickme" onClick="myfunction(this)"> <input type="checkbox" id="you tick me also" class="tickme"> fun ...

Align the Media center div within a column utilizing Bootstrap

I have a template in html with a media object containing a logo and text, both within a column of width 5. Currently, it is not centered and aligned to the left. I am looking for a way to centrally align the media block within the column, regardless of its ...

Display a success message in a div when the form is submitted

I am facing an issue with the submission of multiple forms on a page using AJAX. The problem is that when I submit a particular form, it displays success messages in all the divs instead of showing it only in the relevant div. I want each form to trigger ...

Creating dynamic dropdowns with Ajax and HTML on the code side

I have developed a script that generates a drop-down menu and updates the .box div with a new color and image. Below is the HTML & Java code: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <div> ...

Surprising outcomes when hosting my website on its domain

During the development of my website, I uploaded it to Cpanel once everything was finished. It functioned perfectly fine at that time. However, after making some changes and uploading it again, the fonts and videos on the live domain appear larger than i ...

Tips for optimizing the rendering speed of Vuetify's v-for loop

Hi, I'm new to Vue.js and Vuetify. I am trying to display 400 columns and 6 rows with column IDs (cid) and row IDs (rid), but the rendering speed is slow. Can anyone help me improve the performance of this loop? Thank you in advance! https://i.sstatic ...

Achieving an IE7 opacity background while keeping the text unaffected

This is the given HTML code structure (view in JS Fiddle) How can I adjust the background color to be 20% opaque white while keeping the text black? It needs to be compatible with IE7. <ul class="menu"> <li class="first expanded active-trail ...