Skip to main content
Solved

Known bug getting x,y coordinates of rectangles within frames (but not groups)?

  • August 26, 2021
  • 3 replies
  • 2015 views

Timothy_Jaeger

I’m nearly done with my plugin, but need to access the x,y coordinates of RECTANGLE nodes that contain images. For some reason, I can access the coordinates of RECTANGLES within groups, but not frames. Unsure why this would be the case. Is this a known bug? Is there a workaround?

for (let i = 0; i < nodes.length; i++)
    {
      for (const paint of nodes[i].fills) {
        if (paint.type === 'IMAGE' && nodes[i].name.includes('Screen')) {
          ++counter;
          if(msg.checked === true) {
            const screenshotStar = figma.createStar();
            figma.currentPage.appendChild(screenshotStar);
            screenshotStar.x = nodes[i].x - 50;
            screenshotStar.y = nodes[i].y - 70;
            screenshotStar.name = "Screenshot Here";
            screenshotStar.fills = [{ type: 'SOLID', color: {r: 1, g: 0, b: 0} }];
            screenshotStar.strokes = [{ type: 'SOLID', color: {r: 1, g: 1, b: 1} }];
            screenshotStar.strokeWeight = 10;
            screenshotStar.resizeWithoutConstraints(150,150);
          }
          figma.ui.postMessage(counter);
       }
      }
    

Best answer by Gleb

What specifically is not working for you? You can get X and Y coordinates of nodes just fine no matter where they are placed. However note, that if the object is in a group, its coordinates will be relative to the first parent frame or canvas if there are no frames. If the object is directly in a frame, its coordinates are relative to the frame. You can get the absolute ones relative to the canvas if necessary via node.absoluteTransform.

View original

3 replies

Gleb
  • Power Member
  • 4706 replies
  • Answer
  • August 26, 2021

What specifically is not working for you? You can get X and Y coordinates of nodes just fine no matter where they are placed. However note, that if the object is in a group, its coordinates will be relative to the first parent frame or canvas if there are no frames. If the object is directly in a frame, its coordinates are relative to the frame. You can get the absolute ones relative to the canvas if necessary via node.absoluteTransform.


Timothy_Jaeger

Yes, that’s the problem. They are relative to the frame. Let me try the node.absoluteTransform property.


Tim_Jaeger

Works like a charm:

let newNode = nodes[i].absoluteTransform;
screenshotStar.x = newNode[0][2] - 50;
screenshotStar.y = newNode[1][2] - 50;

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings