There are probably 2 steps to your challenge.
The first would be the actual math of it, something which is probably covered in many places, like here.
The second issue is applying that in Construct blocks or Javascript, in this case I'd suggest fiddling around with the engine, also looking at some basic tutorials so you get the grasp of the task.
I could give you straight up a built project, but I don't feel neither me nor you would gain anything from it in the end. Maybe I'm wrong, sorry if I am.
I'd be glad to answer any specific question you have while you build it though.