5 Replies Latest reply on Aug 2, 2013 2:33 PM by Michael L Hale

    stretching/translating a layer into another document

    strattonbrazil Level 1

      I probably just have my math wrong, but given a selection box boundary (or boundaries), I'm trying to resize it so that it's the size of a target document and then translate it so it's in the top-left corner taking up the whole screen (so what's in square selection takes up whole screen.  Now if I duplicate the layer in the same document and scale it using the document's dimensions and translate, it fills the whole screen.  However, if duplicate the layers into a target document and scale and translate according to its dimensions, the layer is the correct size, but wildly off position (most of the time off the page).  I would think the translation would be independent of the destination doc and based just on the scale and (minX,minY) of the selection box. 

       

      Is my math off or is there something funny about duplicating a layer into another document?  I'm guessing it's the former, but I keep doing it in my head and the math looks sensible. 

       

       

          var layer = app.activeDocument.activeLayer;
          //layer.isBackgroundLayer = false;
          var layers = new Array(thumbnails.length);
          //layers[0] = layer;
          for (var i = 0; i < layers.length; i++) {
                  layers[i] = layer.duplicate(documentTarget);
          }
       
          var srcDocument = app.activeDocument;
          app.activeDocument = documentTarget;
       
          // for every layer, go through translating and resizing based on the box size
          for (var i = 0; i < layers.length; i++) {
                  var selectionBox = thumbnails[i];
         

          var scaleX = documentTarget.width.as("px") / (selectionBox["maxX"] - selectionBox["minX"]);
                   var scaleY = documentTarget.height.as("px") / (selectionBox["maxY"] - selectionBox["minY"]);


             
             
         
             
             
         
                  layers[i].resize(scaleX * 100, scaleY * 100, AnchorPosition.TOPLEFT);
                  layers[i].translate(new UnitValue(-selectionBox["minX"] * scaleX, "px"),
                                                                         new UnitValue(-selectionBox["minY"] * scaleY, "px"));
          }


         


       


      }

      }

        • 1. Re: stretching/translating a layer into another document
          Michael L Hale Level 5

          If I understand what you are saying, the problem is layer position is based on the document canvas not the screen. So is selection bounds. Is this a Bridge script? Where is thumbnails coming from?

          • 2. Re: stretching/translating a layer into another document
            strattonbrazil Level 1

            Sorry, the thumbnails are drawings from a single scan into a photoshop document.  The user grabs the individual "thumbnails" with the marquee, and each own gets blown up into its own layer.  This is starts at that document and places the thumbnails in a different document.  What I don't understand is why placing them in a different document affects how they are positioned. 

            • 3. Re: stretching/translating a layer into another document
              Michael L Hale Level 5

              If we agree that when you said 'screen' in your first post you meant canvas then the problem could be from when you duplicate the layer to the other document. The duplicated layer will not be in the same position unless both documents are the same size and resolution. translate is relative to the current position of the duped layer - not it's original position in the source document. It looks like you are using values from the original position so the ending position is not what you expected.

               

              It is hard to say for sure, you didn't post the entire script. And what you did post will not run.

              • 4. Re: stretching/translating a layer into another document
                strattonbrazil Level 1

                So I'm confused.  Say I have a box at with minX,minY at 50,50, which is half of the target document, it gets scaled first to docWidth/docHeight and the corner where minX,minY is now at 100,100, right?  So I then translate the layer (from the TOPLEFT as a reference) by the original minX,minY * -scale (whatever I scaled by, which was 2x).  This shifts the layer so was at the original 50,50 is now at 0,0. 

                 

                If I apply this to the layer duplicated to another document, I expect the same results because the I'm using the scale of the target document.  Why would minX,minY be different assuming the layer is duplicated to the topleft of the target document at 0,0?  After scaling, minX,minY * -scale should be what's needed to move the duplicated layer into the corner, right? 

                1 person found this helpful
                • 5. Re: stretching/translating a layer into another document
                  Michael L Hale Level 5

                  .. Why would minX,minY be different assuming the layer is duplicated to the topleft of the target document at 0,0?  

                  That was the point I was trying to make. That assumption is incorrect. The duplicated layer will not be at 0,0 unless both documents are the same size and resolution. The duped layer is centered in the new doc. Only when the sizes match will the positions match. Otherwise the new top left position could be anywhere including off canvas( negative values ).

                   

                  If you don't want to change minX, minY for some reason you could add code to align the layer to the top left before the translate line.

                   

                  function align(type){
                     var desc = new ActionDescriptor();
                       var ref = new ActionReference();
                         ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Lnkd" ));
                     desc.putReference( charIDToTypeID( "null" ), ref);
                     desc.putEnumerated( charIDToTypeID( "Usng" ),charIDToTypeID( "ADSt" ), charIDToTypeID( type ) );
                     executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );;
                  };
                  function alignTopLeft(){
                    align("AdTp");
                    align("AdLf");
                  };
                  app.activeDocument.selection.selectAll();
                  alignTopLeft();
                  app.activeDocument.selection.deselect();