Jump to content

Infinity Loop between two values


tiscavalcanti

Recommended Posts

I need to make a code like this:

Initial value 11

Final value 33

I want to insert the code on the property time (that changes) of the program I use

So I want the code to interpolate through the time between numbers 11 and 33, meaning this code will move 22 seconds in time

I want that when the code results in the final value, that is, arrive at the value 33
It is reinitiated, thus causing an infinite loop. Going from 11 to 33 in 22 seconds and then repeating on and on.

There is a codebase that may allow time to be used in a dynamic way:

linear (t, tMin, tMax, value1, value2)

There it is (time, start time, end time, initial value, final value)

Maybe this code is useful ... I do not know. It is an interpolation code

Edited by tiscavalcanti
Link to comment
Share on other sites

You need to learn two things:

  1. Linear interpolation
  2. Timing in Javascript

Linear interpolation is described here with some examples of function implementations: https://en.wikipedia.org/wiki/Linear_interpolation#Programming_language_support

The time t in linear interpolation is a value from 0 to 1. To get this value, you just divide the amount of time that has passed since the animation started by the total duration of the animation. In pseudo-code, something like this:

t = (currentTime - startTime) / (endTime - startTime)

When the animation starts, you need to store the current time in a variable startTime to use it in the formula described above.

In Javascript, when using setTimeout or setInterval, you get the startTime and currentTime values from the Date object's getTime() method. If you're using requestAnimationFrame, see Mozilla's documentation, the current time is given as a parameter in the callback function. When using requestAnimationFrame you can get the startTime value using performance.now() right before the animation begins.

 

  • Like 1
Link to comment
Share on other sites

7 hours ago, Ingolme said:

You need to learn two things:

  1. Linear interpolation
  2. Timing in Javascript

Linear interpolation is described here with some examples of function implementations: https://en.wikipedia.org/wiki/Linear_interpolation#Programming_language_support

The time t in linear interpolation is a value from 0 to 1. To get this value, you just divide the amount of time that has passed since the animation started by the total duration of the animation. In pseudo-code, something like this:


t = (currentTime - startTime) / (endTime - startTime)

When the animation starts, you need to store the current time in a variable startTime to use it in the formula described above.

In Javascript, when using setTimeout or setInterval, you get the startTime and currentTime values from the Date object's getTime() method. If you're using requestAnimationFrame, see Mozilla's documentation, the current time is given as a parameter in the callback function. When using requestAnimationFrame you can get the startTime value using performance.now() right before the animation begins.

 

Your information is quite useful, I will delve into the links, and in fact, I use video program that uses expressions based on JS ...

Look, I just figured out a way to sort this out ... maybe it'll work ...

Do you know how I can make a variable go from 0 to 22, and when it reaches 22, return to 0?

Type a slide inside the number 22, that is, from 0 to 22, it results in 22, and from 22 to 44 it also results in 22, as if the variable was reset every time it was 22, then 23 would be 1 , and 45, 1 also, and so on.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...