Guide to animating an image along a unique path with JavaScript

I'm trying to animate an arrow along a specific path using CSS translate, but I'm facing a challenge.

The arrow should move downwards, then to the left, and finally upwards until it reaches the same horizontal level as its starting point but to the left of its initial position.

I managed to move it down as the first step. However, when I tried to apply all three translate methods, the arrow went directly to the final position without following the intended path. I attempted separating the translate methods into different functions and calling them sequentially, but the outcome remained unchanged.

Below is a snippet of the HTML:

<div>
<img src="img/arwTest.png" alt="Click to rotate" id="arrow">
<img src="img/moveTest_2.png" alt="demonstration" id="demo">
<p>Click anywhere to move the arrow along the path.</p>
</div>

And here are some relevant CSS styles:

#demo, #arrow {
display: block;
margin: 0 auto;
}

#demo {
height: 300px;
}

#arrow {
height: 40px;
position: absolute;
left: 50%;
}

p {
text-align: center;
padding: 20px;
}

This is the JavaScript code that attempts to animate the arrow:

const arrow = document.getElementById("arrow");
arrow.style.transform = `rotate(90deg)`;

addEventListener('click', () => {
arrow.style.transform = `translate(0px,200px)`;
arrow.style.transition = `2s`;
})

If you're interested in exploring the entire project, feel free to check it out here.

Answer №1

If you're searching for information on the @keyframes css rule and the animation property in CSS, you've come to the right place:

@keyframes motion {
  0.000% { left: 30%; top: 70%; }
  33.33% { left: 70%; top: 70%; }
  66.66% { left: 50%; top: 30%; }
  100.0% { left: 30%; top: 70%; }
}

html, body {
  position: absolute;
  width: 100%; height: 100%;
  left: 0; top: 0;
  margin: 0; padding: 0;
}

.point {
  
  position: absolute;
  width: 20px; height: 20px;
  margin-left: -10px; margin-top: -10px;
  border-radius: 100%;
  background-color: #500000;
  
  animation: motion 2000ms infinite linear;
  
}
<div class="point"></div>

Answer №2

If you're looking for a solution to achieve a specific movement in CSS, there's a new property that can help.

Introducing offset-path.

Check out the snippet below to see how it can be utilized for the desired effect you mentioned

Try hovering over the container to activate it

Remember, when seeking help on platforms like Stack Overflow, it's important to include an example of your code, even if it's not functioning properly!

#container {
  width: 400px;
  height: 150px;
  border: dotted 5px black;
  margin: 30px;
   
}

#motion-demo {
  width: 40px;
  height: 40px;
  background: cyan;
  offset-path: path('M400 0 v 150 h -400 v -150');
   offset-distance: 0%;
   transition: offset-distance 2s;
 }


#container:hover #motion-demo {
    offset-distance: 100%;
}
<div id="container">
<div id="motion-demo"></div>
</div>

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

Is it possible to have unique color tags in Material UI Autocomplete?

I'm currently diving into the world of material-ui and encountering some challenges that I could use help with. I am trying to incorporate multiple arrays into the autocomplete menu (e.g., options={top100Films, top100Shows}, but with the correct sy ...

Guide on assigning a value to a concealed type input using JavaScript

Currently, I am utilizing jsp technology and facing an issue where I need to establish a value from JavaScript to an HTML hidden type input so that I can transmit the data to another webpage. This is the part of my HTML code where I am encountering this c ...

Struggling to send API POST request using Next.js

I'm relatively new to backend development, as well as Next.js and TypeScript. I'm currently attempting to make a POST request to an API that will receive a formData object and use it to create a new listing. My approach involves utilizing Next.js ...

Is there a way to play back the recorded sound in my JavaScript project?

I recently completed a tutorial on audio recording in JavaScript where an array is used to store the recorded audio and convert it into a URL for playback. The setup includes a button with a function that both records and automatically plays the audio when ...

Guide on simulating a promise in a Karma unit test that is invoked within a function

Within my Angular controller, I have the following function that retrieves data from a promise and I need to test if it returns the expected result. function getName() { var name = ""; nameService.getName().then(function (data) { n ...

Understanding the Functionality of Object3D.userData in THREE.js

Can someone help me understand how the .userData property functions within the Object3D class? From what I gather, it's simply an empty object that is not utilized by any internal logic of Threejs. It serves as a secure way to store data that I want ...

Struggling to map JSON data (received from WCFRest) onto an HTML table

After creating a WCFRestful service that populates data in JSON format as shown below: {"GetEmployeesJSONResult":"[{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000},{\"Name\":\"Sumanth\",\"I ...

Show MQTT web channel information on an HTML webpage

To showcase data from an MQTT channel on an HTML page, I wrote a Node.js script as shown below: var mqtt = require('mqtt'); client = mqtt.createClient(1883, 'mqtt.beebotte.com', //Authenticate with your channel token, {username: &apos ...

A unique issue arises when custom geometry is registered in A-Frame but fails to render once added to a scene

I have developed custom functions to create non-hollow THREE.js geometries with phi length values that are less than 360. Now, I want to display them in an A-Frame scene (embedded within React). However, I am facing issues while trying to render them. Ini ...

Issue with Angular controller not refreshingalternatively:Angular

I just started reading an AngularJS book by O'Reilly and I encountered a problem with the first example. Instead of seeing "hello" as expected in place of "{{greeting.text}}", it displays exactly that. I have double-checked my angular linking and even ...

No defined parent for 'appendChild' operation

Seeking guidance on implementing a Solar System using THREE.js and encountering an issue: Error message: Cannot read property 'appendChild' of undefined Below is the current code snippet: <html> <head> <meta charse ...

One can only iterate through the type 'HTMLCollection' by utilizing the '--downlevelIteration' flag or setting a '--target' of 'es2015' or above

I'm currently working on developing a loader for my static grid. I've incorporated the react-shimmer-skeleton package source code, but I'm encountering issues with eslint in strict mode. You can find the respective repository file by followi ...

Using Behavior Subject for pagination in Angular with RxJS

Currently, I am creating a filtering system for a product list based on category IDs using the RXJS operator BehaviorSubject. However, I have encountered an issue with implementing infinite scrolling with Behavior Subject because I am unable to access the ...

Having trouble with passing props in React. Any suggestions on what I might be missing?

It seems like I am facing an issue with passing props in React, and for some reason, it's not functioning as expected. I'm a bit puzzled by the whole situation. Main Application Component import React from 'react' import Produc ...

Tips on creating horizontal scrolling for rows once they hit the maximum width

I need the content to scroll horizontally if it exceeds the maximum width. I have tried setting the width to 100%, using overflow-x:auto, and white-space: nowrap as suggested in other stackoverflow threads, but it doesn't work for Bootstrap columns. I ...

Sometimes the last column might not be visible in an HTML table within a collapsed Bootstrap element while using Chrome browser

I've encountered a strange issue when trying to wrap an HTML table inside a collapsing div element using Bootstrap. The last column is not consistently displayed in Chrome (44.0.2403.107 m) depending on the window width, but it works fine in Firefox. ...

Assign a unique identifier to the Angular mat-checkbox component

I need to attach an ID to an Angular material checkbox. I have an object called item. When I check the HTML code, it shows 'item.id + '-input'. However, when I bind the name, it works fine with just 'item.id'. Below is my current ...

Concealing jQueryUI tooltip upon clicking a "target=_blank" link

I have implemented jQuery to display tooltips for all links on my webpage that include a 'details' attribute. $(function() { $tooltip = $(document).tooltip({ show: false, track: true, ...

Ways to change an array of objects with similar named values

Here is an array of nested objects: var existingArray = [{ "content": { "host": { "name": "Mia" } }, }, { "content": { "host": { ...

The functionality of @babel/plugin-proposal-class-properties appears to be malfunctioning

I am currently working on a project with the following structure: /main-directory /applications /app — // Looking to import "project-b" here /node_modules /public /src |app.js |app.css |.babelrc |pack ...