This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
Carl Bl
Posted Jun 13 - Read on Facebook

Hello guys! Beginner here !
Took some time to try loops and arrays with simple distance calculation. Could you tell me if my code could be optimised ? like merging functions into each others, or simplifying. This would help me to check if I did it the best way. Thanks for your time ! 😎


Eelco Lempsink

Code is looking good, Carl! It’s always possible to shuffle code around to make a shorter / clearer, but it’s best to do that when the need arises (or, if you can look into the future, just before that).

One thing to be aware of is the use of ‘state’, in your case, you save the distances in `allDist`. This could be calculated on the fly when needed and generally such an approach will lead to the most flexible code. Having a cache (saved pre-calculated values) is fine if 1) the input (points) never change, or 2) you will use it more than once (which I assume you’re planning to do) and you take care to update the values when needed.

Carl Bl

Hello Eelco Lempsink ! Thank you for your answer. I get the optimisation of the code depends on the result you want and your plan, thank you for explaining.

As you mentioned I tried to move some points, I could print their point value and see their positioned moved, but in this case, as you say, I must update my array of value because their color didn't change.

What would be the approach to calculate the value on the fly ? And you mention the "state" but I don't really see how this is related.

What I have in mind is:
- set up initial positions of dots and calculate initial distance
- function to fill red or grey
- "on.point:change" start a function to calculate new distance
- function to fill red or grey

But I have a hard time creating this new function. Could you give me a hint ? Thank you for your time !
Carl —

Carl Bl

Ok I managed to do it !
The only thing I don't understand is why it uses the top left corner of the circles and not their center, since it's CENTER by default and I didn't change that value.. :(

Read the entire post on Facebook