Color value/position based on time calculation?

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
This question isn't strictly related to B4X, just something I'm working on that's not in B4X and I need to manually animate a couple of items and I can't find my old example snippets for it... :mad: I need to move an object and change it's color over a few seconds and I'm having an aneurism coming up with the formulas for x/y and color-percentages based on elapsed time (simple linear transitions, nothing fancy).

I know I've seen somewhere here in posts examples of both "ColorFromTime" and "PositionFromTime" functions used in the days before ".SetWhateverAnimated" was available and for the life of me I can't find either one after searching the forum for half-an-hour.

So, rather than continuing to bang my head against this particular wall, I figured I'd ask If anyone has an examples of those two routines in B4A, and maybe I can put this particular problem to bed before the weekend ;)

TIA!
 

emexes

Expert
Licensed User
Here's a starting point.

If it was something you were going to do a lot, then you could eventually write it as a class because there's obviously a lot of repetition here, but first let's get it working before the weekend is over. ?

B4X:
'set these parameters:

Dim Duration As Float = 3.5    'seconds

StartX = 123
EndX = 456
StartY = 789
EndY = 1011
StartRed = 23
EndRed = 34
StartGreen = 56
EndGreen = 78
StartBlue = 90
EndBlue = 12

'and do this initialization:

Dim XPerSecond As Float = (EndX - StartX) / Duration
Dim YPerSecond As Float = (EndY - StartY) / Duration
Dim RedPerSecond As Float = (EndRed - StartRed) / Duration
Dim GreenPerSecond As Float = (EndGreen - StartGreen) / Duration
Dim BluePerSecond As Float = (EndBlue - StartBlue) / Duration

Dim StartTime As Long = Now()

'and then for each frame:

Dim SecondsSinceStart = As Float (Now() - StartTime) / 1000    'assuming Now() is in milliseconds'

X = StartX + XPerSecond * SecondsSinceStart
Y = StartY +  YPerSecond * SecondsSinceStart
Red = StartRed +  RedPerSecond * SecondsSinceStart
Green = StartGreen +  GreenPerSecond * SecondsSinceStart
Blue = StartBlue +  BluePerSecond * SecondsSinceStart
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Here's a starting point.
Thank you @emexes, you a gentleperson and a scholar! :D That should get me where I'm going hopefully. I'll throw something together on Monday. I know @Erel had these routines in multiple old posts around this forum (pre-2014 even, I think) and I always liked his elegant small code blocks to do this, passing in duration, start, end, etc. I thought I'd bookmarked them and hadn't and I know I had snippets at one time, but I think they evaporated along with my 2-hard-drives-ago hard drive.

In any event, enjoy your weekend and thanks again! ?
 
Top