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

What is Framer? Join the Community
Return to index
Yohan Baillet
Posted Nov 26 - Read on Facebook

I am trying to target a layer created with a for ... in loop

for index in [0..7]
layer = new Layer
name: "layer"+index
width: 6
height: 6
x: 0
y: 50*index

All layer are named "layer1, layer2, layer3" but I can't access layer2.opacity = 0.2
Any idea if this is possible?

4 Comments

Javier Chávarri

One thing is the layer 'name' property. Another one is the variable name. In your code, each layer is created and then assign to a variable called 'layer' that is overwritten with every new iteration of the for loop.
In order to access each layer, you can use an array:

layers = []
for index in [0..7]
layer = new Layer
name: "layer"+index
width: 100
height: 100
x: 0
y: 50*index
layers.push(layer)

layers[0].animate
properties:
x: 200

Javier Chávarri

As far as I know, the layer 'name' property is helpful to identify different layers in the Framer Studio layer inspector. Also, when you import a Sketch or Photoshop file, it helps you identify the original layers in Framer.
http://framerjs.com/docs/#layer.name

Yohan Baillet

Excellent Javier, thanks for your answer! Exactly what I needed! :)

Marc Krenn

1. Add an empty array on top of your project:

layers = []

2. When creating those layers, you need to add them to the array:

layer = layers[index] = new Layer

3. Access those layers with

layers[0].opacity = 0.2 # arrays are zero-based, so index 0 equals layer1, 1 -> 2, 2->3, etc

Read the entire post on Facebook