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

What is Framer? Join the Community
Return to index
Jon Arnold
Posted Nov 12 - Read on Facebook

Hi guys,
I wonder how I can optimize this code (screenshot).
I copy a layer and want to position the copy on the exact same position without putting them in the same parent.
That's why always check for a parent and then add the parents.x,parents.y to a variable. Currently it only goes 5 times up. Can I write this more efficient and also do it till there is no parent anymore?

Thank you!

P.S.: This is for a new module that is coming soon :-)

3 Comments

Andrew Charkin

assuming I got my coffescript right (normally I code in es6)
You just need to use recursion with an accumulator. If those terms are meaningless I can go into more detail as to what they mean.

accumulateParentOffsets = (item, accumulator) ->
if !item.parent
accumulator
else
accumulator.x += item.parent.x
accumulator.y += item.parent.y
accumulateParentOffsets(item.parent, accumulator)

offset = accumulateParentOffsets(avatar, {x:avatar.x, y: avatar.y})

newAvatar = avatar.copy()
newAvatar.x = offset.x
newAvatar.y = offset.y

Jon Arnold

Exactly what is was looking for! Thank you

Liu Zhou

Maybe u can use layer.screenFrame

Read the entire post on Facebook