Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse of an objects distance

Tags:

javascript

If I have the object's var distance; set at 100. As it moves towards a distance of 0, I want the opacity to increase. How would I use that 100 incrementing down to be the reverse for the opacity? Is there math I could use to do this?

Would I just use a var count to count up from 0 to 1?

like image 322
Tom Avatar asked Nov 23 '25 17:11

Tom


1 Answers

You can use opacity = (100 - distance) / 100.

This way when distance is 100 opacity will be 0 and when distance is 0 opacity will be 1.

In general if you want a transition so that as x moves from xa to xb y correspondingly moves from ya to yb you can use

y = ya + (x - xa) * (yb - ya) / (xb - xa)
like image 72
6502 Avatar answered Nov 25 '25 06:11

6502