Issue with AnimeJS Motion Path causing element to deviate from desired SVG path

I'm attempting to use an SVG element and the AnimeJS library to make the orange marker follow the course of this RC car race track.

https://i.stack.imgur.com/8FKHC.png

Despite my efforts, I am encountering strange and undesirable outcomes.

At times, I observe the marker following a path that is offset from the course and not in the correct scale. Other times, although the marker is moving, it is not visible but I can see the scrollbars on my page adjusting, indicating its presence and movement.

When inspecting the browser's developer tools, I notice rapid changes in the element's styles - translateX, translateY, and rotate.

The code given shows the marker in motion. In the JSFiddle demo, if you carefully watch and wait for 9.49 seconds for each lap to complete, you may catch a brief glimpse of an orange ball which appears disconnected from the track and not correctly scaled.

What could be causing these issues? How can this functionality be achieved successfully? I am open to exploring alternative libraries or tools to accomplish this task.

JSFiddle

HTML

<section id="race-event-overview">
    [...]
    <div class="race-track-map">
        <svg class="race-track" viewBox="0 0 560 350" xmlns="http://www.w3.org/2000/svg" fill-rule="evenodd" clip-rule="evenodd"
            stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="1.5">
            <path class="track"
                d="M648.018 1019s-26.755-194 194-194H2618.98c220.76 0 194 194 194 194v527h-.24s10.84 139-112.49 139-112.49-139-112.49-139h-.24v-286s11.83-148-109.52-148H1413s-81-1.45-81 111c0 0-11.07 108 127... 
                
        </svg>
    </div>
</section>

SCSS:

#race-event-overview {
    position: relative;
    height: 500px;
    background: #242D33;

    .race-track-map {
        position: absolute;
        top: 75px;
        right: 0;

        svg {
            height: 350px;
            width: 560px;
        }
    }
}

JS:

var path = anime.path('.race-track .track');

anime({
    targets: '.race-track .ball',
    translateX: path('x'),
    translateY: path('y'),
    rotate: path('angle'),
    easing: 'linear',
    duration: 9490,
    loop: true
});

Answer №1

It seems like I may be a bit late to provide assistance for your needs, however:

In summary

The main issue lies in the fact that the transform origin behaves unpredictably in SVGs, and anime.js offers a straightforward solution. To resolve this problem, reposition the animated object (the circle) with attributes cx="0" and cy="0", and eliminate any transformations on the path.

Further details

A very informative article discussing SVG transforms can be found on CSS Tricks. The key takeaway is that all elements within an svg element have their transform origins at the upper left corner of the svg element.

When examining the transformations applied by anime.js to the animated element, you will notice they correspond directly to the values in the path's d attribute. These values represent distances in pixels from the origin (once again, the upper left corner of the svg element), but discrepancies arise when transformations are present. Feel free to explore Removing transforms in SVG files

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

What is the process for saving information to a database with JavaScript?

I am currently utilizing the Google Maps API for address translation, primarily through the use of a geocoder. I am interested in saving these results to a local database for future reference, as there are limitations on the total number and frequency of ...

Problems encountered with React Image Magnifiers while attempting to zoom in with Next.js

When I try to use the react-image-magnifiers plugin to make an image zoom in on hover, it works fine without next.js. However, when I integrate it with next.js, the zoom functionality does not work. Could there be an issue in my next.config.js file? This ...

Error: The function $(...).maxlength is not recognized - error in the maxlength plugin counter

I have been attempting to implement the JQuery maxlength() function in a <textarea>, but I keep encountering an error in the firefox console. This is the code snippet: <script type="text/JavaScript"> $(function () { // some jquery ...

Is it possible to pass a useState function to a component during its initialization?

Currently, I am utilizing the useState hook to effectively handle the rendering of components on the screen. My goal is to initialize it with a component while passing in the useState function to set the screen within the component. Below is my App.js fil ...

Auto Start Feature for jQuery Slider Function

Hey there, I currently have an image slider on my website that allows users to navigate through images by clicking on preview and next buttons. My query is: would it be possible to implement an auto start feature instead of having to click manually? Belo ...

Attempting to position items within a container

Currently, I am in the process of creating a list of job opportunities to be displayed on a side bar of our careers page. Each job listing should include the job title along with an apply button enclosed within a rounded grey box that allows for some spaci ...

External API data is shown in the browser console but appears as undefined on the page

Can you please lend me a helping hand? I am facing a critical issue while attempting to retrieve data from an external API using axios in NextJS (Reactjs)/TypeScript through getServerSideProps. The data fetching is successful, and the JSON is returned on t ...

Breaking down objects or arrays to extract specific values in React components

Some articles recommend using ES6 destructuring for React props & state as a best practice. For example: const { showModal, hideModal } = this.props; While I understand the benefits of cleaner code, I recently discussed with another developer who suggest ...

Offspring of the superior element resting above another element

I'm dealing with a unique situation involving the Z-INDEX property. Check out my HTML setup below. <div class="player"> <div class="player-line"> <div class="player-handle"></div> <!-- /.player-handle --> </d ...

What is the reason that object-fit does not affect the width or height?

I am facing an issue with an image in a div where the image is not resizing properly when using object-fit: contain. The width of the image remains unchanged, causing padding to appear around it. https://i.stack.imgur.com/D0wym.png Is there a way to make ...

Obtain the month, day, or year from a timestamp

Can anyone provide guidance on extracting a specific date format (date, month, day, year) from a timestamp? I am interested in implementing this feature within a view using Backbone JS. ...

What is the best choice for my CSS: creating a block or an element using BEM methodology

** According to the official BEM website ** Create a block: If a section of code can be reused and does not rely on other page components being implemented. Create an element: If a section of code cannot be used separately without the parent entity (the ...

Is there a way to programmatically access the title of a TitledPane in JavaFX without using CSS?

Is there a way to set the title of a TitledPane to bold in Java code without using CSS? I created a TitledPane and I want the title's font weight to be bold. This is what I tried: TitledPane Rootpane = new TitledPane(); Rootpane.setText("Options"); ...

Utilize a unique font exclusively for Internet Explorer

I am encountering a significant issue trying to utilize custom fonts in Internet Explorer. Currently, I have implemented a specific font using @font-face and it works perfectly in modern browsers but fails to render properly in IE. The method I am curren ...

Ways to access the properties of an HTML element with JQuery

Can anyone tell me how to extract the value from a specific HTML element using JQuery? For example: <span id="45463_test"></span> (the proper tags need to be used for the code to work correctly) Specifically, I am looking to retrieve the va ...

Iframe navigation tracking technology

Let's say I have an iframe element in my HTML document. <html> <body> This is my webpage <button> Button </button> <iframe src="www.example.com"></iframe> </body> </html> If a user clicks on links wi ...

Datatables stands out by emphasizing rows across all paginated pages

Encountering an issue with the Datatables plugin when attempting to highlight rows on paginated pages beyond the first one. In the JavaScript code below, you can see where I have commented out adding the class info to all rows. When this is done and you n ...

ensure that the element matches the height of its parent element

Allow me to illustrate the issue with a visual aid. In the provided image, you can observe that the checkmark is perfectly centered vertically in the one-line p element. However, as the text extends to two or three lines within the p element, the checkmar ...

Get rid of the folder from the URL using an <a> tag

I have both an English and French version of my website located at: *website.com/fr/index.php *website.com/index.php Currently, I have a direct link to switch between the two versions: -website.com/fr/index.php -website.com/index.php. However, I ...

Error 404 in Angular HTTP Request

I'm encountering a 404 error while attempting to send a post request, along with a 'possibly unhandled rejection' error. Given my limited experience with Angular, any advice would be greatly appreciated. I've gone through the documentat ...